You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here map stores compiled result by statics array as a key. But - each time we call htm`statics` the statics array is always new one, therefore cache just gets bloated and never fetches existing result. That can be checked.
So since Map requires joining statics, same way as getCacheKeyed, there's no much sense to keep getCacheMap.
The key is not recreated on each invocation, since Tagged Templates consistently reference the strings Array on subsequent calls, based on callsite.
consthtm=strings=>strings;functiontest(){// on every call, the first argument to `htm` is the same reference:returnhtm`statics`;}test()===test()// true
Yup, there'll be some redundancy. We figured that the performance win would be worth it in the long run, as the Map-based cache is quite a bit faster.
While the callsite-based behavior is in the specification (IIRC), there are some platforms that diverge from the spec. For example some older TypeScript 2.x versions. Maybe you bumped into one of those cases?
That's because the cache key would then be non-unique. As an example, consider the cases when statics is ["a", "b-c"] and when statics is ["a-b", "c"]. In both of these cases the cache key would be "a-b-c", and if both of those cases appeared in the same program there would be incorrect results. More thorough examples available in e.g. #22.
Activity
developit commentedon Jul 2, 2019
The key is not recreated on each invocation, since Tagged Templates consistently reference the strings Array on subsequent calls, based on callsite.
jviide commentedon Jul 2, 2019
This can be demonstrated by instrumenting
getCacheMapin the following way:Building the modified
htmand using it the following way:prints to the console:
dy commentedon Jul 2, 2019
If cache depends on callsite that’s still a bit of redundancy, not critical though I guess.
Thanks for the explanation, I have to check what I did wrong why I couldn’t reproduce that.
jviide commentedon Jul 2, 2019
Yup, there'll be some redundancy. We figured that the performance win would be worth it in the long run, as the Map-based cache is quite a bit faster.
While the callsite-based behavior is in the specification (IIRC), there are some platforms that diverge from the spec. For example some older TypeScript 2.x versions. Maybe you bumped into one of those cases?
dy commentedon Jul 2, 2019
@jviide I think I've tested that from "0 callsite" from the console, in which case it didn't work:
dy commentedon Jul 2, 2019
Also - just out of curiosity - why there's
lengthusedhtm/src/index.mjs
Line 204 in c5764b5
instead of simple join
key = statics.join('-')?jviide commentedon Jul 2, 2019
That's because the cache key would then be non-unique. As an example, consider the cases when
staticsis["a", "b-c"]and whenstaticsis["a-b", "c"]. In both of these cases the cache key would be"a-b-c", and if both of those cases appeared in the same program there would be incorrect results. More thorough examples available in e.g. #22.