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

perf: avoid unnecessary flat #1703

Merged
merged 2 commits into from
Apr 27, 2023
Merged

Conversation

juanrgm
Copy link
Contributor

@juanrgm juanrgm commented Apr 25, 2023

Summary

splitProps calls Array.prototype.flat unnecessarily and this causes some delays when there are many keys.

How did you test this change?

var values = Array.from({ length: 100 }).map((v, i) => `value${i}`);
var tests = 1000;

// 24.216064453125 ms
function flat() {
  console.time("flat");
  for (let x = 1; x <= tests; ++x) new Set(values.flat());
  console.timeEnd("flat");
}

// 3.248046875 ms
function noflat() {
  console.time("noflat");
  for (let x = 1; x <= tests; ++x) new Set(values);
  console.timeEnd("noflat");
}

function benchmark() {
  flat();
  noflat();
}

benchmark();

@changeset-bot
Copy link

changeset-bot bot commented Apr 25, 2023

🦋 Changeset detected

Latest commit: 1b5ea07

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
solid-js Patch
test-integration Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@juanrgm juanrgm force-pushed the perf-splitProps-flat branch from ff9fca7 to 5675b7c Compare April 25, 2023 23:29
Copy link

@ankit-gautam23 ankit-gautam23 left a comment

Choose a reason for hiding this comment

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

LGTM

@juanrgm juanrgm force-pushed the perf-splitProps-flat branch from 5675b7c to 27e942b Compare April 27, 2023 21:06
@ryansolid
Copy link
Member

To be fair I'd be shocked if the keys ever got over 12. I'm sure it is faster to access the index in any case and size add is minimal.

@ryansolid ryansolid merged commit f04bdc9 into solidjs:main Apr 27, 2023
@juanrgm
Copy link
Contributor Author

juanrgm commented Apr 27, 2023

To be fair I'd be shocked if the keys ever got over 12. I'm sure it is faster to access the index in any case and size add is minimal.

In SUID there are 100 style properties, not counting those of the component itself, so almost all components are suffering a noticeable performance loss.

I'm rewriting the splitProps and mergeProps functions and the results are promising:

✓ test/component.bench.ts (10) 7674ms
   ✓ splitProps(0, 15) (2) 2189ms
     name                            hz     min      max    mean     p75     p99    p995    p999     rme  samples
   · oldSplitProps(0, 15)    646,064.99  0.0011  19.4825  0.0015  0.0013  0.0045  0.0051  0.0100  ±7.74%   323033
   · newSplitProps(0, 15)  3,573,490.18  0.0001   3.0986  0.0003  0.0003  0.0006  0.0007  0.0014  ±1.90%  1786746   fastest
   ✓ splitProps(0, 100) (2) 2423ms
     name                             hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · oldSplitProps(0, 100)    140,244.40  0.0061  0.8396  0.0071  0.0067  0.0128  0.0154  0.0569  ±0.99%    70123
   · newSplitProps(0, 100)  3,655,819.15  0.0002  2.4475  0.0003  0.0003  0.0006  0.0008  0.0016  ±1.45%  1827910   fastest
   ✓ splitProps(25, 100) (2) 3357ms
     name                           hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · oldSplitProps(25, 100)  32,577.70  0.0265  0.5104  0.0307  0.0277  0.0671  0.0942  0.2191  ±0.74%    16289
   · newSplitProps(25, 100)  97,826.43  0.0094  0.5183  0.0102  0.0097  0.0194  0.0228  0.0567  ±0.51%    48914   fastest  
   ✓ splitProps(50, 100) (2) 4270ms
     name                           hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · oldSplitProps(50, 100)  16,848.36  0.0489  2.0058  0.0594  0.0585  0.1376  0.1900  0.3668  ±1.17%     8430
   · newSplitProps(50, 100)  37,215.99  0.0212  0.5338  0.0269  0.0225  0.0810  0.0947  0.1797  ±0.81%    18608   fastest  
   ✓ splitProps(100, 25) (2) 5179ms
     name                           hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · oldSplitProps(100, 25)   9,644.99  0.0952  0.5339  0.1037  0.1000  0.2092  0.2367  0.4495  ±0.72%     4823
   · newSplitProps(100, 25)  21,117.39  0.0425  1.0419  0.0474  0.0441  0.1081  0.1488  0.3723  ±0.88%    10560   fastest  
   · SplitProps bench (0)


 BENCH  Summary

  newSplitProps(0, 15) - test/component.bench.ts > splitProps(0, 15)
    5.53x faster than oldSplitProps(0, 15)

  newSplitProps(0, 100) - test/component.bench.ts > splitProps(0, 100)
    26.07x faster than oldSplitProps(0, 100)

  newSplitProps(25, 100) - test/component.bench.ts > splitProps(25, 100)
    3.00x faster than oldSplitProps(25, 100)

  newSplitProps(50, 100) - test/component.bench.ts > splitProps(50, 100)
    2.21x faster than oldSplitProps(50, 100)

  newSplitProps(100, 25) - test/component.bench.ts > splitProps(100, 25)
    2.19x faster than oldSplitProps(100, 25)

@coveralls
Copy link

coveralls commented May 24, 2024

Pull Request Test Coverage Report for Build 4824514257

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 94.011%

Totals Coverage Status
Change from base Build 4640851655: -0.02%
Covered Lines: 4077
Relevant Lines: 4267

💛 - Coveralls

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