Urgent.News

What's breaking now, across thousands of outlets.

Tech

Objects, attributes, maps, oh my

Many dynamically typed programming languages, such as Python, model objects as a collection of names and corresponding values. This design allows for flexibility and encourages the use of a hash table to map names to values within the virtual machine (VM). For example, a Python sketch demonstrates this object model using functions like write_attr and read_attr to access object attributes.

To improve performance, runtime implementers often create special data structures called maps, layouts, shapes, or hidden classes. These structures optimize the reading, writing, and storage of attributes. The implementation of these maps can be complex, with various edge cases and high-level overviews present in many blog posts. However, understanding the basics of this system can be challenging.

In this article, the author examines the CF Bolz-Tereick's "A Simple Object Model" and extracts the core features of a map system. The map serves three main purposes: acting as an index or write-through cache, reflecting the layout of a given object, and providing a stable identifier for a particular in-memory layout. This structure allows for quick checks to determine if the object's layout has changed, enabling faster loading of data.

The author also presents a Python implementation of objects with maps and the maps themselves. Although the storage is not in-line, as initially promised, the common case still reads and writes attributes using string names. The author notes that the index storage would typically be in-line in languages such as C++ or Rust. The implementation provides an efficient way to look up attributes, with a fast check followed by an indexed load in the common case. This approach aims to reduce the number of memory loads and improve overall performance.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at bernsteinbear.com →

More in Tech

More from Thursday 22 October →