How dynamic language code gets optimized
Yesterday I mentioned in passing that code that used Python dictionaries in predictable struct-like ways could probably be optimized just as similar uses of Python class instances is (in JIT environments like eg PyPy). This may raise some people's eyebrows; after all, aren't Python dictionaries too complex and flexible to get optimized?
(Sometimes this behavior is recognized dynamically, as the code executes, and sometimes it's recognized statically as the code is inspected. And of course there are schemes that do both.)
But there's still two reasons to care about what language level features and data structures get used for something, because all of this optimization is a heuristic. First, you care about how likely it is that your attempts to find behavior will work and this is related to how people actually use the language. If most people use classes as structures instead of dictionaries, then it's must more likely that your structure-recognition heuristics will succeed if you look at class usage. Time spent carefully tracking dictionary usage to spot structure usage patterns may generally be wasted time.
Second, even when you think you recognize a pattern of behavior you may turn out to be wrong; after behaving 'right' for a while the code may then turn around and do something that invalidates the pattern. This can waste some or all of your effort in optimizing for the pattern, so you also care about how often this happens with any particular sort of object. Again this will be related to how people use the language; if most people use dictionaries flexibly, you're much more likely to have false positives on temporary 'structure like' behavior with a particular dictionary and section of code that's invalidated later.
Lack of freedom in the language for how something is used certainly
helps optimization, in part because it means that you don't even have to
consider or worry about certain possibilities. Python's
is an example of this. But it's generally not an essential prerequisite.