Skip to content

fix: skip validation when encapsulating/decapsulating #420

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 14 commits into
base: main
Choose a base branch
from
Open
26 changes: 26 additions & 0 deletions benchmarks/operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# multiaddr Benchmark

Benchmarks multiaddr performance during common operations - parsing strings,
encapsulating/decapsulating addresses, turning to bytes, decoding bytes, etc.

## Running the benchmarks

```console
% npm start

> [email protected] start
> npm run build && node dist/src/index.js


> [email protected] build
> aegir build --bundle false

[06:10:56] tsc [started]
[06:10:56] tsc [completed]
┌─────────┬──────────────────────────────────┬─────────────┬────────┬───────┬────────┐
│ (index) │ Implementation │ ops/s │ ms/op │ runs │ p99 │
├─────────┼──────────────────────────────────┼─────────────┼────────┼───────┼────────┤
│ 0 │ 'head' │ '105679.89' │ '0.01' │ 50000 │ '0.01' │
│ 1 │ '@multiformats/[email protected]' │ '18244.31' │ '0.06' │ 50000 │ '0.13' │
└─────────┴──────────────────────────────────┴─────────────┴────────┴───────┴────────┘
```
21 changes: 21 additions & 0 deletions benchmarks/operations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "benchmarks-operations",
"version": "1.0.0",
"main": "index.js",
"private": true,
"type": "module",
"scripts": {
"clean": "aegir clean",
"build": "aegir build --bundle false",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"doc-check": "aegir doc-check",
"start": "npm run build && node dist/test/index.js"
},
"devDependencies": {
"@multiformats/multiaddr-12.4.0": "npm:@multiformats/[email protected]",
"@multiformats/multiaddr": "../../",
"aegir": "^47.0.7",
"tinybench": "^4.0.1"
}
}
1 change: 1 addition & 0 deletions benchmarks/operations/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
68 changes: 68 additions & 0 deletions benchmarks/operations/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable no-console */

import { multiaddr } from '@multiformats/multiaddr'
import { multiaddr as multiaddr12 } from '@multiformats/multiaddr-12.4.0'
import { Bench } from 'tinybench'

const ITERATIONS = parseInt(process.env.ITERATIONS ?? '50000')
const MIN_TIME = parseInt(process.env.MIN_TIME ?? '1')
const RESULT_PRECISION = 2

function bench (m: typeof multiaddr | typeof multiaddr12): void {
const ma = m('/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiAkH5a4DPGKUuOBjYw0CgwjvcJCJMD2K_1aluKR_tpevQ/certhash/uEiAfbgiymPP2_nX7Dgir8B4QkksjHp2lVuJZz0F79Be9JA')
ma.encapsulate('/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
ma.decapsulate('/quic-v1')

const ma2 = m(ma.bytes)
ma2.encapsulate('/tls/sni/example.com/http/http-path/path%2Findex.html')
ma2.equals(ma)

ma2.getPath()
ma2.getPeerId()

const ma3 = m('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/1234/quic-v1/webtransport/certhash/uEiAkH5a4DPGKUuOBjYw0CgwjvcJCJMD2K_1aluKR_tpevQ/certhash/uEiAfbgiymPP2_nX7Dgir8B4QkksjHp2lVuJZz0F79Be9JA')
ma3.encapsulate('/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
ma3.decapsulate('/quic-v1')
}
Comment on lines +11 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this do any validation on the newer version? if not, it might be good to get a benchmark of the performance with the new validation to ensure there isn't a significant hit there..

Also, can we provide some recommendations for folks as to when & how to validate multiaddrs? i.e. only when reading multiaddr as a string, and then we don't need to again if using ma.* functions.. or whatever the case may be.


async function main (): Promise<void> {
const suite = new Bench({
iterations: ITERATIONS,
time: MIN_TIME
})
suite.add('head', () => {
bench(multiaddr)
})
suite.add('@multiformats/[email protected]', () => {
bench(multiaddr12)
})

await suite.run()

console.table(suite.tasks.map(({ name, result }) => {
if (result?.error != null) {
console.error(result.error)

return {
Implementation: name,
'ops/s': 'error',
'ms/op': 'error',
runs: 'error',
p99: 'error'
}
}

return {
Implementation: name,
'ops/s': result?.hz.toFixed(RESULT_PRECISION),
'ms/op': result?.period.toFixed(RESULT_PRECISION),
runs: result?.samples.length,
p99: result?.p99.toFixed(RESULT_PRECISION)
}
}))
}

main().catch(err => {
console.error(err)
process.exit(1)
})
15 changes: 15 additions & 0 deletions benchmarks/operations/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src",
"test"
],
"references": [
{
"path": "../../"
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@
"@chainsafe/is-ip": "^2.0.1",
"@chainsafe/netmask": "^2.0.0",
"@multiformats/dns": "^1.0.3",
"abort-error": "^1.0.1",
"multiformats": "^13.0.0",
"uint8-varint": "^2.0.1",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@types/sinon": "^17.0.2",
"aegir": "^47.0.12",
"aegir": "^47.0.16",
"sinon": "^19.0.2",
"sinon-ts": "^2.0.0"
},
Expand Down
236 changes: 0 additions & 236 deletions src/codec.ts

This file was deleted.

Loading