Skip to content

Cache is never used #95

Closed
Closed

Description

@dy

htm/src/index.mjs

Lines 193 to 199 in c5764b5

const getCacheMap = (statics) => {
let tpl = CACHE.get(statics);
if (!tpl) {
CACHE.set(statics, tpl = build(statics));
}
return tpl;
};

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.

Activity

  1. developit commented on Jul 2, 2019

    @developit
    Owner

    The key is not recreated on each invocation, since Tagged Templates consistently reference the strings Array on subsequent calls, based on callsite.

    const htm = strings => strings;
    
    function test() {
      // on every call, the first argument to `htm` is the same reference:
      return htm`statics`;
    }
    
    test() === test()  // true
  2. jviide commented on Jul 2, 2019

    @jviide
    Collaborator

    This can be demonstrated by instrumenting getCacheMap in the following way:

    const getCacheMap = statics => {
      let tpl = CACHE.get(statics);
      console.log("cache hit:", Boolean(tpl));
      if (!tpl) {
        CACHE.set(statics, (tpl = build(statics)));
      }
      return tpl;
    };

    Building the modified htm and using it the following way:

    const htm = require("./dist/htm");
    
    const html = htm.bind((type, props, ...children) => ({
      type,
      props,
      children
    }));
    
    for (let i = 0; i < 5; i++) {
      html`
        statics
      `;
    }

    prints to the console:

    cache hit: false
    cache hit: true
    cache hit: true
    cache hit: true
    cache hit: true
    
  3. dy commented on Jul 2, 2019

    @dy
    Author

    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.

  4. jviide commented on Jul 2, 2019

    @jviide
    Collaborator

    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?

  5. dy commented on Jul 2, 2019

    @dy
    Author

    @jviide I think I've tested that from "0 callsite" from the console, in which case it didn't work:

    let prev 
    function x (s) {
    console.log(s === prev)
    prev = s
    }
    x`statics` // false
    x`statics` // false
    x`statics` // false
    
    for (let i = 0; i < 3; i++) x`statics`
    // false
    // true
    // true
  6. dy commented on Jul 2, 2019

    @dy
    Author

    Also - just out of curiosity - why there's length used

    key += statics[i].length + '-' + statics[i];

    instead of simple join key = statics.join('-')?

  7. jviide commented on Jul 2, 2019

    @jviide
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions