PERF: dispatch on UnitSystem
value in _get_multiplier
#1216
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.
Before:
After:
On the current
@scoped_enum
implementation,UnitSystem.NATURAL_UNITS
isn't a constant, it's a dictionary lookup — one that incurs 80 bytes of memory allocation. Thus you can see the performance impact piling up in the "before" case in the order in which the unit systems are checked —DEVICE_BASE
, thenSYSTEM_BASE
, thenNATURAL_UNITS
. There are a few ways one might fix this:@scoped_enum
to makeUnitSystem.NATURAL_UNITS
behave more like an immutable struct lookup, either by actually making it one or doing something to clue the compiler into the fact that it will never change. This would be the best, most holistic solution, but I couldn't immediately figure out how we might do it without completely overhauling@scoped_enum
. I'll keep it in the back of my head though.setting.unit_system == IS.UnitSystem.NATURAL_UNITS
, dosetting.unit_system.value == 2
, etc. No dictionary lookup, but too hardcoded for me.Most of the cost savings are from dispatching on the
UnitSystem
value. Adding another layer of dispatch on the nothing-ness ofget_internal(c).units_info
seemed to make it a bit faster on top of that, so I've done that too.