Description
Feature or enhancement
With #28802 we only create dictionaries when needed, which means we need to guess how big to create the values array attached to each object.
We use the following heuristic: Start with 30 items and reduce by one each time we create an object until the size of the array is no more than one greater than the number of keys in the shared keys.
This works reasonably well, but it could be improved. Many classes have a fixed set of attributes, but they are not all used during early object creation. Static analysis could give us a better estimate of the size of values array to use, by pre-initializing the shared keys.
For each class body we should have the compiler inject a special attribute, say __expected_attributes__
, which can be used at runtime to compute the expected attributes for the class.
For an example of code where static analysis could work, but our current dynamic approach does not, see https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_go/run_benchmark.py