Skip to content

address bad usage of TST #4048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions benchmarks/core/parse-headers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ const headersIrregular = Array.from(
.map((c) => Buffer.from(c.toUpperCase()))
)

// wellknownHeaderName[wellknownHeaderNames.length-1] is the worst case
// but Via is more pathological, we need around 20 times to reach Via,
// if we insert wellknownHeaderNames to the TST in order while it is only
// 3 characters long
const worstCaseIfInsertInOrderToTST = Object.entries({
Via: '1.1 vegur'
})
.flat()
.map((c) => Buffer.from(c))

// avoid JIT bias
bench('noop', () => {})
bench('noop', () => {})
Expand All @@ -98,6 +108,11 @@ group('parseHeaders', () => {
parseHeaders(headersIrregular[i])
}
})
bench('worstCaseIfInsertInOrderToTST', () => {
for (let i = 0; i < 1e4; i++) {
parseHeaders(worstCaseIfInsertInOrderToTST)
}
})
})

await new Promise((resolve) => setTimeout(resolve, 7000))
Expand Down
14 changes: 12 additions & 2 deletions lib/core/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,18 @@ class TernarySearchTree {

const tree = new TernarySearchTree()

for (let i = 0; i < wellknownHeaderNames.length; ++i) {
const key = headerNameLowerCasedRecord[wellknownHeaderNames[i]]
function shuffleArray (arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]]
}
return arr
}
const randomIndexOfWellknownHeaderNames =
shuffleArray(wellknownHeaderNames.map((v, idx) => idx))

for (const randomIndex of randomIndexOfWellknownHeaderNames) {
const key = headerNameLowerCasedRecord[wellknownHeaderNames[randomIndex]]
tree.insert(key, key)
}

Expand Down
Loading