-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Introduce TypeKey instead of String keys for named types #590
base: master
Are you sure you want to change the base?
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
Overview
We can achieve serious performance improvements by introducing a new
TypeKey
struct instead ofString
for candid types represented byType::Var
. When deserializing a candid message, we need to store information about these types (e.g. in theTypeEnv
struct). Previously, we usedBTreeMap
withString
keys. 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 aString
key by callingformat!("table{index}")
. Working with integer keys is much faster.Optimization 2: use
HashMap
instead ofBTreeMap
Looking up values in a HashMap is much faster (especially if the keys are small).
Optimization 3: precompute hash of
String
-basedTypeKey
sBreaking changes
This is a breaking change:
Type::Var(var)
var
now has typeTypeKey
instead ofString
. Callingvar.as_str()
returns&str
andvar.to_string()
returns aString
. The string representation of indexed variables remainstable{index}
to maintain compatibility with previous versions.TypeEnv
now contains aHashMap
instead 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.