Skip to content

Use CommonDictionaryStorage for class variables#1531

Merged
BCSharp merged 2 commits intoIronLanguages:masterfrom
BCSharp:var_attrib
Aug 9, 2022
Merged

Use CommonDictionaryStorage for class variables#1531
BCSharp merged 2 commits intoIronLanguages:masterfrom
BCSharp:var_attrib

Conversation

@BCSharp
Copy link
Copy Markdown
Member

@BCSharp BCSharp commented Aug 8, 2022

Part of #20.

Because it effectively changes the name lookup mechanism in a class body lambda, I was curious what performance impact this may have. The old mechanism uses RuntimeVariableDictionaryStorage, which is backed by a fixed length array and a linear lookup. In the new code, only __class__ goes there, all other variables are stored in CommonDictionaryStorage, which grows dynamically. Since the new mechanism is only required for classes created by user metaclasses, it is possible to fall back on the old mechanism if no metaclass is specified, thus maintaining the existing performance profile in most cases.

Intuitively, the old mechanism should be better for small (few methods) classes, while the new approach should outperform for very large classes. But the question was where the break-even is and how much gain/loss it entails.

Here are best-case test results for classes of various sizes. N is the number of methods (with empty body). On top of that, each class has a method g returning __self__, which adds symbols g, __class__, __classcell__. Plus every class has the mandatory __module__. (CPython classes also have __qualname__).

N CPython* New Old
0 4.8 μs 3.2 μs 3.1 μs
10 6.4 μs 5.1 μs 5.1 μs
100 17 μs 24 μs 23 μs
500 66 μs 120 μs 154 μs

*CPython is for reference only, runs with GC disabled.
Standard deviation was in the order of 0.5 μs at the low end, 5 μs around N=100, and approx. 10 μs for N=500.

What it tells me that any differences between the two algorithms are negligible for any sensibly-sized class. The bulk of time is probably spend on parsing and namebinding, and the choice of storage is not a significant factor.

Copy link
Copy Markdown
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't kow much about this part of the codebase, but looks good to me.

}
}

private MSAst.Expression SeLocalName(string name, MSAst.Expression expression)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo? SetLocalName?

@BCSharp BCSharp merged commit 8cd4482 into IronLanguages:master Aug 9, 2022
@BCSharp BCSharp deleted the var_attrib branch August 9, 2022 07:58
@slozier slozier mentioned this pull request Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants