8000 It is necessary to check not only exact hits, but also non-existent keys. · Issue #20 · dominictarr/bench-lru · GitHub
[go: up one dir, main page]

Skip to content

It is necessary to check not only exact hits, but also non-existent keys. #20

Closed

Description

@repsac-by

In addition to using string keys.

An important parameter of reality is an unsuccessful search in the cache.
Misses will break off v8 optimization in implementations that use objects to store keys.

Activity

  1. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    [[GetOwnProperty]] ( P ), P is always a string - https://es5.github.io/#x8.12.1

    Using 0x & deoptigate I can't validate your claim that accessing a property which doesn't exist has any impact on the hidden class that v8 is looking at. Is there documentation that says otherwise?

  2. repsac-by commented on Nov 21, 2018

    @repsac-by
    Author

    V8 performs many optimizations that are sure to be compatible with the language specification, but do not have to follow it literally.

  3. repsac-by commented on Nov 21, 2018

    @repsac-by
    Author

    https://github.com/repsac-by/bench-lru/tree/kill-optimizations

    node --version 
    v11.2.0
    
    | name             | set  | get1 | update | get2 | evict |
    |------------------|------|------|--------|------|-------|
    | lru_cache        | 3173 | 3248 | 6560   | 3182 | 7125  |
    | quick-lru        | 4052 | 1705 | 5199   | 1820 | 6240  |
    | lru-cache        | 2179 | 2073 | 3064   | 2097 | 4605  |
    | hashlru          | 2499 | 2448 | 3893   | 2165 | 4097  |
    | tiny-lru         | 3161 | 3209 | 5516   | 3295 | 1812  |
    | simple-lru-cache | 1632 | 3072 | 4908   | 3307 | 1789  |
    | lru              | 1662 | 3323 | 5247   | 3268 | 1725  |
    | lru-fast         | 1781 | 4526 | 8636   | 4546 | 1501  |
    | js-lru           | 1494 | 2928 | 6333   | 3209 | 1431  |
    | hyperlru-object  | 1487 | 3277 | 4393   | 2933 | 1374  |
    | hyperlru-map     | 1226 | 2657 | 4990   | 2650 | 1276  |
    | secondary-cache  | 1092 | 2603 | 2833   | 2658 | 996   |
    | modern-lru       | 915  | 1581 | 2932   | 1572 | 958   |
    
  4. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    Ok, but this benchmark is only testing slow evicts, not how to deoptimize v8 with strings in megamorphic objects.

  5. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    Oh, I think I can kill perf much better... :)

    Sample worker.js script:

    'use strict';
    
    const precise = require('precise'),
      retsu = require('retsu'),
      LRUCache = require('lru_cache').LRUCache,
      Simple = require('simple-lru-cache'),
      Fast = require('lru-fast').LRUCache,
      QuickLRU = require('quick-lru'),
      Modern = require('modern-lru'),
      hyperlru = require('hyperlru'),
      {LRUMap} = require('lru_map'),
      MKC = require('mkc'),
      hyperlruObject = hyperlru(require('hyperlru-object')),
      hyperlruMap = hyperlru(require('hyperlru-map')),
      caches = {
        'lru-cache': () => require('lru-cache')(),
        'lru-fast': n => new Fast(n),
        'js-lru': n => new LRUMap(n),
        'modern-lru': n => new Modern(n),
        'quick-lru': maxSize => new QuickLRU({maxSize}),
        'secondary-cache': require('secondary-cache'),
        'simple-lru-cache': maxSize => new Simple({maxSize}),
        'tiny-lru': require('tiny-lru'),
        hashlru: require('hashlru'),
        'hyperlru-object': max => hyperlruObject({max}),
        'hyperlru-map': max => hyperlruMap({max}),
        lru_cache: n => new LRUCache(n),
        lru: require('lru')//,
        //mkc: max => new MKC({max})
      },
      num = 2e5,
      times = 5,
      x = 1e6,
      data1 = new Array(num),
      data2 = new Array(num);
    
    function letter () {
        return String.fromCharCode(Math.floor(Math.random() * 122))
    }
    
    (function seed () {
    	let z = -1;
    
    	while (++z < num) {
    		data1[z] = [`${letter()}${z}`, Math.floor(Math.random() * 1e7)];
    		data2[z] = [`${letter()}${z}`, Math.floor(Math.random() * 1e7)];
    	}
    }());
    
    self. (ev) {
      const id = ev.data,
        lru = caches[id](num),
        time = {
          'set': [],
          get1: [],
          update: [],
          get2: [],
          evict: []
        },
        results = {
          name: id,
          'set': 0,
          get1: 0,
          update: 0,
          get2: 0,
          evict: 0,
        };
    
      let n = -1;
    
      while (++n < times) {
        let stimer = precise().start();
        for (let i = 0; i < num; i++) lru.set(data1[i][0], data1[i][1]);
        time.set.push(stimer.stop().diff() / x);
    
        let gtimer = precise().start();
        for (let i = 0; i < num; i++) lru.get(data1[i][0]);
        time.get1.push(gtimer.stop().diff() / x);
    
        let utimer = precise().start();
        for (let i = 0; i < num; i++) lru.set(data1[i][0], data2[i][1]);
        time.update.push(utimer.stop().diff() / x);
    
        const g2timer = precise().start();
        for (let i = 0; i < num; i++) lru.get(data1[i][0]);
        time.get2.push(g2timer.stop().diff() / x);
    
        let etimer = precise().start();
        for (let i = 0; i < num; i++) lru.set(data2[i][0], data2[i][1]);
        time.evict.push(etimer.stop().diff() / x);
      }
    
      ['set', 'get1', 'update', 'get2', 'evict'].forEach(i => {
        results[i] = Number((num / retsu.median(time[i]).toFixed(2)).toFixed(0));
      });
    
      postMessage(JSON.stringify(results));
    };
    
    

    results in...

    | name             | set  | get1 | update | get2 | evict |
    |------------------|------|------|--------|------|-------|
    | lru_cache        | 2263 | 4281 | 3449   | 4526 | 3883  |
    | lru-cache        | 1515 | 2128 | 1770   | 1942 | 2227  |
    | quick-lru        | 1998 | 1477 | 2246   | 2458 | 2027  |
    | hashlru          | 1632 | 1648 | 1504   | 1687 | 1691  |
    | lru-fast         | 1002 | 4383 | 1087   | 3591 | 1125  |
    | tiny-lru         | 2422 | 3158 | 1152   | 932  | 1115  |
    | lru              | 879  | 2932 | 987    | 3003 | 988   |
    | hyperlru-object  | 711  | 1754 | 987    | 1990 | 974   |
    | js-lru           | 867  | 1983 | 1021   | 1402 | 919   |
    | hyperlru-map     | 841  | 1738 | 757    | 1724 | 831   |
    | simple-lru-cache | 632  | 943  | 611    | 806  | 568   |
    | secondary-cache  | 703  | 2279 | 474    | 1731 | 559   |
    | modern-lru       | 539  | 1754 | 633    | 1760 | 543   |
    

    deopts for all.

    edit: modified it to simply swap between the 2 datasets for evict vs twice the record count.

  6. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    Curious why mkc gives up the ghost.

  7. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator
  8. repsac-by commented on Nov 21, 2018

    @repsac-by
    Author

    @avoidwork , there must be an lru.get instead lru.set

    for (let i = 0; i < num; i++) lru.set(data2[i][0]);

    Perhaps I was mistaken that the missing keys will greatly influence. According to the tests, it turns out that there is almost no difference. However for reliability it is necessary to check.

  9. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    @repsac-by fixed the 1 line, also changed the evict to be more obvious imo (data1,data2 > data2,data2).

  10. avoidwork commented on Nov 21, 2018

    @avoidwork
    Collaborator

    @repsac-by if I add a third get utilizing the data2 keys there's no impact.

    | name             | set  | get1 | update | get2 | evict | get3 |
    |------------------|------|------|--------|------|-------|------|
    | lru_cache        | 2024 | 3702 | 3445   | 3431 | 3563  | 3923 |
    | lru-cache        | 1431 | 2338 | 1940   | 2222 | 2491  | 2438 |
    | quick-lru        | 2113 | 1487 | 2411   | 1574 | 2279  | 1346 |
    | hashlru          | 1538 | 1389 | 1743   | 1623 | 1777  | 1389 |
    | tiny-lru         | 2087 | 2837 | 3065   | 2718 | 1105  | 4412 |
    | simple-lru-cache | 1031 | 2253 | 2368   | 2395 | 1090  | 2858 |
    | lru-fast         | 977  | 3874 | 3324   | 4078 | 1062  | 4145 |
    | js-lru           | 801  | 1924 | 2022   | 2056 | 976   | 1783 |
    | hyperlru-object  | 853  | 1780 | 1774   | 1816 | 950   | 2029 |
    | lru              | 854  | 3213 | 2605   | 2973 | 801   | 3219 |
    | hyperlru-map     | 798  | 1953 | 1837   | 1830 | 731   | 1925 |
    | modern-lru       | 551  | 1830 | 1694   | 1850 | 612   | 1804 |
    | secondary-cache  | 580  | 1526 | 1499   | 2244 | 531   | 1766 |
    
    • os scheduling is why there's relatively large diff in numbers as i post results
  11. Yomguithereal commented on Nov 21, 2018

    @Yomguithereal
    Contributor

    Results on my end (dropping temporarily lru_cache because of the capacity leak and lru-cache because of #26):

    | name             | set  | get1 | update | get2 | evict |
    |------------------|------|------|--------|------|-------|
    | quick-lru        | 3479 | 2193 | 3658   | 2287 | 3297  |
    | hashlru          | 2587 | 2053 | 2592   | 2118 | 2818  |
    | mnemonist-object | 1612 | 7100 | 5903   | 6961 | 1912  |
    | mnemonist-map    | 1684 | 6081 | 5942   | 7418 | 1727  |
    | lru-fast         | 1621 | 5304 | 5569   | 5248 | 1542  |
    | tiny-lru         | 4373 | 3623 | 4288   | 3226 | 1505  |
    | hyperlru-object  | 1163 | 2257 | 2160   | 2235 | 1486  |
    | js-lru           | 1272 | 2574 | 2542   | 2523 | 1314  |
    | simple-lru-cache | 1165 | 2476 | 2464   | 2461 | 1270  |
    | lru              | 1353 | 4018 | 3969   | 3799 | 1207  |
    | lru-cache        | 1079 | 3095 | 2576   | 2920 | 1147  |
    | hyperlru-map     | 1094 | 2323 | 2208   | 2474 | 1032  |
    | secondary-cache  | 971  | 2556 | 2408   | 2956 | 773   |
    | modern-lru       | 753  | 2115 | 2030   | 2093 | 741   |
    

    I guess that one takeout is that in this case with killed perfs, the approach initiated by @dominictarr still is very good evict-wise while being not so good with gets and memory-wise (twice the memory amount since you need to store two maps).

  12. avoidwork commented on Nov 25, 2018

    @avoidwork
    Collaborator

    I've updated the test in master to utilize the consistent data set from the perf* killer above. We could have 2 benchmarks with Number & String keys; I don't think we should drop the Number test 'cause that's primarily what I use in production apps (I hope most people would), as it is a legitimate /10 for perf.

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

      0