tc39 / proposal-policy-map-set

A TC39 proposal for Maps and Sets with cache replacement policies like LRU and LFU.
BSD 3-Clause "New" or "Revised" License
15 stars 2 forks source link

Cache use cases #4

Open js-choi opened 2 years ago

js-choi commented 2 years ago

My primary use case is to give control to the developer over the caches that the memo function would use for function memoization. For example, Python offers a LRU policy with a customizable limit.

@syg from V8 said:

We have a lot of pressure, from my observations, of large and "expert" apps asking for GC hooking points and GC info to be exposed for the purpose of writing userland caches. They might misguidedly use WeakRefs right now, which, with a “maybe clear on every GC”, is a particularly bad cache eviction policy. One realization the V8 team has had recently is well, maybe there's space for directly designing caches instead of exposing GC stuff, which seems blech. […] I have not thought yet deeply about if LIFO and FIFO with a simple size are sufficiently flexible eviction policies. [Use cases would include] roughly, the same kind of things Java's soft reference and prio references tried to solve.

@rickbutton added:

Userland caches with gc-hooks has come up on our side internally as something people are really asking for. Not convinced but it keeps getting mentioned. We also had the thought “what if we provided a cache instead”.

We need to come up with specific isolated illustrations of these use cases and check if they would be adequately served by Map-like data structures with constant limits.

js-choi commented 2 years ago

The withdrawn WICG memory-pressure proposal lists several use cases related to memory pressure and therefore to caching in general:

  • An application with an infinite-scroll wants to know if they need to remove items from their list and how agressively.
  • An application handling large files (photo/video sharing for example) wants to free up cache when the user's device need it.
  • An email application saving email content for fast interaction wants to free up the cache if it is needed by the system.