Skip to content

Commit

Permalink
Reworked source content cache strategy to gain search performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Minetti committed May 19, 2015
1 parent 74036dd commit 98e5453
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 40 deletions.
14 changes: 4 additions & 10 deletions lib/DebuggerAgent.js
Expand Up @@ -210,7 +210,6 @@ DebuggerAgent.prototype = {
Number(params.scriptId),
function(err, source) {
if (err) return done(err);
self._searchScriptSourceCache[params.scriptId] = source;
return done(null, { scriptSource: source });
}
);
Expand Down Expand Up @@ -500,7 +499,6 @@ DebuggerAgent.prototype = {
_searchScriptSourceCache: {},

searchInContent: function(params, done) {
var self = this;

function initSearch(content) {
if (content) {
Expand All @@ -510,14 +508,10 @@ DebuggerAgent.prototype = {
}
}

if(self._searchScriptSourceCache.hasOwnProperty(params.scriptId)) {
initSearch(self._searchScriptSourceCache[params.scriptId]);
} else {
self.getScriptSource({scriptId: params.scriptId}, function (err, data) {
if (err) return done(null, {result: []});
initSearch(data.scriptSource);
});
}
this._scriptManager.getScriptSourceByIdFromCache(Number(params.scriptId), function (err, data) {
if (err) return done(null, {result: []});
initSearch(data.scriptSource);
});
}
};

Expand Down
15 changes: 5 additions & 10 deletions lib/PageAgent.js
Expand Up @@ -233,7 +233,6 @@ extend(PageAgent.prototype, {
_searchResourceContentCache: {},

searchInResource: function(params, done){
var self = this;

function initSearch(content){
if (content) {
Expand All @@ -242,15 +241,11 @@ extend(PageAgent.prototype, {
done(null,{ result: [] });
}
}

if(self._searchResourceContentCache.hasOwnProperty(params.url)) {
initSearch(self._searchResourceContentCache[params.url]);
} else {
self.getResourceContent({ url: params.url }, function(err,data){
if (err) return done(null,{ result: [] });
initSearch(data.content);
});
}

this.getResourceContent({ url: params.url }, function(err,data){
if (err) return done(null,{ result: [] });
initSearch(data.content);
});
}
});

Expand Down
37 changes: 21 additions & 16 deletions lib/ScriptFileStorage.js
Expand Up @@ -58,23 +58,28 @@ $class.save = function(path, content, callback) {
* @param {function(Object, string)} callback
*/
$class.load = function(path, callback) {
var scriptId = this._scriptManager.findScriptIdByPath(path);
if (scriptId != null) {
this._scriptManager.getScriptSourceById(scriptId, callback);
var cache = this._scriptManager._scriptSourceCache;
if (cache[path]) {
return callback(null, cache[path]);
} else {
fs.readFile(
path,
'utf-8',
function(err, content) {
if (err) return callback(err);

// remove shebang
content = content.replace(/^\#\!.*/, '');

var source = MODULE_HEADER + content + MODULE_TRAILER;
return callback(null, source);
}
);
var scriptId = this._scriptManager.findScriptIdByPath(path);
if (scriptId != null) {
this._scriptManager.getScriptSourceByIdFromCache(scriptId, callback);
} else {
fs.readFile(
path,
'utf-8',
function(err, content) {
if (err) return callback(err);
// remove shebang
content = content.replace(/^\#\!.*/, '');
var source = MODULE_HEADER + content + MODULE_TRAILER;

cache[path] = source;
return callback(null, source);
}
);
}
}
};

Expand Down
26 changes: 23 additions & 3 deletions lib/ScriptManager.js
Expand Up @@ -96,7 +96,22 @@ ScriptManager.prototype = Object.create(events.EventEmitter.prototype, {
}
}
},

_scriptSourceCache: {
value: {}
},

getScriptSourceByIdFromCache: {
value: function(id, callback) {
var cache = this._scriptSourceCache;
if (cache[id]) {
callback(null,cache[id]);
} else {
this.getScriptSourceById(id,callback);
}
}
},

getScriptSourceById: {
value: function(id, callback) {
this._debuggerClient.request(
Expand All @@ -106,16 +121,18 @@ ScriptManager.prototype = Object.create(events.EventEmitter.prototype, {
types: 4,
ids: [id]
},
function handleScriptSourceResponse(err, result) {
(function handleScriptSourceResponse(cache, err, result) {
if (err) return callback(err);

// Some modules gets unloaded (?) after they are parsed,
// e.g. node_modules/express/node_modules/methods/index.js
// V8 request 'scripts' returns an empty result in such case
var source = result.length > 0 ? result[0].source : undefined;

if(source) cache[id] = source;

callback(null, source);
}
}).bind(null,this._scriptSourceCache)
);
}
},
Expand All @@ -127,7 +144,7 @@ ScriptManager.prototype = Object.create(events.EventEmitter.prototype, {
this._debuggerClient.request(
'scripts',
{
includeSource: false,
includeSource: true,
filter: id
},
function(error, scripts) {
Expand Down Expand Up @@ -173,6 +190,9 @@ ScriptManager.prototype = Object.create(events.EventEmitter.prototype, {

debug('addScript id: %s localPath: %s hidden? %s source? %s',
v8data.id, localPath, hidden, !!v8data.source);

var cache = this._scriptSourceCache;
cache[v8data.id] = v8data.source;

if (hidden) return done(null, inspectorScriptData);

Expand Down
4 changes: 3 additions & 1 deletion lib/search.js
Expand Up @@ -156,4 +156,6 @@ exports.performSearchInContent = function(content, query, caseSensitive, isRegex
}
}
return result;
}
}

exports.performSearchInContent = exports._performSearchInContent;

0 comments on commit 98e5453

Please sign in to comment.