Skip to content

Commit 0c3c779

Browse files
authored
Merge pull request #528 from napi-rs/xxh3
perf(xxhash): avoid copy input content
2 parents 5c2c322 + 01ef88d commit 0c3c779

File tree

5 files changed

+96
-17
lines changed

5 files changed

+96
-17
lines changed

packages/xxhash/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,23 @@ Memory: 32688MiB
9696
### Result
9797

9898
```
99-
@node-rs/xxhash h32 x 4,663 ops/sec ±6.22% (81 runs sampled)
100-
xxhashjs h32 x 1,880 ops/sec ±7.11% (75 runs sampled)
99+
@node-rs/xxhash h32 x 18,847 ops/sec ±3.81% (81 runs sampled)
100+
xxhash c++ x 12,190 ops/sec ±2.94% (83 runs sampled)
101+
xxhashjs h32 x 1,035 ops/sec ±11.04% (68 runs sampled)
101102
xxh32 bench suite: Fastest is @node-rs/xxhash h32
102103
103-
@node-rs/xxhash h32 x 13,452 ops/sec ±2.73% (80 runs sampled)
104-
xxhashjs h32 x 2,496 ops/sec ±0.39% (97 runs sampled)
104+
@node-rs/xxhash h32 x 13,248 ops/sec ±4.38% (78 runs sampled)
105+
xxhashjs h32 x 1,366 ops/sec ±1.96% (85 runs sampled)
105106
xxh32 multi steps bench suite: Fastest is @node-rs/xxhash h32
106107
107-
@node-rs/xxhash 64 x 15,806 ops/sec ±3.14% (79 runs sampled)
108-
xxhashjs h64 x 69.11 ops/sec ±5.99% (60 runs sampled)
108+
@node-rs/xxhash 64 x 43,532 ops/sec ±1.33% (88 runs sampled)
109+
xxhash C++ x 41,658 ops/sec ±1.45% (90 runs sampled)
110+
wasm x 32,415 ops/sec ±1.38% (90 runs sampled)
111+
xxhashjs h64 x 47.52 ops/sec ±3.20% (62 runs sampled)
109112
xxh64 bench suite: Fastest is @node-rs/xxhash 64
110113
111-
@node-rs/xxhash 64 x 13,841 ops/sec ±3.17% (82 runs sampled)
112-
xxhashjs h64 x 79.71 ops/sec ±4.34% (70 runs sampled)
114+
@node-rs/xxhash 64 x 33,153 ops/sec ±5.42% (76 runs sampled)
115+
wasm x 29,477 ops/sec ±2.72% (81 runs sampled)
116+
xxhashjs h64 x 54.96 ops/sec ±1.93% (71 runs sampled)
113117
xxh64 multi steps bench suite: Fastest is @node-rs/xxhash 64
114118
```

packages/xxhash/benchmark/xxhash.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ const { join } = require('path')
33

44
const { Suite } = require('benchmark')
55
const chalk = require('chalk')
6+
const createWasmHasher = require('webpack/lib/util/hash/xxhash64')
7+
const { hash64, hash } = require('xxhash')
68
const { h32: h32js, h64: h64js } = require('xxhashjs')
79

810
const { xxh32, xxh64, Xxh32, Xxh64 } = require('../index')
911

1012
const FX = readFileSync(join(__dirname, '..', '..', '..', 'yarn.lock'))
1113

