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

Performance: Cache obj[key] access into #707

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

cesco69
Copy link
Contributor

@cesco69 cesco69 commented Apr 10, 2024

Caching the value of obj[key] into a variable instead of continue access to object

PR

short string............................................. x 22,806,425 ops/sec ±0.47% (191 runs sampled)
unsafe short string...................................... x 1,027,602,256 ops/sec ±0.34% (191 runs sampled)
short string with double quote........................... x 13,276,009 ops/sec ±0.33% (190 runs sampled)
long string without double quotes........................ x 15,398 ops/sec ±0.24% (192 runs sampled)
unsafe long string without double quotes................. x 1,011,926,548 ops/sec ±0.30% (188 runs sampled)
long string.............................................. x 15,779 ops/sec ±0.22% (193 runs sampled)
unsafe long string....................................... x 1,018,123,202 ops/sec ±0.31% (190 runs sampled)
number................................................... x 1,021,065,239 ops/sec ±0.26% (191 runs sampled)
integer.................................................. x 218,888,807 ops/sec ±0.33% (191 runs sampled)

master

short string............................................. x 23,010,004 ops/sec ±0.58% (190 runs sampled)
unsafe short string...................................... x 1,062,255,820 ops/sec ±0.37% (191 runs sampled)
short string with double quote........................... x 13,749,470 ops/sec ±0.37% (191 runs sampled)
long string without double quotes........................ x 15,661 ops/sec ±0.31% (192 runs sampled)
unsafe long string without double quotes................. x 1,062,491,836 ops/sec ±0.37% (191 runs sampled)
long string.............................................. x 16,092 ops/sec ±0.32% (192 runs sampled)
unsafe long string....................................... x 1,060,550,353 ops/sec ±0.31% (192 runs sampled)
number................................................... x 1,058,792,481 ops/sec ±0.33% (191 runs sampled)
integer.................................................. x 220,943,513 ops/sec ±0.29% (192 runs sampled)

Checklist

Cache into a variable the object[key] value

Signed-off-by: francesco <[email protected]>
@cesco69 cesco69 changed the title Performance: Cache object[key] access into Performance: Cache obj[key] access into Apr 10, 2024
@mcollina
Copy link
Member

it does not seem there are any improvements, or I'm misreading this?

@nigrosimone
Copy link
Contributor

The problem with micro-optimization is the instability of the benchmark. The PR is correct, but the difference is negligible. Tomorrow try to run benchmark on thousands of samples (now is 100)

@cesco69
Copy link
Contributor Author

cesco69 commented Apr 11, 2024

@mcollina results on 250 minSamples (default is 100):
PR

short string............................................. x 23,559,223 ops/sec ±0.46% (340 runs sampled)
unsafe short string...................................... x 1,066,690,805 ops/sec ±0.35% (339 runs sampled)
short string with double quote........................... x 13,826,413 ops/sec ±1.12% (341 runs sampled)
long string without double quotes........................ x 15,676 ops/sec ±0.36% (339 runs sampled)
unsafe long string without double quotes................. x 1,068,193,570 ops/sec ±0.41% (339 runs sampled)
long string.............................................. x 15,581 ops/sec ±0.63% (339 runs sampled)
unsafe long string....................................... x 1,066,081,767 ops/sec ±0.40% (339 runs sampled)
number................................................... x 1,069,705,607 ops/sec ±0.41% (340 runs sampled)
integer.................................................. x 223,383,371 ops/sec ±0.38% (342 runs sampled)
formatted date-time...................................... x 1,725,162 ops/sec ±0.41% (342 runs sampled)

Master

short string............................................. x 23,490,390 ops/sec ±0.49% (340 runs sampled)
unsafe short string...................................... x 1,061,049,228 ops/sec ±0.32% (340 runs sampled)
short string with double quote........................... x 13,880,875 ops/sec ±0.35% (342 runs sampled)
long string without double quotes........................ x 15,662 ops/sec ±0.39% (340 runs sampled)
unsafe long string without double quotes................. x 1,060,244,571 ops/sec ±0.48% (339 runs sampled)
long string.............................................. x 15,562 ops/sec ±0.55% (341 runs sampled)
unsafe long string....................................... x 1,070,663,605 ops/sec ±0.40% (342 runs sampled)
number................................................... x 1,063,399,118 ops/sec ±0.38% (340 runs sampled)
integer.................................................. x 222,684,335 ops/sec ±0.37% (342 runs sampled)
formatted date-time...................................... x 1,661,265 ops/sec ±0.39% (340 runs sampled)

@mcollina
Copy link
Member

It seems you are cutting out all the object benchmarks? I thought this impacted those.

@cesco69
Copy link
Contributor Author

cesco69 commented Apr 11, 2024

It seems you are cutting out all the object benchmarks? I thought this impacted those.

@mcollina minSamples 500

PR

