perf(c++): optimize type dispatch performance by a fast flat-int map #2966
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.
Why?
Type dispatch is a hot path in serialization/deserialization. The current implementation uses
absl::flat_hash_mapfor type lookups by compile-time type ID (uint64_t) and runtime type ID (uint32_t). Whileabsl::flat_hash_mapis a high-quality general-purpose hash map, we can achieve better performance with a specialized data structure optimized specifically for integer keys.What does this PR do?
This PR introduces a specialized
FlatIntMapdata structure optimized for integer keys (uint32_t/uint64_t) to accelerate type dispatch lookups in the serialization path.Key optimizations:
0x9E3779B97F4A7C15) for excellent key distribution with a single multiply operationFORY_ALWAYS_INLINE: Ensures critical lookup methods are inlinedChanges:
cpp/fory/util/flat_int_map.hwithFlatIntMap<K, V>template and convenience aliases (U64PtrMap,U32PtrMap, etc.)cpp/fory/util/flat_int_map_test.ccabsl::flat_hash_map<uint64_t, TypeInfo*>withU64PtrMap<TypeInfo>for:type_info_by_ctid_(compile-time type ID lookups)partial_type_infos_absl::flat_hash_map<uint32_t, TypeInfo*>withU32PtrMap<TypeInfo>for:type_info_by_id_(runtime type ID lookups)field_name_to_index()method fromTypeResolverRelated issues
#2958
Does this PR introduce any user-facing change?
TypeResolver::field_name_to_index<T>()method (appears unused)Benchmark
Benchmarks run on Apple M2 Max (arm64), 12 cores, 48GB RAM.
After this PR
Before this PR
Analysis
The benchmark results show variance between runs (note Protobuf numbers also changed significantly between measurements), but the key observation is that the
FlatIntMapprovides a specialized, cache-friendly implementation for the type dispatch hot path. The Fibonacci hashing technique combined with power-of-2 sizing eliminates expensive hash computations and provides excellent key distribution.The main benefits are: