From 3d8000a53f3327ebcdccfa4cd172715989b798a0 Mon Sep 17 00:00:00 2001 From: Soreine Date: Thu, 3 Nov 2016 13:18:36 +0100 Subject: [PATCH 1/5] Add global variable to disable memoization at any time --- src/utils/memoize.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils/memoize.js b/src/utils/memoize.js index c49bda1c7..b0d7d3745 100644 --- a/src/utils/memoize.js +++ b/src/utils/memoize.js @@ -42,6 +42,10 @@ function memoize(object, properties) { } object[property] = function (...args) { + if (window.__NO_MEMOIZE) { + return original.apply(this, args) + } + const keys = [property, ...args] this.__cache = this.__cache || new Map() From e505b7a1598e2702e96da1b8bf64eae2d451afc4 Mon Sep 17 00:00:00 2001 From: Soreine Date: Thu, 3 Nov 2016 13:19:21 +0100 Subject: [PATCH 2/5] Remove ludicrous amount of setup work that was needed to prevent memoization --- perf/index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/perf/index.js b/perf/index.js index 10497782e..2551c0817 100644 --- a/perf/index.js +++ b/perf/index.js @@ -22,6 +22,8 @@ const readMetadata = require('read-metadata') const { Raw } = require('..') const { resolve } = require('path') +window.__NO_MEMOIZE = true + const DEFAULT_BENCHMARK = { setup(state) { return state }, run(state) {} @@ -95,27 +97,19 @@ function runBenchmarks() { // memoization between calls to `fn` const scope = global.getScope() - let states = [] - let numberOfExecution = this.count - while (numberOfExecution--) { - states.push( - // Each benchmark is given the chance to do its own setup - scope.benchmark.setup( - scope.Raw.deserialize(scope.input, { terse: true }) - ) - ) - } - - let stateIndex = 0 + const state = + // Each benchmark is given the chance to do its own setup + scope.benchmark.setup( + scope.Raw.deserialize(scope.input, { terse: true }) + ) }, // Because of the way BenchmarkJS compiles the functions, // the variables declared in `setup` are visible to `fn` fn() { - scope.benchmark.run(states[stateIndex]) // eslint-disable-line no-undef + scope.benchmark.run(state) // eslint-disable-line no-undef // Next call will use another State instance - stateIndex++ // eslint-disable-line no-undef } })) } From bada4fa3f17c277d15b2edc3fe1d5d642d8b1155 Mon Sep 17 00:00:00 2001 From: Soreine Date: Thu, 3 Nov 2016 13:19:55 +0100 Subject: [PATCH 3/5] Add teardown for benchmarks, and adapt memoize-utils benchmark --- perf/benchmarks/memoize-util/index.js | 8 ++++++++ perf/index.js | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/perf/benchmarks/memoize-util/index.js b/perf/benchmarks/memoize-util/index.js index 668974862..fdc538ff5 100644 --- a/perf/benchmarks/memoize-util/index.js +++ b/perf/benchmarks/memoize-util/index.js @@ -2,6 +2,8 @@ const { default: memoize } = require('../../../lib/utils/memoize') module.exports = { setup(state) { + window.__NO_MEMOIZE = false + let obj = { fibonacci(n = 20) { if (n === 0 || n === 1) { @@ -18,5 +20,11 @@ module.exports = { run(obj) { obj.fibonacci() + // Clear cache for next runs + delete obj.__cache + }, + + teardown() { + window.__NO_MEMOIZE = true } } diff --git a/perf/index.js b/perf/index.js index 2551c0817..d41e392e7 100644 --- a/perf/index.js +++ b/perf/index.js @@ -110,6 +110,10 @@ function runBenchmarks() { fn() { scope.benchmark.run(state) // eslint-disable-line no-undef // Next call will use another State instance + }, + + onComplete() { + global.getScope().benchmark.teardown() } })) } From 14241039757960657c224a89346ff6e2c0fa3bdf Mon Sep 17 00:00:00 2001 From: Soreine Date: Mon, 7 Nov 2016 15:38:10 +0100 Subject: [PATCH 4/5] Do not use window for globals. Expose clear/disable functions in utils/memoize --- perf/benchmarks/memoize-util/index.js | 8 ----- perf/index.js | 20 +++++------ src/utils/memoize.js | 52 +++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/perf/benchmarks/memoize-util/index.js b/perf/benchmarks/memoize-util/index.js index fdc538ff5..668974862 100644 --- a/perf/benchmarks/memoize-util/index.js +++ b/perf/benchmarks/memoize-util/index.js @@ -2,8 +2,6 @@ const { default: memoize } = require('../../../lib/utils/memoize') module.exports = { setup(state) { - window.__NO_MEMOIZE = false - let obj = { fibonacci(n = 20) { if (n === 0 || n === 1) { @@ -20,11 +18,5 @@ module.exports = { run(obj) { obj.fibonacci() - // Clear cache for next runs - delete obj.__cache - }, - - teardown() { - window.__NO_MEMOIZE = true } } diff --git a/perf/index.js b/perf/index.js index d41e392e7..5dc8132fa 100644 --- a/perf/index.js +++ b/perf/index.js @@ -20,9 +20,9 @@ const fs = require('fs') const _ = require('lodash') const readMetadata = require('read-metadata') const { Raw } = require('..') -const { resolve } = require('path') +const memoize = require('../lib/utils/memoize') -window.__NO_MEMOIZE = true +const { resolve } = require('path') const DEFAULT_BENCHMARK = { setup(state) { return state }, @@ -77,6 +77,7 @@ function runBenchmarks() { // Setup global scope for this benchmark global.setScope(benchmarkName, { Raw, + memoize, benchmark, input }) @@ -93,15 +94,11 @@ function runBenchmarks() { // Time spent in setup is not taken into account setup() { - // Create as much independant Slate.State as needed, to avoid - // memoization between calls to `fn` const scope = global.getScope() - - const state = - // Each benchmark is given the chance to do its own setup - scope.benchmark.setup( - scope.Raw.deserialize(scope.input, { terse: true }) - ) + // Each benchmark is given the chance to do its own setup + const state = scope.benchmark.setup( + scope.Raw.deserialize(scope.input, { terse: true }) + ) }, // Because of the way BenchmarkJS compiles the functions, @@ -109,7 +106,8 @@ function runBenchmarks() { fn() { scope.benchmark.run(state) // eslint-disable-line no-undef - // Next call will use another State instance + // Clear memoized values between each run + scope.memoize.__clear() // eslint-disable-line no-undef }, onComplete() { diff --git a/src/utils/memoize.js b/src/utils/memoize.js index b0d7d3745..2dcd53671 100644 --- a/src/utils/memoize.js +++ b/src/utils/memoize.js @@ -1,5 +1,18 @@ /* global Map */ +import IS_DEV from './is-dev' + +/** + * This module serves to memoize methods on immutable instances. + */ + +// Global: True if memoization should is enabled. Only effective in DEV mode +let ENABLED = true + +// Global: Changing this cache key will clear all previous cached +// results. Only effective in DEV mode +let CACHE_KEY = 0 + /** * The leaf node of a cache tree. Used to support variable argument length. * @@ -42,8 +55,15 @@ function memoize(object, properties) { } object[property] = function (...args) { - if (window.__NO_MEMOIZE) { - return original.apply(this, args) + if (IS_DEV) { + if (!ENABLED) { + // Memoization disabled + return original.apply(this, args) + } else if (CACHE_KEY !== this.__cache_key) { + // Previous caches must be cleared + this.__cache_key = CACHE_KEY + this.__cache = new Map() + } } const keys = [property, ...args] @@ -118,8 +138,34 @@ function getIn(map, keys) { return childMap.get(LEAF) } +/** + * In DEV mode, clears the previously memoized values, globally. + * @return {Void} + */ + +function __clear() { + CACHE_KEY++ + if (CACHE_KEY >= Number.MAX_SAFE_INTEGER) { + CACHE_KEY = 0 + } +} + +/** + * In DEV mode, enable or disable the use of memoize values, globally. + * @param {Boolean} enabled + * @return {Void} + */ + +function __enable(enabled) { + ENABLED = enabled +} + /** * Export. */ -export default memoize +export { + memoize as default, + __clear, + __enable +} From f51189ce9adaf143cb14a9185e011f09e439d6f1 Mon Sep 17 00:00:00 2001 From: Soreine Date: Mon, 7 Nov 2016 15:38:47 +0100 Subject: [PATCH 5/5] Fix teardown --- perf/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/perf/index.js b/perf/index.js index 5dc8132fa..b69f33ccc 100644 --- a/perf/index.js +++ b/perf/index.js @@ -111,7 +111,8 @@ function runBenchmarks() { }, onComplete() { - global.getScope().benchmark.teardown() + const teardown = global.getScope().benchmark.teardown + if (teardown) teardown() } })) }