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

uint: optimize FromStr, make it no_std-compatible #468

Merged
merged 7 commits into from
Dec 9, 2020
Merged

uint: optimize FromStr, make it no_std-compatible #468

merged 7 commits into from
Dec 9, 2020

Conversation

vorot93
Copy link
Contributor

@vorot93 vorot93 commented Dec 6, 2020

Removes a Vec allocation, and a String creation in odd length path.

As I had to change underlying hex impl from rustc-hex to hex, the resulting error type changed as well. This is a breaking change.

uint/src/uint.rs Outdated Show resolved Hide resolved
uint/src/uint.rs Outdated Show resolved Hide resolved
@ordian ordian added the breaking-change Breaking change label Dec 7, 2020
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

I like the change from rustc-hex to hex and I think it's the right path. I agree with @ordian that it'd be good to avoid repeating the same mistake again and leak error types; an opaque Error type seems good here.

I'm less convinced by the buffer-length gymnastics; unless there's a compelling performance win I think I prefer the readability of the previous code over what is here. Can you provide a benchmark that proves a substantial speedup of the code?

@vorot93
Copy link
Contributor Author

vorot93 commented Dec 7, 2020

@dvdplm Added bench:

Before PR:
from_str time: [2.4288 us 2.4715 us 2.5148 us]

With PR changes:
from_str time: [752.62 ns 756.82 ns 761.81 ns]

But even more importantly, by avoiding Vec and String, we can enable FromStr implementation for no_std targets without alloc.

@vorot93 vorot93 requested review from ordian and dvdplm December 7, 2020 23:23
Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

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

Looks fine. I guess we're trading readability here for perf and no_std support.
Would be nice to add more test case of length 0, 1, 3 and bring back the previous test case.

@dvdplm
Copy link
Contributor

dvdplm commented Dec 8, 2020

I can confirm the perf improvement, 2 - 2.5x.

uint/src/uint.rs Outdated Show resolved Hide resolved
uint/src/uint.rs Outdated Show resolved Hide resolved
@vorot93 vorot93 requested a review from niklasad1 December 8, 2020 14:36

