prismicio / prismic-ts-codegen

A Prismic model-to-TypeScript-type generator.
Apache License 2.0
18 stars 6 forks source link

refactor: use LRU cache for better performance #53

Closed angeloashmore closed 1 year ago

angeloashmore commented 1 year ago

Types of changes

Description

This PR implements an LRU cache for runtime caching.

Code blocks are cached at the custom type and Slice level. If a custom type's field is edited, for example, the whole custom type will be regenerated. All other types will be loaded from the cache, if still in memory.

The LRU cache is configured to hold 1000 items, which should be a safe amount to avoid memory issues.

With a warm cache, generateTypes() is ~4600x faster than v0.1.11.

Without a cache, generateTypes() is ~1000x faster than v0.1.11.

> prismic-ts-codegen@0.1.11 bench
> vitest bench --watch=false

Benchmarking is an experimental feature.
Breaking changes might not follow semver, please pin Vitest's version when using it.

 RUN  v0.31.1 /Users/angeloashmore/projects/prismic/prismic-ts-codegen

 ✓ test/generateTypes.bench.ts (4) 37082ms
   ✓ cached (2) 25218ms
     name                            hz       min       max      mean       p75       p99      p995
  p999     rme  samples
   · generate types (src)      3,907.07    0.2010    0.7658    0.2559    0.2480    0.5039    0.5783
0.6669  ±1.15%     1954   fastest
   · generate types (v0.1.11)    0.8405  1,182.37  1,204.27  1,189.74  1,190.79  1,204.27  1,204.27  1,
204.27  ±0.77%       10
   ✓ uncached (2) 37080ms
     name                          hz       min       max      mean       p75       p99      p995
p999     rme  samples
   · generate types (src)      850.76    1.0382    2.8518    1.1754    1.2435    1.5750    1.7920    2.
8518  ±1.39%      426   fastest
   · generate types (v0.1.11)  0.8431  1,179.69  1,193.36  1,186.13  1,187.37  1,193.36  1,193.36  1,19
3.36  ±0.72%       10

 BENCH  Summary

  generate types (src) - test/generateTypes.bench.ts > cached
    4648.39x faster than generate types (v0.1.11)

  generate types (src) - test/generateTypes.bench.ts > uncached
    1009.10x faster than generate types (v0.1.11)

Checklist: