Skip to content

Commit

Permalink
Use memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Oct 19, 2015
1 parent 6a0447e commit 34f99e5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
_docpress
cache
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const ware = require('ware')
const assign = Object.assign

const useCache = require('./lib/helpers/use_cache')
const buildJs = require('./lib/build_js')
const memoize = require('./lib/memoize')

module.exports = function search (options) {
const ctx = {}
Expand All @@ -23,23 +25,32 @@ module.exports = function search (options) {
*/

function updateIndex (files, ms, done) {
let idx = {}
const idx = {}

Object.keys(files).forEach((fname) => {
const file = files[fname]
if (file.$) index(fname, file, idx)
// memoize based on file contents
if (file.$) {
const cacheKey = [ ms.directory(), fname, file.contents ]
const newIndices = memoize(['index', cacheKey], () => {
return index(fname, file)
})
assign(idx, newIndices)
}
})

extendIndex(files['_docpress.json'], (file) => {
file.searchIndex = idx
file.lunrIndex = JSON.stringify(lunrize(idx))
file.lunrIndex = memoize([ 'lunr', ms.directory(), idx ], () => {
return JSON.stringify(lunrize(idx))
})
})

done()
}

/**
* Internal: adds the search.js file
Internal: adds the search.js file
*/

function addJs (files, ms, done) {
Expand Down Expand Up @@ -104,9 +115,10 @@ function extendIndex (file, block) {
* Internal: adds new indexes to `idx` from the file `file`/`fname`.
*/

function index (fname, file, idx) {
function index (fname, file) {
let slug = fname
let title = file.title
let idx = {}

file.$(':root').each(function () {
const $this = file.$(this)
Expand All @@ -127,6 +139,8 @@ function index (fname, file, idx) {
}
})

return idx

function initial () {
return {
title: title,
Expand Down
36 changes: 36 additions & 0 deletions lib/memoize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

let cache = {}
let hits = []

function memoize (keyObject, fn) {
const key = JSON.stringify(keyObject)
if (cache[key]) return cache[key]
cache[key] = fn()
hits.push(key)
return cache[key]
}

// Ensure that the last X keys are always available
memoize.keepKeys = 20

// Truncate every now and then
memoize.truncateInterval = 3 * 60 * 1000

// truncate the cache to the last N keys used
memoize.prune = function prune () {
const preserved = hits.slice(hits.length - memoize.keepKeys)
const newCache = {}

preserved.forEach((key) => {
newCache[key] = cache[key]
})

hits = preserved
cache = newCache
}

// truncate every few minutes
memoize.timer = setInterval(memoize.prune, memoize.truncateInterval)

module.exports = memoize

0 comments on commit 34f99e5

Please sign in to comment.