if $n_words * 8 < bytes.len() {
return Err(Self::Err::InvalidHexLength);
if encoded.len() > MAX_ENCODED_LEN {
Copy link
Member

Choose a reason for hiding this comment

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

I find this code hard to read, it could maybe be possible to refactor to avoid repeating let out = &mut bytes[BYTES_LEN - encoded.len() / 2..]; and $crate::hex::decode_to_slice(encoded, out).map_err(Self::Err::from)?; twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it's possible because owning buffers are of different types (value: &str vs s: [u8; MAX_ENCODED_LEN]), different lengths and borrowing is involved here.

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

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

As Andronik said, would be good with a couple of tests with odd lengths of the hex str

@ordian
Copy link
Member

ordian commented Dec 9, 2020

Thanks!

@ordian ordian merged commit 7a54f5b into paritytech:master Dec 9, 2020
@vorot93 vorot93 deleted the uint-no-std-fromstr branch December 9, 2020 16:10
sergeyboyko0791 added a commit to KomodoPlatform/parity-common that referenced this pull request Aug 18, 2021
* The time crate after 0.2 is unergonomic to use; just use chrono (paritytech#458)

The 0.2 series of `time` doesn't make it easy to "just" create a timestamp. The `chrono` crate uses `time` v0.1 and is, I believe, what users like us should be using. So let's just do that.

* build(deps): update send_wrapper requirement from 0.3.0 to 0.5.0 (paritytech#461)

Updates the requirements on [send_wrapper](https://github.com/thk1/send_wrapper) to permit the latest version.
- [Release notes](https://github.com/thk1/send_wrapper/releases)
- [Changelog](https://github.com/thk1/send_wrapper/blob/master/CHANGELOG.md)
- [Commits](thk1/send_wrapper@v0.3.0...v0.5.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* rlp: add bytes impls (paritytech#459)

* rlp: add bytes impls

* add tests

* rlp: Fix buffer indexing (paritytech#462)

* Fix buffer indexing

* test for clear

* rlp: store test bytes in hex literals (paritytech#465)

* Make RLP optional in several crates (paritytech#466)

* bump versions and update changelogs (paritytech#463)

* update other dependencies (paritytech#471)

* ethereum-types/rlp should pull primitive-types/rlp (paritytech#474)

* uint: optimize FromStr, make it no_std-compatible (paritytech#468)

* Add from_str bench

* uint: optimize FromStr, make it no_std-compatible

* custom error type

* fmt::Display is actually available in core

* Error::source for FromHexError

* uppercase consts

* additional tests

* kvdb-rocksdb: replace tempdir with tempfile(paritytech#350) (paritytech#477)

* chore(deps): cargo upgrade parking_lot --all (paritytech#470)

* chore(deps): cargo upgrade parking_lot --all

* chore(deps): bump versions breaking change.

* chore: update changelog

	* kvdb
	* kvdb-memorydb
	* kvdb-rocksdb
	* parity-util-mem

* fix nits

* fix: kvdb-web add missing changelog entry

* fix: bad merge

* fix more nits: use breaking label

* [kvdb-memorydb]: add `wasm-bindgen` feature flag

* grumbles: remove `wasm-bindgen` feature flag

* Add hack only in `kvdb-web`
* Remove feature flag `wasm-bindgen` from `kvdb-memorydb`

* Bump bytes to 1.0 (paritytech#482)

* Implement Num from num-traits (paritytech#480)

* parity-crypto: remove UB test (paritytech#484)

* parity-crypto: remove UB test

* rlp: fix unused import

* parity-crypto: upgrade deps (paritytech#483)

* update some dev-dependencies (paritytech#493)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* fix: make from_str parse 0x-prefixed strings (paritytech#487)

* fix: make from_str parse 0x-prefixed strings

* fix(uint): make from_str parse 0x-prefixed strings

* chore: address review styling comments

* fix: tabs instead of spaces

* chore: cargo fmt

* fix: use strip_prefix instead of starts_with

* build(deps): update impl-trait-for-tuples requirement (paritytech#490)

Updates the requirements on [impl-trait-for-tuples](https://github.com/bkchr/impl-trait-for-tuples) to permit the latest version.
- [Release notes](https://github.com/bkchr/impl-trait-for-tuples/releases)
- [Commits](https://github.com/bkchr/impl-trait-for-tuples/commits/v0.2)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* primitive-types: address nits of paritytech#480 (paritytech#485)

* primitive-types: address nits of paritytech#480

* fix ethereum-types

* Remove deprecated FromStr/TryFrom impls for Secret (paritytech#495)

* Remove deprecated FromStr/TryFrom impls for Secret

* update CHANGELOG

* build(deps): update secp256k1 requirement from 0.19.0 to 0.20.0 (paritytech#496)

Updates the requirements on [secp256k1](https://github.com/rust-bitcoin/rust-secp256k1) to permit the latest version.
- [Release notes](https://github.com/rust-bitcoin/rust-secp256k1/releases)
- [Changelog](https://github.com/rust-bitcoin/rust-secp256k1/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-bitcoin/rust-secp256k1/commits)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Andronik Ordian <write@reusable.software>

* build(deps): update smallvec requirement from 0.6.10 to 1.6.0 (paritytech#494)

Updates the requirements on [smallvec](https://github.com/servo/rust-smallvec) to permit the latest version.
- [Release notes](https://github.com/servo/rust-smallvec/releases)
- [Commits](https://github.com/servo/rust-smallvec/commits/v1.6.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Andronik Ordian <write@reusable.software>

* build(deps): update rand requirement from 0.7.2 to 0.8.0 (paritytech#488)

* build(deps): update rand requirement from 0.7.2 to 0.8.0

Updates the requirements on [rand](https://github.com/rust-random/rand) to permit the latest version.
- [Release notes](https://github.com/rust-random/rand/releases)
- [Changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md)
- [Commits](rust-random/rand@0.7.3...0.8.0)

Signed-off-by: dependabot[bot] <support@github.com>

* Seed from u64

* uint: use rand 0.7 for quickcheck feature

* kvdb-rocksdb: fix compilation for benches

* parity-crypto: remove unused dep and fix a warning

* cargo fmt and another unused dep

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: David Palm <dvdplm@gmail.com>

* build(deps): update rand_xorshift requirement from 0.2.0 to 0.3.0 (paritytech#491)

Updates the requirements on [rand_xorshift](https://github.com/rust-random/rngs) to permit the latest version.
- [Release notes](https://github.com/rust-random/rngs/releases)
- [Commits](https://github.com/rust-random/rngs/commits/rand_xorshift-0.3.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* update changelogs and bump uint (paritytech#486)

* update changelogs and bump uint

* update ethereum-types changelog

* update uint changelog

* tabs

* fixed-hash: bump to 0.7

* bump keccak-hash to 0.6.0

* contract-address: bump keccak-hash to 0.6

* update changelogs after publishing

* bump fs-swap (paritytech#498)

* triehash: patch release (paritytech#499)

* fix clippy warning (paritytech#504)

* add a test for paritytech#507 (paritytech#508)

* add a test for paritytech#507

* CI: test uint on a big-endian platform

* a workaround for gmp

* grumbles

* bump byteorder to 1.4.2

* ethereum-types: fix wasm builds for serialize feature (paritytech#503)

* ethbloom: do not pull std for 'serialize' feature

* ethereum-types: do not pull std for 'serialize' feature

* CI: check wasm builds for ethbloom and ethereum-types

* fix wasm target

* CI: remove redundant check

* CI: fix wasm target install

* update changelogs

* remove parity-runtime (paritytech#511)

* Update codec and crates depending (paritytech#510)

Co-authored-by: Andronik Ordian <write@reusable.software>

Co-authored-by: David <dvdplm@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Artem Vorotnikov <artem@vorotnikov.me>
Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: Frost Red Lee <frostredlee@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Sam Wilson <57262657+SamWilsn@users.noreply.github.com>
Co-authored-by: Qinxuan Chen <koushiro.cqx@gmail.com>
Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
Co-authored-by: honeywest <50997103+honeywest@users.noreply.github.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants