jcrist · GitHub

@jcrist

This adds an optimized lookup for IntEnum types. For small/compact
ranges of values this uses a direct index with an offset. For larger
ranges (where a direct index would result in too much wasted space) a
simple hashmap is used. To avoid recreating the lookup table multiple
times, it's stored on the enum class itself as the `__msgspec_lookup__`
attribute.
This switches the decoding process from:
- Decode int64_t/uint64_t value
- Create python int from this value
- Get `IntEnum` class
- Lookup python int on `IntEnum` class
to
- Decode int64_t/uint64_t value
- Get lookup table (already stored in the decoder `TypeNode` tree)
- Lookup value in lookup table
On my machine this results in:
- ~12x improvement in decode speed for compact ranges (1, 2, 3, 4, ...)
of enums (as fast or faster than decoding an integer).
- ~6x improvement in decode speed for wider ranges that require a
hashtable (slightly slower than decoding an integer).
This makes using enums more useful, as they don't add any slowdown over
raw literal values.
This also makes it more efficient to add `Literal` support for integer
literals later (to be done in a later PR).

Read the original on github.com ↗