You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Heavily inspired from ruby/json#675.
When parsing documents with the same structure repeatedly, a lot
of time can be saved by keeping a small cache of map keys encountered.
Using the existing `bench/bench.rb`, comparing with and without the
cache shows a 30% performance improvement:
```
Calculating -------------------------------------
unpack-pooled 960.380k (± 1.4%) i/s - 4.865M in 5.066600s
unpack-key-cache 1.245M (± 1.6%) i/s - 6.232M in 5.009060s
Comparison:
unpack-pooled: 960379.8 i/s
unpack-key-cache: 1244517.6 i/s - 1.30x (± 0.00) faster
```
However, on the same benchmark, but with the cache filled with other
keys, the performance is notably degraded:
```
Calculating -------------------------------------
unpack-pooled 926.849k (± 2.1%) i/s - 4.639M in 5.007333s
unpack-key-cache 822.266k (± 2.4%) i/s - 4.113M in 5.004645s
Comparison:
unpack-pooled: 926849.2 i/s
unpack-key-cache: 822265.6 i/s - 1.13x (± 0.00) slower
```
So this feature is powerful but situational.
Copy file name to clipboardExpand all lines: doclib/msgpack/unpacker.rb
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,8 @@ class Unpacker
19
19
# Supported options:
20
20
#
21
21
# * *:symbolize_keys* deserialize keys of Hash objects as Symbol instead of String
22
+
# * *:freeze* freeze the deserialized objects. Can allow string deduplication and some allocation elision.
23
+
# * *:key_cache* Enable caching of map keys, this can improve performance significantly if the same map keys are frequently encountered, but also degrade performance if that's not the case.
22
24
# * *:allow_unknown_ext* allow to deserialize ext type object with unknown type id as ExtensionValue instance. Otherwise (by default), unpacker throws UnknownExtTypeError.
0 commit comments