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

⚡ Faster decomposeFloat/Double #4059

Merged
merged 3 commits into from
Jul 13, 2023

Conversation

zbjornson
Copy link
Contributor

This replaces the (creative) loop-based implementation with a constant-time one. The number 1.234 is about 1100x faster and 1e30 is about 2300x faster.

If you're okay with this change I can do the same thing for double.

Category:

  • ✨ Introduce new features
  • 📝 Add or update documentation
  • ✅ Add or update tests
  • 🐛 Fix a bug
  • 🏷️ Add or update types
  • ⚡️ Improve performance
  • Other(s): ...

Potential impacts:

  • Generated values
  • Shrink values
  • Performance - faster
  • Typings
  • Other(s): ...

Sorry, something went wrong.

@zbjornson zbjornson requested a review from dubzzz as a code owner July 10, 2023 20:32
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 10, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1c798e1:

Sandbox Source
Vanilla Configuration
@fast-check/examples Configuration

@dubzzz
Copy link
Owner

dubzzz commented Jul 10, 2023

Looks awesome. I'll spend some time checking the diff but I think we can go with a similar optim for doubles

@zbjornson
Copy link
Contributor Author

Thanks! I'm happy to work on the double variant also.

It looks like I broke something. How do you get unit tests to run locally? If I run yarn workspace fast-check test, I get a ton of TS errors.

Comment on lines 19 to 17
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

Perfect! It even works with node 8 which is the currently minimal supported version for fast-check (will be dropped soon, but so far v3 is supposed to support it).

@dubzzz
Copy link
Owner

dubzzz commented Jul 11, 2023

You may try:

yarn # install all deps
yarn build:all # build all packages

# usually I changedir into /packages/fast-check
# but workspace commands also work
yarn test
yarn e2e # runs against prod bundle so need `yarn build` to be called before if you change things

You'll also need to declare the version bump (minor for fast-check and none for others):

yarn version:bump

I'd need to update my contributing guide with this setup

@zbjornson zbjornson force-pushed the zb/constant-time-decompose branch from c2516bf to ffcd568 Compare July 13, 2023 02:56
The returned `significand` and `exponent` can never be NaN, and `significand * 0x800000` will always be an integer. That means we can simplify `floatToIndex`.
@zbjornson zbjornson force-pushed the zb/constant-time-decompose branch from ffcd568 to f715047 Compare July 13, 2023 03:17
@zbjornson
Copy link
Contributor Author

Thanks for the guidance there. Fixed the failing test, switched from DataView to TypedArray for slightly better perf, ran version:bump, and added another commit for decomposeDouble.

@codecov
Copy link

codecov bot commented Jul 13, 2023

Codecov Report

Merging #4059 (f715047) into main (f1a86ce) will increase coverage by 0.03%.
The diff coverage is 100.00%.

❗ Current head f715047 differs from pull request most recent head 1c798e1. Consider uploading reports for the commit 1c798e1 to get more accurate results

@@            Coverage Diff             @@
##             main    #4059      +/-   ##
==========================================
+ Coverage   94.60%   94.64%   +0.03%     
==========================================
  Files         211      211              
  Lines        5893     5897       +4     
  Branches     1351     1353       +2     
==========================================
+ Hits         5575     5581       +6     
+ Misses        302      300       -2     
  Partials       16       16              
Flag Coverage Δ
unit-tests 94.64% <100.00%> (+0.03%) ⬆️
unit-tests-14.x-Linux 94.64% <100.00%> (+0.03%) ⬆️
unit-tests-16.x-Linux 94.64% <100.00%> (+0.03%) ⬆️
unit-tests-18.x-Linux 94.64% <100.00%> (+0.03%) ⬆️
unit-tests-latest-Linux 94.64% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
.../src/arbitrary/_internals/helpers/DoubleHelpers.ts 100.00% <100.00%> (+1.63%) ⬆️
...k/src/arbitrary/_internals/helpers/FloatHelpers.ts 100.00% <100.00%> (+2.12%) ⬆️
packages/fast-check/src/arbitrary/double.ts 100.00% <100.00%> (ø)
packages/fast-check/src/arbitrary/float.ts 100.00% <100.00%> (ø)

@dubzzz
Copy link
Owner

dubzzz commented Jul 13, 2023

It seems that format is invalid. Can you please run yarn format on your PR?

@zbjornson zbjornson force-pushed the zb/constant-time-decompose branch from f715047 to 1c798e1 Compare July 13, 2023 15:28
@dubzzz dubzzz changed the title Faster decomposeFloat implementation ⚡ Faster decomposeFloat implementation Jul 13, 2023
@dubzzz
Copy link
Owner

dubzzz commented Jul 13, 2023

Thank you so much for this awesome contribution 🥰

@dubzzz dubzzz changed the title ⚡ Faster decomposeFloat implementation ⚡ Faster decomposeFloat/Double Jul 13, 2023
@dubzzz dubzzz enabled auto-merge (squash) July 13, 2023 22:00
@dubzzz dubzzz merged commit 11b316e into dubzzz:main Jul 13, 2023
@zbjornson zbjornson deleted the zb/constant-time-decompose branch July 14, 2023 00:39
@zbjornson
Copy link
Contributor Author

My pleasure, thanks for the fast review.

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.

None yet

2 participants