cryptocoinjs / base-x

Encode/decode any base
MIT License
312 stars 75 forks source link

Improve decoding performance #81

Closed steveluscher closed 4 months ago

steveluscher commented 1 year ago

This yields a performance improvement over indexing into source over and over. source is asserted to be a string, psz is monotonically increasing, and psz >= source.length is a good indication that you've run out of characters.

Originally proposed by @oscxc in #74.

Benchmarks

cd benchmark/
(cd ../ && npm run build) && SEED=8854dc2a353e143702ef1b29874b63a4 npm start

Before

> base-x-benchmark@0.0.0 start
> node index.js

Seed: 8854dc2a353e143702ef1b29874b63a4
--------------------------------------------------
encode x 389,286 ops/sec ±0.21% (8 runs sampled)
decode x 415,231 ops/sec ±0.91% (9 runs sampled)
==================================================

After

> base-x-benchmark@0.0.0 start
> node index.js

Seed: 8854dc2a353e143702ef1b29874b63a4
--------------------------------------------------
encode x 389,695 ops/sec ±0.12% (9 runs sampled)
decode x 449,877 ops/sec ±0.13% (9 runs sampled)
==================================================
steveluscher commented 4 months ago

Rebased on top of @Nesopie's ESM changes. Let's get this in!

steveluscher commented 4 months ago

You don’t feel like backporting this to bs58 version 4.x, do you @junderw? There are reasons we can’t upgrade to v6 and have to stay on 4.x.

junderw commented 4 months ago

Give me a list of commands to backport and publish all these backports without updating the npm latest tag for everything, and I'll consider running those commands when I have free time.

It would probably take me a few minutes to find everything, cherry-pick, push a branch for previous major version(s) push a tag to Github, publish to npm under a new non-latest tag etc. And then I would need to do the same for bs58 and bump the dependency etc.

Probably about 15-20 minutes of work depending on how much I can concentrate without messing up.

But if you give me a list of commands and I can check that they're sane, running them would be a few seconds.

Assuming base-x and bs58 are sibling directories and these repos are origin remotes is fine.

steveluscher commented 4 months ago

For sure!

# Rewind to base-x@3.0.9
cd base-x
git checkout v3.0.9
git clean -dfx .
npm i

# Backport change, build, and test
git cherry-pick 261fb3636147815a7705375cfe7c1217db15e8f5
rm -rf src
npm run build
git add -A
git cherry-pick --continue --no-edit
npm run test

# Publish base-x@3.0.10
npm version 3.0.10  # automatically adds the git tag v3.0.10
npm publish --tag bp  # avoids updating the latest tag on npm
npm dist-tag rm base-x bp  # deletes the temporary npm tag we just created

# Create a backport branch for posterity and push the whole thing to Github
git checkout -b v3.x
git push origin v3.x  # the branch
git push origin v3.0.10  # the tag

That's it. bs58@4.0.1 will pick up this new version by virtue of it depending on base-x@^3.0.2. No need to backport anything to bs58.

junderw commented 4 months ago

ok, should be published.

steveluscher commented 4 months ago

Thank you!