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

fix: faster hex to byte implementation #6596

Merged
merged 6 commits into from
Nov 20, 2023
Merged

fix: faster hex to byte implementation #6596

merged 6 commits into from
Nov 20, 2023

Conversation

mpetrunic
Copy link
Contributor

Description

Noticed this implementation in couple of crypto repos, apparently this implementation is 6x faster.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I have selected the correct base branch.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • Any dependent changes have been merged and published in downstream modules.
  • I ran npm run lint with success and extended the tests and types if necessary.
  • I ran npm run test:unit with success.
  • I ran npm run test:coverage and my test cases cover all the lines and branches of the added code.
  • I ran npm run build and tested dist/web3.min.js in a browser.
  • I have tested my code on the live network.
  • I have checked the Deploy Preview and it looks correct.
  • I have updated the CHANGELOG.md file in the root folder.
  • I have linked Issue(s) with this PR in "Linked Issues" menu.

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
Copy link

github-actions bot commented Nov 14, 2023

Bundle Stats

Hey there, this message comes from a github action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Asset Old size New size Diff Diff %
Total 589 KB 589 KB 209 bytes 0.03%
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset Old size New size Diff Diff %
web3.min.js 571 KB 571 KB 209 bytes 0.04%
../lib/commonjs/index.d.ts 8.63 KB 8.63 KB 0 0.00%
../lib/commonjs/accounts.d.ts 3.67 KB 3.67 KB 0 0.00%
../lib/commonjs/types.d.ts 2.45 KB 2.45 KB 0 0.00%
../lib/commonjs/web3.d.ts 1.14 KB 1.14 KB 0 0.00%
../lib/commonjs/abi.d.ts 1000 bytes 1000 bytes 0 0.00%
../lib/commonjs/eth.exports.d.ts 280 bytes 280 bytes 0 0.00%
../lib/commonjs/providers.exports.d.ts 148 bytes 148 bytes 0 0.00%
../lib/commonjs/version.d.ts 60 bytes 60 bytes 0 0.00%

Copy link

cloudflare-workers-and-pages bot commented Nov 14, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e3e188a
Status: ✅  Deploy successful!
Preview URL: https://5decc4d6.web3-js-docs.pages.dev
Branch Preview URL: https://fix-optimize-hex-2-bytes.web3-js-docs.pages.dev

View logs

Copy link

codecov bot commented Nov 14, 2023

Codecov Report

Merging #6596 (d6df883) into 4.x (4358140) will increase coverage by 2.18%.
Report is 110 commits behind head on 4.x.
The diff coverage is 92.97%.

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

Additional details and impacted files
@@            Coverage Diff             @@
##              4.x    #6596      +/-   ##
==========================================
+ Coverage   87.37%   89.56%   +2.18%     
==========================================
  Files         197      215      +18     
  Lines        7548     8288     +740     
  Branches     2059     2245     +186     
==========================================
+ Hits         6595     7423     +828     
+ Misses        953      865      -88     
Flag Coverage Δ
UnitTests 89.56% <92.97%> (+2.18%) ⬆️

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

Components Coverage Δ
web3 ∅ <ø> (∅)
web3-core ∅ <ø> (∅)
web3-errors ∅ <ø> (∅)
web3-eth ∅ <ø> (∅)
web3-eth-abi ∅ <ø> (∅)
web3-eth-accounts ∅ <ø> (∅)
web3-eth-contract ∅ <ø> (∅)
web3-eth-ens ∅ <ø> (∅)
web3-eth-iban ∅ <ø> (∅)
web3-eth-personal ∅ <ø> (∅)
web3-net ∅ <ø> (∅)
web3-providers-http ∅ <ø> (∅)
web3-providers-ipc ∅ <ø> (∅)
web3-providers-ws ∅ <ø> (∅)
web3-rpc-methods ∅ <ø> (∅)
web3-utils ∅ <ø> (∅)
web3-validator ∅ <ø> (∅)

Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba left a comment

Choose a reason for hiding this comment

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

Thanks @mpetrunic for your contribution.

I have just small suggestions.

packages/web3-validator/src/utils.ts Outdated Show resolved Hide resolved
packages/web3-validator/src/utils.ts Outdated Show resolved Hide resolved
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

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

linting should be fixed before merging ,

@jxom
Copy link

jxom commented Dec 7, 2023

Any chance there should be a comment in the code here that this was adapted from Viem?

@mpetrunic
Copy link
Contributor Author

mpetrunic commented Dec 7, 2023

Any chance there should be a comment in the code here that this was adapted from Viem?

@jxom Sure, I can open PR to add that note. But tbh, I wasn't sure where it originated from.

I thought it came from https://github.com/paulmillr/noble-hashes/blob/main/src/utils.ts#L50
because I've seen paul's PR on viem repo?

@jxom
Copy link

jxom commented Dec 7, 2023

Ah true! Paul made the PR to Viem for this. Didn’t realise it was also in hashes! Might make a reference note for this in Viem repo then.

@mpetrunic
Copy link
Contributor Author

Ah true! Paul made the PR to Viem for this. Didn’t realise it was also in hashes! Might make a reference note for this in Viem repo then.

#6646 :)

Comment on lines +453 to +470
const charCodeMap = {
zero: 48,
nine: 57,
A: 65,
F: 70,
a: 97,
f: 102,
} as const

function charCodeToBase16(char: number) {
if (char >= charCodeMap.zero && char <= charCodeMap.nine)
return char - charCodeMap.zero
if (char >= charCodeMap.A && char <= charCodeMap.F)
return char - (charCodeMap.A - 10)
if (char >= charCodeMap.a && char <= charCodeMap.f)
return char - (charCodeMap.a - 10)
return undefined
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@mpetrunic Is this function not equivalent to the native parseInt with difference take it take integer parameter while parseInt take string parameter as charCodeToBase16(97) is same as parseInt('a', 16).

The native parseInt in that use case is almost 5 times more faster.

https://runkit.com/nazarhussain/6571df9b17589500083041e5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well yes, but for parseInt, I need to substring 2 characters which is expensive (memory allocation and all).
Funny enough, benching on runkit yields totally different results than locally:
image
image

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.

6 participants