Skip to content
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

Unpacker: key_cache option. #378

Merged
merged 1 commit into from
Nov 14, 2024
Merged

Unpacker: key_cache option. #378

merged 1 commit into from
Nov 14, 2024

Conversation

casperisfine
Copy link

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, hence why it is opt-in.

@casperisfine casperisfine force-pushed the key-cache-2 branch 2 times, most recently from 5092d4f to ed4491f Compare November 13, 2024 16:03
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.
@byroot byroot merged commit eb4859d into msgpack:master Nov 14, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants