hn.today

Objects, attributes, maps, oh my

bernsteinbear.com14 points1 comments
Screenshot of Objects, attributes, maps, oh my

Explains how dynamic-language objects modeled as per-object name→value hash tables pay heavy costs in hashing and memory indirection, and shows how runtimes replace that with maps (aka shapes/hidden classes): immutable, persistent-tree descriptors that map attribute names to small integer slots and identify an object's in-memory layout. As attributes are added, maps produce new map nodes (next_map) and objects carry a map pointer plus a storage array indexed by slots. This removes string hashing from the fast path, enables inline attribute storage in low-level implementations, and gives a stable, cheap-to-compare layout identifier so callers can detect layout changes quickly.

Gives compact Python sketches for Base/Class/Instance, Map.get_index/next_map, and an InlineCache that caches a (map, index) pair and uses pointer equality to fast-path loads by index; on a miss it falls back to name lookup, updates the cache, and returns the value. Notes Python lacks true in-object inline storage, but the pattern still reduces loads and instructions in an interpreter; production runtimes and JITs use the same concepts and more aggressive optimizations to eliminate interpreted overhead and accelerate attribute access.

Read on bernsteinbear.com1 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

We have traits at home

We have traits at home

Zig uses two main approaches for polymorphism: runtime interfaces with dynamic dispatch and compile-time duck typing for static polymorphism. These methods offer different tradeoffs in performance and safety, with Zig emphasizing simplicity and compile-time checks over complex abstractions. (axelmagn.com)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.