Skip to content

Commit

Permalink
Cache and reuse result of HNTrieRef.matches() when possible
Browse files Browse the repository at this point in the history
Due to how web pages typically load secondary resources and due
to how HNTrieContainer instances are used in uBO, there is a
great likelihood that the result of a previous call to
HNTrieRef.matches() can be reused in a subsequent call.
This has been confirmed by instrumenting HNTrieRef.matches().

Since uBO uses distinct HNTrieContainer instances to either
match against the request or the origin hostnames, this
means a high likelihood of repeated calls to HNTrieRef.matches()
with the same hostname as argument, hence a performance gain
when caching the argument+result -- as despite that
HNTrie.matches() is fast, comparing two short strings is even
faster if this allows to skip HNTrie.matches() altogether.
  • Loading branch information
gorhill committed Apr 25, 2019
1 parent 9939039 commit 155abfb
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/js/hntrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ HNTrieContainer.prototype = {
this.container = container;
this.iroot = iroot;
this.size = size;
this.last = -1;
this.needle = '';
},

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -548,33 +550,51 @@ HNTrieContainer.prototype = {
HNTrieContainer.prototype.HNTrieRef.prototype = {
add: function(hn) {
if ( this.container.setNeedle(hn).add(this.iroot) === 1 ) {
this.last = -1;
this.needle = '';
this.size += 1;
return true;
}
return false;
},
addJS: function(hn) {
if ( this.container.setNeedle(hn).addJS(this.iroot) === 1 ) {
this.last = -1;
this.needle = '';
this.size += 1;
return true;
}
return false;
},
addWASM: function(hn) {
if ( this.container.setNeedle(hn).addWASM(this.iroot) === 1 ) {
this.last = -1;
this.needle = '';
this.size += 1;
return true;
}
return false;
},
matches: function(needle) {
return this.container.setNeedle(needle).matches(this.iroot);
if ( needle !== this.needle ) {
this.needle = needle;
this.last = this.container.setNeedle(needle).matches(this.iroot);
}
return this.last;
},
matchesJS: function(needle) {
return this.container.setNeedle(needle).matchesJS(this.iroot);
if ( needle !== this.needle ) {
this.needle = needle;
this.last = this.container.setNeedle(needle).matchesJS(this.iroot);
}
return this.last;
},
matchesWASM: function(needle) {
return this.container.setNeedle(needle).matchesWASM(this.iroot);
if ( needle !== this.needle ) {
this.needle = needle;
this.last = this.container.setNeedle(needle).matchesWASM(this.iroot);
}
return this.last;
},
[Symbol.iterator]: function() {
return {
Expand Down

0 comments on commit 155abfb

Please sign in to comment.