Objects, attributes, maps, oh my
This article explains how dynamic programming languages optimize object attribute storage using 'shapes' or 'hidden classes' instead of simple hash tables. It provides a technical breakdown of how VMs improve performance for object-oriented code.
Why it matters
Understanding these internal optimizations is crucial for developers writing high-performance code in languages like Python, Ruby, and JavaScript.
In many dynamically typed programming languages, objects look like a big ol’ mapping of names to values. The languages encourage this flexibility. Consider for example Python:
class C : def __init__ ( self ): self . a = 1 self . b = 2 Or Ruby:
class C def initialize @a = 1 @b = 2 end end Or JavaScript 1 :
let obj = {}; obj . a = 1 ; obj . b = 2 ; This might encourage the VM implementor to model object attributes using a hash table. Hash tables, after all, are very good at mapping names to values.
Here is, by way of example, a Python sketch of what this object model looks like. Because it is a sketch, it uses write_attr and read_attr instead of the much more common syntax that your programming language comes with.
Get smarter about the news
Sign up free for a feed built around what you actually care about, Dive Deeper research on any story, and the full text of every article.
Create free accountAlready have an account? Sign in