WIP: faster low level modules#1221
Closed
nstarman wants to merge 3 commits into
Closed
Conversation
…e instantiation
_ModuleMeta.__call__ previously recomputed three things on every instantiation:
- dataclasses.fields(cls) — allocates a new tuple each call
- f.metadata.get("converter") / f.metadata.get("static") — per-field dict lookups
- for parent_cls in cls.__mro__: __check_init__ — full MRO scan each call
All three are now computed once at class-definition time in _ModuleMeta.__new__
and stored in four new _ModuleInfo fields:
- converter_fields: tuple of (name, converter) pairs
- static_field_names: tuple of static=True field names
- non_init_field_names: tuple of init=False field names
- check_init_methods: tuple of unbound __check_init__ functions from the MRO
__call__ now does a single _module_info[cls] lookup and iterates over
pre-filtered tuples, which are empty for the common case (no converters,
no static fields, no __check_init__).
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1a5811b to
de7e58b
Compare
de7e58b to
e29bd99
Compare
Contributor
Author
|
@patrick-kidger what do you think of this potential fast-path for low-level Modules? |
Owner
|
I think a Ultimately I definitely don't want a new class – as you note it's user-visible, but more than that it's simply past the complexity/speed tradeoff for would consider valuable. It's important to me that Equinox be easy to maintain. |
Contributor
Author
|
See #1230 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proof of concept.
Results in 21x speedup in BoundMethod initialization, translating into 14x speedup of method access from a Module.
This is stacked on top of speedups associated with #1220.
In
quaxI was doing some line profiling and found that some examples in the tests had 2k method accesses, making this a non-trivial slowdown when considering a whole code pipeline.This PR introduces a lower-level meta class and base class which internal Modules like
BoundMethodcan use, that bypasses theModule.__call__, andModule.__getattribte__. This is a WIP since there are details to be worked out, like:_AbstractModuleshould implement a from-style__setattr__, etc.But I do think something should be done, since creating a
BoundMethodon every method access is a common operation and is relatively slow, evidenced by this 14x performance improvement.The main issue is the mildly(?) user-facing change