mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Perf: refactor trie to avoid iterate twice
This commit is contained in:
@@ -1,27 +1,41 @@
|
||||
import { createTrie } from './trie';
|
||||
import { createTrie, hostnameToTokens } from './trie';
|
||||
import { describe, it } from 'mocha';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('Trie', () => {
|
||||
it('should be possible to add items to a Trie.', () => {
|
||||
const trie = createTrie();
|
||||
describe('hostname to tokens', () => {
|
||||
it('should split hostname into tokens.', () => {
|
||||
expect(hostnameToTokens('.blog.skk.moe')).to.deep.equal([
|
||||
'.',
|
||||
'blog',
|
||||
'.',
|
||||
'skk',
|
||||
'.',
|
||||
'moe'
|
||||
]);
|
||||
|
||||
trie.add('sukka');
|
||||
trie.add('ukka');
|
||||
trie.add('akku');
|
||||
expect(hostnameToTokens('blog.skk.moe')).to.deep.equal([
|
||||
'blog',
|
||||
'.',
|
||||
'skk',
|
||||
'.',
|
||||
'moe'
|
||||
]);
|
||||
|
||||
expect(trie.size).to.equal(3);
|
||||
expect(hostnameToTokens('skk.moe')).to.deep.equal([
|
||||
'skk',
|
||||
'.',
|
||||
'moe'
|
||||
]);
|
||||
|
||||
expect(trie.has('sukka')).to.equal(true);
|
||||
expect(trie.has('ukka')).to.equal(true);
|
||||
expect(trie.has('akku')).to.equal(true);
|
||||
expect(trie.has('noc')).to.equal(false);
|
||||
expect(trie.has('suk')).to.equal(false);
|
||||
expect(trie.has('sukkaw')).to.equal(false);
|
||||
expect(hostnameToTokens('moe')).to.deep.equal([
|
||||
'moe'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be possible to add domains to a Trie (hostname).', () => {
|
||||
const trie = createTrie(null, true);
|
||||
describe('Trie', () => {
|
||||
it('should be possible to add domains to a Trie.', () => {
|
||||
const trie = createTrie();
|
||||
|
||||
trie.add('a.skk.moe');
|
||||
trie.add('skk.moe');
|
||||
@@ -40,17 +54,6 @@ describe('Trie', () => {
|
||||
it('adding the same item several times should not increase size.', () => {
|
||||
const trie = createTrie();
|
||||
|
||||
trie.add('rat');
|
||||
trie.add('erat');
|
||||
trie.add('rat');
|
||||
|
||||
expect(trie.size).to.equal(2);
|
||||
expect(trie.has('rat')).to.equal(true);
|
||||
});
|
||||
|
||||
it('adding the same item several times should not increase size (hostname).', () => {
|
||||
const trie = createTrie(null, true);
|
||||
|
||||
trie.add('skk.moe');
|
||||
trie.add('blog.skk.moe');
|
||||
trie.add('skk.moe');
|
||||
@@ -71,30 +74,7 @@ describe('Trie', () => {
|
||||
});
|
||||
|
||||
it('should be possible to delete items.', () => {
|
||||
const trie = createTrie();
|
||||
|
||||
trie.add('rat');
|
||||
trie.add('rate');
|
||||
trie.add('tar');
|
||||
|
||||
expect(trie.delete('')).to.equal(false);
|
||||
expect(trie.delete('')).to.equal(false);
|
||||
expect(trie.delete('hello')).to.equal(false);
|
||||
|
||||
expect(trie.delete('rat')).to.equal(true);
|
||||
expect(trie.has('rat')).to.equal(false);
|
||||
expect(trie.has('rate')).to.equal(true);
|
||||
|
||||
expect(trie.size).to.equal(2);
|
||||
|
||||
expect(trie.delete('rate')).to.equal(true);
|
||||
expect(trie.size).to.equal(1);
|
||||
expect(trie.delete('tar')).to.equal(true);
|
||||
expect(trie.size).to.equal(0);
|
||||
});
|
||||
|
||||
it('should be possible to delete items (hostname).', () => {
|
||||
const trie = createTrie(null, true);
|
||||
const trie = createTrie(null);
|
||||
|
||||
trie.add('skk.moe');
|
||||
trie.add('example.com');
|
||||
@@ -117,17 +97,6 @@ describe('Trie', () => {
|
||||
});
|
||||
|
||||
it('should be possible to check the existence of a sequence in the Trie.', () => {
|
||||
const trie = createTrie();
|
||||
|
||||
trie.add('romanesque');
|
||||
|
||||
expect(trie.has('romanesque')).to.equal(true);
|
||||
expect(trie.has('roman')).to.equal(false);
|
||||
expect(trie.has('esque')).to.equal(false);
|
||||
expect(trie.has('')).to.equal(false);
|
||||
});
|
||||
|
||||
it('should be possible to check the existence of a sequence in the Trie (hostname).', () => {
|
||||
const trie = createTrie(null, true);
|
||||
|
||||
trie.add('example.org.skk.moe');
|
||||
@@ -139,23 +108,7 @@ describe('Trie', () => {
|
||||
});
|
||||
|
||||
it('should be possible to retrieve items matching the given prefix.', () => {
|
||||
const trie = createTrie();
|
||||
|
||||
trie.add('roman');
|
||||
trie.add('esqueroman');
|
||||
trie.add('sesqueroman');
|
||||
trie.add('greek');
|
||||
|
||||
expect(trie.find('roman')).to.deep.equal(['roman', 'esqueroman', 'sesqueroman']);
|
||||
expect(trie.find('man')).to.deep.equal(['roman', 'esqueroman', 'sesqueroman']);
|
||||
expect(trie.find('esqueroman')).to.deep.equal(['esqueroman', 'sesqueroman']);
|
||||
expect(trie.find('eek')).to.deep.equal(['greek']);
|
||||
expect(trie.find('hello')).to.deep.equal([]);
|
||||
expect(trie.find('')).to.deep.equal(['greek', 'roman', 'esqueroman', 'sesqueroman']);
|
||||
});
|
||||
|
||||
it('should be possible to retrieve items matching the given prefix (hostname).', () => {
|
||||
const trie = createTrie(null, true);
|
||||
const trie = createTrie(null);
|
||||
|
||||
trie.add('example.com');
|
||||
trie.add('blog.example.com');
|
||||
@@ -171,47 +124,42 @@ describe('Trie', () => {
|
||||
});
|
||||
|
||||
it('should be possible to create a trie from an arbitrary iterable.', () => {
|
||||
let trie = createTrie(['roman', 'esqueroman']);
|
||||
let trie = createTrie(['skk.moe', 'blog.skk.moe']);
|
||||
|
||||
expect(trie.size).to.equal(2);
|
||||
expect(trie.has('roman')).to.equal(true);
|
||||
expect(trie.has('skk.moe')).to.equal(true);
|
||||
|
||||
trie = createTrie(new Set(['skk.moe', 'example.com']), true);
|
||||
trie = createTrie(new Set(['skk.moe', 'example.com']));
|
||||
expect(trie.size).to.equal(2);
|
||||
expect(trie.has('skk.moe')).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
([
|
||||
['hostname mode off', false],
|
||||
['hostname mode on', true]
|
||||
] as const).forEach(([description, hostnameMode]) => {
|
||||
describe('surge domainset dedupe ' + description, () => {
|
||||
it('should not remove same entry', () => {
|
||||
const trie = createTrie(['.skk.moe', 'noc.one'], hostnameMode);
|
||||
describe('surge domainset dedupe', () => {
|
||||
it('should not remove same entry', () => {
|
||||
const trie = createTrie(['.skk.moe', 'noc.one']);
|
||||
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe']);
|
||||
expect(trie.find('noc.one')).to.deep.equal(['noc.one']);
|
||||
});
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe']);
|
||||
expect(trie.find('noc.one')).to.deep.equal(['noc.one']);
|
||||
});
|
||||
|
||||
it('should match subdomain - 1', () => {
|
||||
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
|
||||
it('should match subdomain - 1', () => {
|
||||
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net']);
|
||||
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['image.cdn.skk.moe', 'blog.skk.moe']);
|
||||
expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
|
||||
});
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['image.cdn.skk.moe', 'blog.skk.moe']);
|
||||
expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
|
||||
});
|
||||
|
||||
it('should match subdomain - 2', () => {
|
||||
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
|
||||
it('should match subdomain - 2', () => {
|
||||
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net']);
|
||||
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
|
||||
expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
|
||||
});
|
||||
expect(trie.find('.skk.moe')).to.deep.equal(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
|
||||
expect(trie.find('.sukkaw.com')).to.deep.equal(['www.sukkaw.com']);
|
||||
});
|
||||
|
||||
it('should not remove non-subdomain', () => {
|
||||
const trie = createTrie(['skk.moe', 'sukkaskk.moe'], hostnameMode);
|
||||
expect(trie.find('.skk.moe')).to.deep.equal([]);
|
||||
});
|
||||
it('should not remove non-subdomain', () => {
|
||||
const trie = createTrie(['skk.moe', 'sukkaskk.moe']);
|
||||
expect(trie.find('.skk.moe')).to.deep.equal([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -221,7 +169,7 @@ describe('smol tree', () => {
|
||||
'.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe',
|
||||
'www.noc.one', 'cdn.noc.one',
|
||||
'.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'.sub.example.com',
|
||||
@@ -233,7 +181,7 @@ describe('smol tree', () => {
|
||||
it('should create simple tree - 2', () => {
|
||||
const trie = createTrie([
|
||||
'.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'.skk.moe'
|
||||
@@ -243,7 +191,7 @@ describe('smol tree', () => {
|
||||
it('should create simple tree - 2', () => {
|
||||
const trie = createTrie([
|
||||
'.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'.sub.example.com'
|
||||
@@ -261,7 +209,7 @@ describe('smol tree', () => {
|
||||
'act.commercial.shouji.360.cn',
|
||||
'cdn.creative.medialytics.com',
|
||||
'px.cdn.creative.medialytics.com'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'cdn.creative.medialytics.com',
|
||||
@@ -277,7 +225,7 @@ describe('smol tree', () => {
|
||||
'anotherskk.moe',
|
||||
'blog.anotherskk.moe',
|
||||
'blog.skk.moe'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'anotherskk.moe',
|
||||
@@ -293,7 +241,7 @@ describe('smol tree', () => {
|
||||
'anotherskk.moe',
|
||||
'blog.anotherskk.moe',
|
||||
'blog.skk.moe'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'anotherskk.moe',
|
||||
@@ -328,7 +276,7 @@ describe('smol tree', () => {
|
||||
'.skk.moe',
|
||||
'blog.cdn.example.com',
|
||||
'cdn.example.com'
|
||||
], true, true);
|
||||
], true);
|
||||
|
||||
expect(trie.dump()).to.deep.equal([
|
||||
'cdn.example.com', 'blog.cdn.example.com',
|
||||
|
||||
Reference in New Issue
Block a user