OpenGen / GenSQL.query

42 stars 5 forks source link

Add caching #86

Closed KingMob closed 7 months ago

KingMob commented 7 months ago

Add caching, tests, and cache scalar pdf/prob/condition/constrain/mutual-info fns in clj

Also bump up inferenceql.inference version to avoid arrow constructor bug

Notes

CLJS does not have core.cache or core.memoize. And in Js, there were surprisingly few comprehensive options for caching, so I went with one that seemed popular, well-maintained, and flexible enough to handle custom keys.

The Clojure hash fn hashes both 0 and nil to the same value. Thus hash alone cannot distinguish between {:x 0} and {:x nil}. (Hash maps have fallbacks to handle collisions, and disambiguate between 0 and nil, but these don't work with most Js cache implementations afaict.)

There are two popular solutions to this problem:

  1. Rely on identity. This might work well, given persistent data structures, at the cost of cache misses on different vars with identical contents.
  2. Use JSON/stringify. Recommended, and should be pretty optimized under modern browsers. May need to revisit if benchmarks show otherwise.