perf: Introduce TypeKey instead of String keys for named types#590
Conversation
Click to see raw report |
|
This looks ok to me. However, I do recall @chenyan-dfinity being averse to introducing breaking changes to the API but I'm not sure how serious a problem this is. Another approach might have been to introduce a second, indexed Var constructor (IndexedVar), used by the binary decoder to index into an array, since the binary decoder should actually know the size of the type table. This would be even more efficient than using a hash table, I expect. |
|
The I did benchmark using a combination of a |
# Conflicts: # Changelog.md
67a1964
See #590 --------- Co-authored-by: David Frank <david.frank@dfinity.org>
--------- Co-authored-by: Linwei Shang <linwei.shang@dfinity.org> Co-authored-by: David Frank <david.frank@dfinity.org>
Overview
We can achieve serious performance improvements by introducing a new
TypeKeystruct instead ofStringfor candid types represented byType::Var. When deserializing a candid message, we need to store information about these types (e.g. in theTypeEnvstruct). Previously, we usedBTreeMapwithStringkeys. This is slow and a significant amount of time is spent looking up types by name.Optimization 1: distinguish indexed (integer) keys from String keys
When decoding type information from the message header in
binary_parser.rs, the types are indexed. Previously these indexed types were assigned aStringkey by callingformat!("table{index}"). Working with integer keys is much faster.Optimization 2: use
HashMapinstead ofBTreeMapLooking up values in a HashMap is much faster (especially if the keys are small).
Optimization 3: precompute hash of
String-basedTypeKeysBreaking changes
This is a breaking change:
Type::Var(var)varnow has typeTypeKeyinstead ofString. Callingvar.as_str()returns&strandvar.to_string()returns aString. The string representation of indexed variables remainstable{index}to maintain compatibility with previous versions.TypeEnvnow contains aHashMapinstead ofBTreeMap. Code that relied on the iteration order of the map (e.g.env.0.iter()) should make use of the newly addedTypeEnv::to_sorted_iter()method which returns types sorted by their keys.