cachette icon indicating copy to clipboard operation
cachette copied to clipboard

ðŸ’Ą [major] Prefer MSGPack over JSON

Open christianblais opened this issue 1 year ago â€Ē 1 comments

Cachette cached values aren't meant to be human readable. This lib is meant to be fast. Therefore, I believe we can achieve a little bit more speed by avoiding JSON serialization, and instead use more performant algorithms, such as msgpack. With this PR, I'm replacing JSON with msgpacker. We would incur a small hit on writes, but gain interesting wins on reads.

5000 writes;

Platform info:
==============
   Darwin 23.3.0 arm64
   Node.JS: 18.20.4
   V8: 10.2.154.26-node.37
   CPU: Apple M2 × 8
   Memory: 16 GB

Suite: serialize
================

✔ msgpack       126,894 ops/sec
✔ json          131,671 ops/sec

   msgpack        -3.63%    (126,894 ops/sec)   (avg: 7Ξs)
   json (#)           0%    (131,671 ops/sec)   (avg: 7Ξs)

┌─────────┮────────────────────────────────────────────────────┐
│ msgpack │ ████████████████████████████████████████████████   │
├─────────┾────────────────────────────────────────────────────â”Ī
│ json    │ ██████████████████████████████████████████████████ │
└─────────â”ī────────────────────────────────────────────────────┘

5000 reads;

Platform info:
==============
   Darwin 23.3.0 arm64
   Node.JS: 18.20.4
   V8: 10.2.154.26-node.37
   CPU: Apple M2 × 8
   Memory: 16 GB

Suite: parse
================

✔ msgpack        82,745 ops/sec
✔ json           59,111 ops/sec

   msgpack       +39.98%     (82,745 ops/sec)   (avg: 12Ξs)
   json (#)           0%     (59,111 ops/sec)   (avg: 16Ξs)

┌─────────┮────────────────────────────────────────────────────┐
│ msgpack │ ██████████████████████████████████████████████████ │
├─────────┾────────────────────────────────────────────────────â”Ī
│ json    │ ████████████████████████████████████               │
└─────────â”ī────────────────────────────────────────────────────┘

Given cachette is all about optimizing reads, I believe this to be a good trade-off. Here, assuming a 5:1 read:write ratio, we see a 34% overall gain.

1000 reads, 5000 writes

Platform info:
==============
   Darwin 23.3.0 arm64
   Node.JS: 18.20.4
   V8: 10.2.154.26-node.37
   CPU: Apple M2 × 8
   Memory: 16 GB

Suite: serialize
================

✔ msgpack        24,490 ops/sec
✔ json           18,211 ops/sec

   msgpack       +34.48%     (24,490 ops/sec)   (avg: 40Ξs)
   json (#)           0%     (18,211 ops/sec)   (avg: 54Ξs)

┌─────────┮────────────────────────────────────────────────────┐
│ msgpack │ ██████████████████████████████████████████████████ │
├─────────┾────────────────────────────────────────────────────â”Ī
│ json    │ █████████████████████████████████████              │
└─────────â”ī────────────────────────────────────────────────────┘

Note: Careful, as this PR is not revertable. Values set as buffer depends on the code present in this PR to be deserialized.

christianblais avatar Aug 15 '24 18:08 christianblais