mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 19:22:35 +02:00
Merge pull request #5 from GitbookIO/memoization-global-disabler
Global memoization control, faster benchmark script
This commit is contained in:
@@ -20,6 +20,8 @@ const fs = require('fs')
|
|||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const readMetadata = require('read-metadata')
|
const readMetadata = require('read-metadata')
|
||||||
const { Raw } = require('..')
|
const { Raw } = require('..')
|
||||||
|
const memoize = require('../lib/utils/memoize')
|
||||||
|
|
||||||
const { resolve } = require('path')
|
const { resolve } = require('path')
|
||||||
|
|
||||||
const DEFAULT_BENCHMARK = {
|
const DEFAULT_BENCHMARK = {
|
||||||
@@ -75,6 +77,7 @@ function runBenchmarks() {
|
|||||||
// Setup global scope for this benchmark
|
// Setup global scope for this benchmark
|
||||||
global.setScope(benchmarkName, {
|
global.setScope(benchmarkName, {
|
||||||
Raw,
|
Raw,
|
||||||
|
memoize,
|
||||||
benchmark,
|
benchmark,
|
||||||
input
|
input
|
||||||
})
|
})
|
||||||
@@ -91,31 +94,25 @@ function runBenchmarks() {
|
|||||||
|
|
||||||
// Time spent in setup is not taken into account
|
// Time spent in setup is not taken into account
|
||||||
setup() {
|
setup() {
|
||||||
// Create as much independant Slate.State as needed, to avoid
|
|
||||||
// memoization between calls to `fn`
|
|
||||||
const scope = global.getScope()
|
const scope = global.getScope()
|
||||||
|
// Each benchmark is given the chance to do its own setup
|
||||||
let states = []
|
const state = scope.benchmark.setup(
|
||||||
let numberOfExecution = this.count
|
scope.Raw.deserialize(scope.input, { terse: true })
|
||||||
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
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Because of the way BenchmarkJS compiles the functions,
|
// Because of the way BenchmarkJS compiles the functions,
|
||||||
// the variables declared in `setup` are visible to `fn`
|
// the variables declared in `setup` are visible to `fn`
|
||||||
|
|
||||||
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
|
// Clear memoized values between each run
|
||||||
stateIndex++ // eslint-disable-line no-undef
|
scope.memoize.__clear() // eslint-disable-line no-undef
|
||||||
|
},
|
||||||
|
|
||||||
|
onComplete() {
|
||||||
|
const teardown = global.getScope().benchmark.teardown
|
||||||
|
if (teardown) teardown()
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,18 @@
|
|||||||
/* global Map */
|
/* 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.
|
* 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) {
|
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]
|
const keys = [property, ...args]
|
||||||
this.__cache = this.__cache || new Map()
|
this.__cache = this.__cache || new Map()
|
||||||
|
|
||||||
@@ -114,8 +138,34 @@ function getIn(map, keys) {
|
|||||||
return childMap.get(LEAF)
|
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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export default memoize
|
export {
|
||||||
|
memoize as default,
|
||||||
|
__clear,
|
||||||
|
__enable
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user