Skip to content

Commit 579e251

Browse files
authored
feat!: compare component codes instead of strings (#60)
Switches matcher implementation to use multiaddr components and compare the codes rather than encoding/decoding strings which leads to a 2x speed up.
1 parent 35bbd50 commit 579e251

File tree

9 files changed

+214
-141
lines changed

9 files changed

+214
-141
lines changed

benchmarks/operations/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# multiaddr Benchmark
2+
3+
Benchmarks multiaddr performance during common operations - parsing strings,
4+
encapsulating/decapsulating addresses, turning to bytes, decoding bytes, etc.
5+
6+
## Running the benchmarks
7+
8+
```console
9+
% npm start
10+
11+
12+
> npm run build && node dist/src/index.js
13+
14+
15+
16+
> aegir build --bundle false
17+
18+
[06:10:56] tsc [started]
19+
[06:10:56] tsc [completed]
20+
┌─────────┬─────────────────────────────────────────┬──────────────┬────────┬───────┬────────┐
21+
│ (index) │ Implementation │ ops/s │ ms/op │ runs │ p99 │
22+
├─────────┼─────────────────────────────────────────┼──────────────┼────────┼───────┼────────┤
23+
│ 0 │ 'head' │ '2427997.80' │ '0.00' │ 50000 │ '0.00' │
24+
│ 1 │ '@multiformats/multiaddr-matcher-1.7.2' │ '1098132.24' │ '0.00' │ 50000 │ '0.00' │
25+
└─────────┴─────────────────────────────────────────┴──────────────┴────────┴───────┴────────┘
26+
```

benchmarks/operations/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "benchmarks-operations",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"private": true,
6+
"type": "module",
7+
"scripts": {
8+
"clean": "aegir clean",
9+
"build": "aegir build --bundle false",
10+
"lint": "aegir lint",
11+
"dep-check": "aegir dep-check",
12+
"doc-check": "aegir doc-check",
13+
"start": "npm run build && node dist/test/index.js"
14+
},
15+
"devDependencies": {
16+
"@multiformats/multiaddr-matcher-1.7.2": "npm:@multiformats/[email protected]",
17+
"@multiformats/multiaddr-matcher": "../../",
18+
"aegir": "^47.0.7",
19+
"tinybench": "^4.0.1"
20+
}
21+
}

benchmarks/operations/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

benchmarks/operations/test/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable no-console */
2+
3+
import { multiaddr } from '@multiformats/multiaddr'
4+
import { TCP } from '@multiformats/multiaddr-matcher'
5+
import { TCP as TCP_172 } from '@multiformats/multiaddr-matcher-1.7.2'
6+
import { Bench } from 'tinybench'
7+
8+
const ITERATIONS = parseInt(process.env.ITERATIONS ?? '50000')
9+
const MIN_TIME = parseInt(process.env.MIN_TIME ?? '1')
10+
const RESULT_PRECISION = 2
11+
12+
function bench (m: typeof TCP | typeof TCP_172): void {
13+
const ma = multiaddr('/ip4/127.0.0.1/tcp/1234')
14+
15+
m.exactMatch(ma)
16+
}
17+
18+
async function main (): Promise<void> {
19+
const suite = new Bench({
20+
iterations: ITERATIONS,
21+
time: MIN_TIME
22+
})
23+
suite.add('head', () => {
24+
bench(TCP)
25+
})
26+
suite.add('@multiformats/multiaddr-matcher-1.7.2', () => {
27+
bench(TCP_172)
28+
})
29+
30+
await suite.run()
31+
32+
console.table(suite.tasks.map(({ name, result }) => {
33+
if (result?.error != null) {
34+
console.error(result.error)
35+
36+
return {
37+
Implementation: name,
38+
'ops/s': 'error',
39+
'ms/op': 'error',
40+
runs: 'error',
41+
p99: 'error'
42+
}
43+
}
44+
45+
return {
46+
Implementation: name,
47+
'ops/s': result?.hz.toFixed(RESULT_PRECISION),
48+
'ms/op': result?.period.toFixed(RESULT_PRECISION),
49+
runs: result?.samples.length,
50+
p99: result?.p99.toFixed(RESULT_PRECISION)
51+
}
52+
}))
53+
}
54+
55+
main().catch(err => {
56+
console.error(err)
57+
process.exit(1)
58+
})

benchmarks/operations/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
],
10+
"references": [
11+
{
12+
"path": "../../"
13+
}
14+
]
15+
}

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@
162162
"docs": "aegir docs"
163163
},
164164
"dependencies": {
165-
"@chainsafe/is-ip": "^2.0.1",
166-
"@multiformats/multiaddr": "^12.0.0",
167-
"multiformats": "^13.0.0"
165+
"@multiformats/multiaddr": "^12.0.0"
168166
},
169167
"devDependencies": {
170168
"aegir": "^47.0.19"

0 commit comments

Comments
 (0)