minor review of hntrie code

This commit is contained in:
Raymond Hill 2018-11-06 13:38:37 -02:00
parent bfb9d50d23
commit 19b7cbca55
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 21 additions and 14 deletions

View File

@ -17,7 +17,7 @@
<script src="https://rawcdn.githack.com/gorhill/uBlock/e83ffde5af29bd44ae529c5a60e2506970e7af34/src/js/hntrie.js"></script> <script src="https://rawcdn.githack.com/gorhill/uBlock/e83ffde5af29bd44ae529c5a60e2506970e7af34/src/js/hntrie.js"></script>
<script src="https://raw.githack.com/gorhill/uBlock/master/src/js/hntrie.js"></script> <script src="https://raw.githack.com/gorhill/uBlock/master/src/js/hntrie.js"></script>
<!-- script src="../../src/js/hntrie.js"></script> --> <!-- <script src="../../src/js/hntrie.js"></script> -->
<script src="hostname-pool.js"></script> <script src="hostname-pool.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script> <script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script>
@ -289,11 +289,11 @@ function initBenchmarks() {
const bms = new Benchmark.Suite(); const bms = new Benchmark.Suite();
const needles = []; const needles = [];
let setDicts = []; let setDicts;
let regexDicts = []; let regexDicts;
let oldTrieDicts = [] let oldTrieDicts;
let newTrieDicts = [] let newTrieDicts;
let results = []; let results;
const lookupDict = function(dicts, fn) { const lookupDict = function(dicts, fn) {
for ( let i = 0; i < needles.length; i++ ) { for ( let i = 0; i < needles.length; i++ ) {
@ -325,6 +325,7 @@ function initBenchmarks() {
regexDicts = []; regexDicts = [];
oldTrieDicts = [] oldTrieDicts = []
newTrieDicts = [] newTrieDicts = []
results = [];
hnTrieManager.reset(); hnTrieManager.reset();
for ( const domainOpt of domainOpts ) { for ( const domainOpt of domainOpts ) {
setDicts.push(setBasedDictCreate(domainOpt)); setDicts.push(setBasedDictCreate(domainOpt));
@ -339,7 +340,11 @@ function initBenchmarks() {
.on('cycle', function(event) { .on('cycle', function(event) {
stdout(gWhich, String(event.target) + '\n'); stdout(gWhich, String(event.target) + '\n');
}) })
.on('complete', exitBenchmark); .on('complete', ( ) => {
setDicts = regexDicts = oldTrieDicts = newTrieDicts = results = undefined;
hnTrieManager.reset();
exitBenchmark();
});
if ( hnTrieManager.matchesWASM !== null ) { if ( hnTrieManager.matchesWASM !== null ) {
bms.add(' - Trie-based WASM', function() { bms.add(' - Trie-based WASM', function() {

View File

@ -10,7 +10,7 @@
<div id="stdout"></div> <div id="stdout"></div>
<script src="https://rawcdn.githack.com/gorhill/uBlock/e83ffde5af29bd44ae529c5a60e2506970e7af34/src/js/hntrie.js"></script> <script src="https://rawcdn.githack.com/gorhill/uBlock/e83ffde5af29bd44ae529c5a60e2506970e7af34/src/js/hntrie.js"></script>
<script src="https://raw.githack.com/gorhill/uBlock/master/src/js/hntrie.js"></script> <script src="https://raw.githack.com/gorhill/uBlock/master/src/js/hntrie.js"></script>
<!-- script src="../../src/js/hntrie.js"></script> --> <!-- <script src="../../src/js/hntrie.js"></script> -->
<script src="hostname-pool.js"></script> <script src="hostname-pool.js"></script>
<script> <script>
const createRandomLabel = function() { const createRandomLabel = function() {

View File

@ -65,7 +65,7 @@ const hnTrieManager = {
reset: function() { reset: function() {
if ( this.wasmMemory === null && this.trie.byteLength > 65536 ) { if ( this.wasmMemory === null && this.trie.byteLength > 65536 ) {
this.trie = new Uint8Array(65536); this.trie = new Uint8Array(65536);
this.trie32 = new Uint32Array(this.trie.buffer); this.trie32 = null;
} else { } else {
this.trie.fill(0); this.trie.fill(0);
} }
@ -88,6 +88,7 @@ const hnTrieManager = {
if ( needle !== this.needle ) { if ( needle !== this.needle ) {
const buf = this.trie; const buf = this.trie;
let i = needle.length; let i = needle.length;
if ( i > 255 ) { i = 255; }
buf[255] = i; buf[255] = i;
while ( i-- ) { while ( i-- ) {
buf[i] = needle.charCodeAt(i); buf[i] = needle.charCodeAt(i);
@ -167,12 +168,13 @@ const hnTrieManager = {
*/ */
add: function(hn) { add: function(hn) {
let ichar = hn.length - 1;
if ( ichar === -1 ) { return; }
// 256 * 3 + 3 = 771 // 256 * 3 + 3 = 771
if ( this.treesz + 771 >= this.tree.length ) { if ( this.treesz + 771 >= this.tree.length ) {
this.growTree(); this.growTree();
} }
let ichar = hn.length - 1; if ( ichar > 254 ) { ichar = 254; }
if ( ichar === -1 ) { return; }
let c = hn.charCodeAt(ichar), let c = hn.charCodeAt(ichar),
i = 0, inext; i = 0, inext;
for (;;) { for (;;) {
@ -313,8 +315,8 @@ const hnTrieManager = {
byte 7: number of extra characters byte 7: number of extra characters
Offset & count values are little-endian. Offset & count values are little-endian.
3 + 3 + 1 + 1 = 8 bytes for one character, otherwise 4 + 4 + 1 + 1 = 10 bytes for one character, otherwise
3 + 3 + 1 + 1 + n = 8 + n bytes for one + n character(s) 4 + 4 + 1 + 1 + n = 10 + n bytes for one + n character(s)
*/ */
finish: function() { finish: function() {
@ -410,7 +412,7 @@ const hnTrieManager = {
} else { } else {
this.tree = null; this.tree = null;
} }
}, 30000); }, 10000);
} }
}, },