1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 03:11:44 +02:00

Merge pull request #5 from GitbookIO/memoization-global-disabler

Global memoization control, faster benchmark script
This commit is contained in:
Samy Pessé
2016-11-07 15:51:18 +01:00
committed by GitHub
2 changed files with 66 additions and 19 deletions

View File

@@ -20,6 +20,8 @@ const fs = require('fs')
const _ = require('lodash')
const readMetadata = require('read-metadata')
const { Raw } = require('..')
const memoize = require('../lib/utils/memoize')
const { resolve } = require('path')
const DEFAULT_BENCHMARK = {
@@ -75,6 +77,7 @@ function runBenchmarks() {
// Setup global scope for this benchmark
global.setScope(benchmarkName, {
Raw,
memoize,
benchmark,
input
})
@@ -91,31 +94,25 @@ 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()
let states = []
let numberOfExecution = this.count
while (numberOfExecution--) {
states.push(
// Each benchmark is given the chance to do its own setup
scope.benchmark.setup(
const state = scope.benchmark.setup(
scope.Raw.deserialize(scope.input, { terse: true })
)
)
}
let stateIndex = 0
},
// 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
// Next call will use another State instance
stateIndex++ // eslint-disable-line no-undef
scope.benchmark.run(state) // eslint-disable-line no-undef
// Clear memoized values between each run
scope.memoize.__clear() // eslint-disable-line no-undef
},
onComplete() {
const teardown = global.getScope().benchmark.teardown
if (teardown) teardown()
}
}))
}

View File

@@ -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,6 +55,17 @@ function memoize(object, properties) {
}
object[property] = function (...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]
this.__cache = this.__cache || new Map()
@@ -114,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
}