short string............................................. x 23,486,212 ops/sec ±0.41% (591 runs sampled)
unsafe short string...................................... x 1,071,682,433 ops/sec ±0.25% (591 runs sampled)
short string with double quote........................... x 14,071,608 ops/sec ±0.31% (590 runs sampled)
long string without double quotes........................ x 15,706 ops/sec ±0.28% (590 runs sampled)
unsafe long string without double quotes................. x 1,078,410,601 ops/sec ±0.30% (592 runs sampled)
long string.............................................. x 16,323 ops/sec ±0.27% (592 runs sampled)
unsafe long string....................................... x 1,083,630,259 ops/sec ±0.31% (592 runs sampled)
number................................................... x 1,033,759,666 ops/sec ±0.27% (590 runs sampled)
integer.................................................. x 219,223,329 ops/sec ±0.27% (591 runs sampled)
formatted date-time...................................... x 1,697,386 ops/sec ±0.27% (590 runs sampled)
formatted date........................................... x 1,109,075 ops/sec ±0.27% (588 runs sampled)
formatted time........................................... x 1,107,261 ops/sec ±0.27% (590 runs sampled)
short array of numbers................................... x 77,795 ops/sec ±0.21% (591 runs sampled)
short array of integers.................................. x 65,291 ops/sec ±0.22% (591 runs sampled)
short array of short strings............................. x 20,105 ops/sec ±0.21% (590 runs sampled)
short array of long strings.............................. x 20,130 ops/sec ±0.19% (590 runs sampled)
short array of objects with properties of different types x 9,486 ops/sec ±0.19% (590 runs sampled)
object with number property.............................. x 1,044,304,818 ops/sec ±0.26% (589 runs sampled)
object with integer property............................. x 222,809,874 ops/sec ±0.27% (591 runs sampled)
object with short string property........................ x 22,944,390 ops/sec ±0.33% (589 runs sampled)
object with long string property......................... x 16,179 ops/sec ±0.25% (592 runs sampled)
object with properties of different types................ x 1,837,666 ops/sec ±0.42% (582 runs sampled)
simple object............................................ x 9,934,298 ops/sec ±0.24% (590 runs sampled)
simple object with required fields....................... x 9,819,406 ops/sec ±0.23% (590 runs sampled)
object with const string property........................ x 1,057,533,293 ops/sec ±0.29% (589 runs sampled)
object with const number property........................ x 1,055,692,892 ops/sec ±0.28% (588 runs sampled)
object with const bool property.......................... x 1,059,992,242 ops/sec ±0.29% (591 runs sampled)
object with const object property........................ x 1,077,156,612 ops/sec ±0.27% (591 runs sampled)
object with const null property.......................... x 1,080,980,241 ops/sec ±0.27% (591 runs sampled)

Master

short string............................................. x 20,336,896 ops/sec ±1.56% (591 runs sampled)
unsafe short string...................................... x 1,063,754,842 ops/sec ±0.27% (591 runs sampled)
short string with double quote........................... x 14,049,638 ops/sec ±0.29% (591 runs sampled)
long string without double quotes........................ x 15,826 ops/sec ±0.28% (591 runs sampled)
unsafe long string without double quotes................. x 1,078,118,074 ops/sec ±0.28% (591 runs sampled)
long string.............................................. x 16,363 ops/sec ±0.27% (592 runs sampled)
unsafe long string....................................... x 1,072,536,686 ops/sec ±0.25% (592 runs sampled)
number................................................... x 1,073,209,502 ops/sec ±0.29% (592 runs sampled)
integer.................................................. x 219,494,857 ops/sec ±0.32% (586 runs sampled)
formatted date-time...................................... x 1,626,708 ops/sec ±0.25% (588 runs sampled)
formatted date........................................... x 1,110,917 ops/sec ±0.27% (591 runs sampled)
formatted time........................................... x 1,113,999 ops/sec ±0.25% (589 runs sampled)
short array of numbers................................... x 77,544 ops/sec ±0.24% (591 runs sampled)
short array of integers.................................. x 64,974 ops/sec ±0.21% (591 runs sampled)
short array of short strings............................. x 20,208 ops/sec ±0.20% (591 runs sampled)
short array of long strings.............................. x 20,094 ops/sec ±0.26% (591 runs sampled)
short array of objects with properties of different types x 9,549 ops/sec ±0.16% (591 runs sampled)
object with number property.............................. x 1,043,371,336 ops/sec ±0.25% (590 runs sampled)
object with integer property............................. x 218,891,786 ops/sec ±0.18% (589 runs sampled)
object with short string property........................ x 23,101,644 ops/sec ±0.32% (589 runs sampled)
object with long string property......................... x 15,707 ops/sec ±0.11% (590 runs sampled)
object with properties of different types................ x 1,831,538 ops/sec ±0.52% (580 runs sampled)
simple object............................................ x 9,887,605 ops/sec ±0.23% (590 runs sampled)
simple object with required fields....................... x 10,118,325 ops/sec ±0.31% (590 runs sampled)
object with const string property........................ x 1,072,143,772 ops/sec ±0.28% (590 runs sampled)
object with const number property........................ x 1,061,290,681 ops/sec ±0.26% (590 runs sampled)
object with const bool property.......................... x 1,074,415,151 ops/sec ±0.29% (591 runs sampled)
object with const object property........................ x 1,080,103,573 ops/sec ±0.29% (591 runs sampled)
object with const null property.......................... x 1,074,310,277 ops/sec ±0.27% (589 runs sampled)

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 686e6ce into fastify:master Apr 11, 2024
19 checks passed
@cesco69 cesco69 deleted the cache-obj-access branch April 11, 2024 14:15
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.

4 participants