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

View File

@ -10,7 +10,7 @@
<div id="stdout"></div>
<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="../../src/js/hntrie.js"></script> -->
<!-- <script src="../../src/js/hntrie.js"></script> -->
<script src="hostname-pool.js"></script>
<script>
const createRandomLabel = function() {

View File

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