14+
const wasmHasher = createWasmHasher()
15+
1216
new Suite('xxh32')
1317
.add('@node-rs/xxhash h32', () => {
14-
xxh32(FX)
18+
xxh32(FX, 0)
19+
})
20+
.add('xxhash c++', () => {
21+
hash(FX, 0)
1522
})
1623
.add('xxhashjs h32', () => {
1724
h32js(FX, 0).toNumber()
@@ -43,6 +50,13 @@ new Suite('xxh64')
4350
.add('@node-rs/xxhash 64', () => {
4451
xxh64(FX).toString(16)
4552
})
53+
.add('xxhash C++', () => {
54+
hash64(FX, 0)
55+
})
56+
.add('wasm', () => {
57+
wasmHasher.update(FX).digest()
58+
wasmHasher.reset()
59+
})
4660
.add('xxhashjs h64', () => {
4761
h64js(FX, 0).toString(16)
4862
})
@@ -58,6 +72,10 @@ new Suite('xxh64 multi steps')
5872
.add('@node-rs/xxhash 64', () => {
5973
new Xxh64().update(FX).digest().toString(16)
6074
})
75+
.add('wasm', () => {
76+
wasmHasher.update(FX).digest()
77+
wasmHasher.reset()
78+
})
6179
.add('xxhashjs h64', () => {
6280
h64js(0).update(FX).digest().toString(16)
6381
})

packages/xxhash/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@ Xxh3.prototype.reset = xxh3.reset
3131

3232
module.exports = {
3333
xxh32: function xxh32(input, seed) {
34-
return _xxh32(Buffer.from(input), seed == null ? 0 : seed)
34+
return _xxh32(Buffer.isBuffer(input) ? input : Buffer.from(input), seed == null ? 0 : seed)
3535
},
3636
xxh64: function xxh64(input, seed) {
37-
return _xxh64(Buffer.from(input), seed == null ? BigInt(0) : seed)
37+
return _xxh64(Buffer.isBuffer(input) ? input : Buffer.from(input), seed == null ? BigInt(0) : seed)
3838
},
3939
Xxh32: class Xxh32 extends _Xxh32 {
4040
update(input) {
41-
return super.update(Buffer.from(input))
41+
return super.update(Buffer.isBuffer(input) ? input : Buffer.from(input))
4242
}
4343
},
4444
Xxh64: class Xxh64 extends _Xxh64 {
4545
update(input) {
46-
return super.update(Buffer.from(input))
46+
return super.update(Buffer.isBuffer(input) ? input : Buffer.from(input))
4747
}
4848
},
4949
xxh3: {
5050
xxh64: function xxh64(input, seed) {
51-
return xxh3.xxh64(Buffer.from(input), seed == null ? BigInt(0) : seed)
51+
return xxh3.xxh64(Buffer.isBuffer(input) ? input : Buffer.from(input), seed == null ? BigInt(0) : seed)
5252
},
5353
xxh64WithSecret(input, secret) {
54-
return xxh3.xxh64WithSecret(Buffer.from(input), Buffer.from(secret))
54+
return xxh3.xxh64WithSecret(Buffer.isBuffer(input) ? input : Buffer.from(input), Buffer.from(secret))
5555
},
5656
xxh128: function xxh128(input, seed) {
57-
return xxh3.xxh128(Buffer.from(input), seed == null ? BigInt(0) : seed)
57+
return xxh3.xxh128(Buffer.isBuffer(input) ? input : Buffer.from(input), seed == null ? BigInt(0) : seed)
5858
},
5959
xxh128WithSecret(input, secret) {
60-
return xxh3.xxh128WithSecret(Buffer.from(input), Buffer.from(secret))
60+
return xxh3.xxh128WithSecret(Buffer.isBuffer(input) ? input : Buffer.from(input), Buffer.from(secret))
6161
},
6262
Xxh3,
6363
},

packages/xxhash/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
},
5454
"devDependencies": {
5555
"@types/xxhashjs": "^0.2.2",
56+
"webpack": "^5.59.1",
57+
"xxhash": "^0.3.0",
5658
"xxhashjs": "^0.2.2"
5759
},
5860
"funding": {

yarn.lock

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,14 @@ enhanced-resolve@^5.8.0:
29082908
graceful-fs "^4.2.4"
29092909
tapable "^2.2.0"
29102910

2911+
enhanced-resolve@^5.8.3:
2912+
version "5.8.3"
2913+
resolved "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.8.3.tgz#6d552d465cce0423f5b3d718511ea53826a7b2f0"
2914+
integrity sha512-EGAbGvH7j7Xt2nc0E7D99La1OiEs8LnyimkRgwExpUMScN6O+3x9tIWs7PLQZVNx4YD+00skHXPXi1yQHpAmZA==
2915+
dependencies:
2916+
graceful-fs "^4.2.4"
2917+
tapable "^2.2.0"
2918+
29112919
enquirer@^2.3.5, enquirer@^2.3.6:
29122920
version "2.3.6"
29132921
resolved "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"
@@ -2997,6 +3005,11 @@ es-module-lexer@^0.7.1:
29973005
resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.7.1.tgz#c2c8e0f46f2df06274cdaf0dd3f3b33e0a0b267d"
29983006
integrity sha512-MgtWFl5No+4S3TmhDmCz2ObFGm6lEpTnzbQi+Dd+pw4mlTIZTmM2iAs5gRlmx5zS9luzobCSBSI90JM/1/JgOw==
29993007

3008+
es-module-lexer@^0.9.0:
3009+
version "0.9.3"
3010+
resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.9.3.tgz#6f13db00cc38417137daf74366f535c8eb438f19"
3011+
integrity sha512-1HQ2M2sPtxwnvOvT1ZClHyQDiggdNjURWpY2we6aMKCQiUVxTmVs2UYPLIrD84sS+kMdUwfBSylbJPwNnBrnHQ==
3012+
30003013
es-to-primitive@^1.2.1:
30013014
version "1.2.1"
30023015
resolved "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a"
@@ -5022,6 +5035,11 @@ [email protected], mute-stream@~0.0.4:
50225035
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
50235036
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==
50245037

5038+
nan@^2.13.2:
5039+
version "2.15.0"
5040+
resolved "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee"
5041+
integrity sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==
5042+
50255043
natural-compare@^1.4.0:
50265044
version "1.4.0"
50275045
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -7279,6 +7297,36 @@ webpack@^5:
72797297
watchpack "^2.2.0"
72807298
webpack-sources "^3.2.0"
72817299

7300+
webpack@^5.59.1:
7301+
version "5.59.1"
7302+
resolved "https://registry.npmjs.org/webpack/-/webpack-5.59.1.tgz#60c77e9aad796252153d4d7ab6b2d4c11f0e548c"
7303+
integrity sha512-I01IQV9K96FlpXX3V0L4nvd7gb0r7thfuu1IfT2P4uOHOA77nKARAKDYGe/tScSHKnffNIyQhLC8kRXzY4KEHQ==
7304+
dependencies:
7305+
"@types/eslint-scope" "^3.7.0"
7306+
"@types/estree" "^0.0.50"
7307+
"@webassemblyjs/ast" "1.11.1"
7308+
"@webassemblyjs/wasm-edit" "1.11.1"
7309+
"@webassemblyjs/wasm-parser" "1.11.1"
7310+
acorn "^8.4.1"
7311+
acorn-import-assertions "^1.7.6"
7312+
browserslist "^4.14.5"
7313+
chrome-trace-event "^1.0.2"
7314+
enhanced-resolve "^5.8.3"
7315+
es-module-lexer "^0.9.0"
7316+
eslint-scope "5.1.1"
7317+
events "^3.2.0"
7318+
glob-to-regexp "^0.4.1"
7319+
graceful-fs "^4.2.4"
7320+
json-parse-better-errors "^1.0.2"
7321+
loader-runner "^4.2.0"
7322+
mime-types "^2.1.27"
7323+
neo-async "^2.6.2"
7324+
schema-utils "^3.1.0"
7325+
tapable "^2.1.1"
7326+
terser-webpack-plugin "^5.1.3"
7327+
watchpack "^2.2.0"
7328+
webpack-sources "^3.2.0"
7329+
72827330
well-known-symbols@^2.0.0:
72837331
version "2.0.0"
72847332
resolved "https://registry.npmjs.org/well-known-symbols/-/well-known-symbols-2.0.0.tgz#e9c7c07dbd132b7b84212c8174391ec1f9871ba5"
@@ -7432,6 +7480,13 @@ xtend@~4.0.1:
74327480
resolved "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
74337481
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
74347482

7483+
xxhash@^0.3.0:
7484+
version "0.3.0"
7485+
resolved "https://registry.npmjs.org/xxhash/-/xxhash-0.3.0.tgz#d20893a62c5b0f7260597dd55859b12a1e02c559"
7486+
integrity sha512-1ud2yyPiR1DJhgyF1ZVMt+Ijrn0VNS/wzej1Z8eSFfkNfRPp8abVZNV2u9tYy9574II0ZayZYZgJm8KJoyGLCw==
7487+
dependencies:
7488+
nan "^2.13.2"
7489+
74357490
xxhashjs@^0.2.2:
74367491
version "0.2.2"
74377492
resolved "https://registry.npmjs.org/xxhashjs/-/xxhashjs-0.2.2.tgz#8a6251567621a1c46a5ae204da0249c7f8caa9d8"

0 commit comments

Comments
 (0)