Skip to content
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

Initial Implementation #1

Merged
merged 4 commits into from
Jun 26, 2022
Merged

Initial Implementation #1

merged 4 commits into from
Jun 26, 2022

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented Jun 13, 2022

@mcollina
@kibertoad

This is the promised faster implementation.

I tried to extract the function processMatch but it is slightly slower. Or I had not the right idea.

identity;q=1 and gzip,identity x 7,380,422 ops/sec ±0.99% (88 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 11,526,066 ops/sec ±0.89% (89 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 2,521,171 ops/sec ±0.90% (87 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 2,846,303 ops/sec ±0.88% (88 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 2,920,872 ops/sec ±0.97% (89 runs sampled)
* and gzip,deflate x 734,274,924 ops/sec ±1.77% (82 runs sampled)
deflate;q=1.0, * and gzip x 5,058,241 ops/sec ±0.87% (89 runs sampled)
test,br and br x 10,555,576 ops/sec ±1.07% (85 runs sampled)
gzip;q=0 and  x 88,404,780 ops/sec ±0.85% (94 runs sampled)
gzip;q=0 and gzip,identity x 12,899,899 ops/sec ±1.21% (87 runs sampled)
white rabbit and gzip,identity x 6,975,091 ops/sec ±0.97% (90 runs sampled)
undefined and gzip,identity x 85,799,975 ops/sec ±0.69% (92 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 3,131,648 ops/sec ±1.04% (89 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 3,237,159 ops/sec ±1.15% (88 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 2,976,764 ops/sec ±0.66% (93 runs sampled)
gzip, deflate, br and br,gzip,deflate x 4,861,383 ops/sec ±0.65% (89 runs sampled)
* and br,gzip,deflate x 399,423,765 ops/sec ±19.45% (59 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 5,312,961 ops/sec ±0.90% (78 runs sampled)
identity;q=0 and identity x 7,171,469 ops/sec ±1.33% (87 runs sampled)
gzip, compress;q=0 and compress,gzip x 4,881,432 ops/sec ±0.87% (89 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 3,711,530 ops/sec ±1.81% (89 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 2,252,691 ops/sec ±0.77% (94 runs sampled)

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jun 13, 2022

We could theoretically write the negotiate function like a function where you set the supportedValues and then it has a fixed supportedValues array. Then we could apply caching, which could do some performance gain. But I am kind of unsure if this is actually a smart idea or not, as encodings could be alot variations.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jun 21, 2022

@mcollina
Any feedback?

@mcollina
Copy link
Member

Does this passes the same tests of the more popular module?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jun 22, 2022

Yes, I used the same tests + some tests from encoding package from dougwilson.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Code looks good.
How is the performance against the original?

Could you fill in the README too?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jun 24, 2022

I implemented also a Negotiator-Class, which seems to be slightly faster. You can also pass a Cache-Store, like mnemonist LRU-Cache, which makes it then incredible fast of course.

Here the latest benchmarks:

node benchmarks/negotiate.js 
identity;q=1 and gzip,identity x 8,324,480 ops/sec ±1.12% (88 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 12,881,974 ops/sec ±1.07% (89 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 2,640,400 ops/sec ±1.15% (90 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 2,976,596 ops/sec ±1.10% (91 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 3,030,612 ops/sec ±1.26% (86 runs sampled)
* and gzip,deflate x 721,577,308 ops/sec ±1.88% (84 runs sampled)
deflate;q=1.0, * and gzip x 5,104,490 ops/sec ±1.02% (87 runs sampled)
test,br and br x 9,533,048 ops/sec ±0.86% (88 runs sampled)
gzip;q=0 and  x 75,220,077 ops/sec ±0.73% (92 runs sampled)
gzip;q=0 and gzip,identity x 13,089,768 ops/sec ±1.08% (88 runs sampled)
white rabbit and gzip,identity x 7,194,967 ops/sec ±1.17% (85 runs sampled)
undefined and gzip,identity x 75,556,391 ops/sec ±0.85% (93 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 2,930,521 ops/sec ±1.76% (90 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 3,070,647 ops/sec ±0.83% (87 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 2,932,494 ops/sec ±1.02% (88 runs sampled)
gzip, deflate, br and br,gzip,deflate x 4,868,715 ops/sec ±1.37% (90 runs sampled)
* and br,gzip,deflate x 120,858,353 ops/sec ±3.32% (81 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 5,266,389 ops/sec ±1.17% (85 runs sampled)
identity;q=0 and identity x 7,097,012 ops/sec ±1.14% (85 runs sampled)
gzip, compress;q=0 and compress,gzip x 4,879,960 ops/sec ±0.65% (90 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 3,872,093 ops/sec ±0.69% (93 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 2,275,268 ops/sec ±1.96% (89 runs sampled)

The benchmarks of Negotiator-class

node benchmarks/Negotiator.js 
identity;q=1 and gzip,identity x 9,056,308 ops/sec ±1.10% (91 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 13,893,625 ops/sec ±1.08% (93 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 2,819,782 ops/sec ±1.21% (92 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 3,354,313 ops/sec ±0.68% (95 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 3,302,053 ops/sec ±1.42% (93 runs sampled)
* and gzip,deflate x 701,541,302 ops/sec ±0.43% (98 runs sampled)
deflate;q=1.0, * and gzip x 5,385,857 ops/sec ±0.83% (92 runs sampled)
test,br and br x 10,469,662 ops/sec ±1.94% (94 runs sampled)
gzip;q=0 and  x 82,215,189 ops/sec ±0.71% (94 runs sampled)
gzip;q=0 and gzip,identity x 13,703,082 ops/sec ±0.97% (89 runs sampled)
white rabbit and gzip,identity x 7,522,050 ops/sec ±0.99% (93 runs sampled)
undefined and gzip,identity x 82,851,862 ops/sec ±0.70% (95 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 3,173,137 ops/sec ±0.75% (94 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 3,285,739 ops/sec ±0.88% (92 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 3,239,147 ops/sec ±0.69% (95 runs sampled)
gzip, deflate, br and br,gzip,deflate x 5,111,553 ops/sec ±1.34% (96 runs sampled)
* and br,gzip,deflate x 665,710,687 ops/sec ±1.45% (93 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 5,685,409 ops/sec ±1.30% (93 runs sampled)
identity;q=0 and identity x 8,424,843 ops/sec ±0.86% (92 runs sampled)
gzip, compress;q=0 and compress,gzip x 5,223,453 ops/sec ±0.67% (94 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 4,018,832 ops/sec ±1.23% (94 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 2,524,941 ops/sec ±1.32% (93 runs sampled)

The benchmarks of Negotiator-class with LRUCache from mnemonist with a size of 1000

identity;q=1 and gzip,identity x 1,276,276,316 ops/sec ±0.11% (100 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 83,448,701 ops/sec ±0.12% (100 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 83,267,662 ops/sec ±0.16% (100 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 82,609,401 ops/sec ±0.68% (94 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 83,379,855 ops/sec ±0.15% (96 runs sampled)
* and gzip,deflate x 81,986,292 ops/sec ±1.22% (97 runs sampled)
deflate;q=1.0, * and gzip x 82,118,327 ops/sec ±0.28% (99 runs sampled)
test,br and br x 69,695,704 ops/sec ±4.09% (84 runs sampled)
gzip;q=0 and  x 56,319,830 ops/sec ±1.09% (94 runs sampled)
gzip;q=0 and gzip,identity x 56,102,778 ops/sec ±1.16% (92 runs sampled)
white rabbit and gzip,identity x 82,132,157 ops/sec ±0.39% (91 runs sampled)
undefined and gzip,identity x 159,007,665 ops/sec ±0.14% (98 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 79,755,680 ops/sec ±0.38% (96 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 65,173,032 ops/sec ±4.15% (80 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 54,334,682 ops/sec ±0.93% (89 runs sampled)
gzip, deflate, br and br,gzip,deflate x 78,978,702 ops/sec ±1.38% (95 runs sampled)
* and br,gzip,deflate x 71,324,729 ops/sec ±4.07% (83 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 53,901,367 ops/sec ±1.06% (93 runs sampled)
identity;q=0 and identity x 75,140,280 ops/sec ±3.05% (93 runs sampled)
gzip, compress;q=0 and compress,gzip x 54,929,535 ops/sec ±0.94% (94 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 53,847,423 ops/sec ±1.40% (87 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 64,022,160 ops/sec ±3.90% (78 runs sampled)

The same benchmarks against the encoding-negotiator-package

identity;q=1 and gzip,identity x 2,471,479 ops/sec ±1.23% (87 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 895,704 ops/sec ±0.94% (90 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 944,281 ops/sec ±1.75% (89 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 788,710 ops/sec ±1.60% (87 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 777,065 ops/sec ±2.07% (88 runs sampled)
* and gzip,deflate x 3,098,768 ops/sec ±1.07% (88 runs sampled)
deflate;q=1.0, * and gzip x 1,318,978 ops/sec ±0.99% (86 runs sampled)
test,br and br x 1,696,693 ops/sec ±0.89% (91 runs sampled)
gzip;q=0 and  x 2,896,883 ops/sec ±0.96% (90 runs sampled)
gzip;q=0 and gzip,identity x 2,466,758 ops/sec ±1.07% (92 runs sampled)
white rabbit and gzip,identity x 2,998,611 ops/sec ±1.18% (89 runs sampled)
undefined and gzip,identity x 110,211,761 ops/sec ±1.13% (88 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 804,550 ops/sec ±1.02% (90 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 848,624 ops/sec ±1.02% (87 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 804,496 ops/sec ±1.04% (90 runs sampled)
gzip, deflate, br and br,gzip,deflate x 852,218 ops/sec ±0.97% (87 runs sampled)
* and br,gzip,deflate x 2,893,678 ops/sec ±0.89% (85 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 886,444 ops/sec ±1.10% (88 runs sampled)
identity;q=0 and identity x 2,659,673 ops/sec ±0.90% (91 runs sampled)
gzip, compress;q=0 and compress,gzip x 1,083,041 ops/sec ±1.43% (88 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 1,061,551 ops/sec ±1.15% (92 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 578,058 ops/sec ±0.79% (90 runs sampled)

The same benchmarks against the negotiator-package, which is used by express:

identity;q=1 and gzip,identity x 1,196,783 ops/sec ±0.54% (95 runs sampled)
gzip;q=1, identity;q=0.5 and gzip,deflate x 678,071 ops/sec ±0.47% (96 runs sampled)
deflate;q=0.5,identity; q=0.5 and gzip,deflate x 645,024 ops/sec ±0.60% (92 runs sampled)
deflate;q=0.5, gzip;q=0.5 and gzip,deflate x 560,488 ops/sec ±0.54% (97 runs sampled)
deflate;q=0.5, gzip;q=0.5 and deflate,gzip x 574,648 ops/sec ±0.96% (91 runs sampled)
* and gzip,deflate x 1,771,614 ops/sec ±0.55% (97 runs sampled)
deflate;q=1.0, * and gzip x 983,923 ops/sec ±0.98% (94 runs sampled)
test,br and br x 1,894,286 ops/sec ±0.78% (91 runs sampled)
gzip;q=0 and  x 1,345,700 ops/sec ±2.14% (95 runs sampled)
gzip;q=0 and gzip,identity x 1,113,240 ops/sec ±0.55% (90 runs sampled)
white rabbit and gzip,identity x 1,866,715 ops/sec ±0.76% (91 runs sampled)
undefined and gzip,identity x 2,074,228 ops/sec ±1.18% (97 runs sampled)
compress;q=0.5, gzip;q=1.0 and gzip,compress x 497,194 ops/sec ±0.89% (95 runs sampled)
gzip;q=1.0, compress;q=0.5 and compress,gzip x 492,783 ops/sec ±0.53% (93 runs sampled)
compress;q=0.5, gzip;q=1.0 and compress x 571,516 ops/sec ±0.76% (93 runs sampled)
gzip, deflate, br and br,gzip,deflate x 773,635 ops/sec ±0.85% (86 runs sampled)
* and br,gzip,deflate x 1,508,239 ops/sec ±1.17% (88 runs sampled)
*;q=0, identity;q=1 and gzip,identity x 710,647 ops/sec ±1.76% (97 runs sampled)
identity;q=0 and identity x 1,185,037 ops/sec ±1.42% (92 runs sampled)
gzip, compress;q=0 and compress,gzip x 782,730 ops/sec ±0.48% (89 runs sampled)
gzip;q=0.8, deflate and gzip,deflate x 647,693 ops/sec ±0.43% (88 runs sampled)
gzip;q=0.8, identity;q=0.5, *;q=0.3 and deflate,gzip,br x 414,305 ops/sec ±0.61% (91 runs sampled)

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jun 24, 2022

I guess we can also use this for fastify-accepts

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 276a8db into fastify:master Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants