-
Notifications
You must be signed in to change notification settings - Fork 17
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
Added serde
feature and support
#15
Conversation
@gakonst - Got around to taking some time to add Also, I tried to address your comment from #7 (comment) by adding convenience methods for doing the parsing both |
Some(result) => result, | ||
None => return Err(pie(PosOverflow)), | ||
|
||
if can_not_overflow::<T>(radix, is_signed_ty, digits) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ported from latest more optimized from_str_radix
implementation from the Rust standard library: https://github.com/rust-lang/rust/blob/7f9e013ba62e7bf77a95ba29bcb2c5740b7b2059/library/core/src/num/mod.rs#L1067
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Lgtm. What are the highest leverage places where we can use this to achieve speedup over primitive_types/ethereum_types?
cc @rakita in case interesting for revm
I checked it when I was looking to find fast ethnum-rs/src/intrinsics/native/divmod.rs Line 166 in c6ad614
maybe it can be backported to here as it is widely used algo. There is additional speedup from knuth algo that evmone uses from gmplib. This is a paper: https://gmplib.org/~tege/division-paper.pdf but that seems harder to backport zkp_u256 is one other that implemented knuth but it had a little better comments there: https://github.com/0xProject/OpenZKP/blob/c28a5c66b6ee9b97bf177373ba148981df60b7fb/algebra/u256/src/arch/generic/knuth_division.rs#L96 |
Awesome! That is a lot of great resources to look at. |
@rakita - so one trouble I have with benchmarking division is that there is a huge variation on performance depending on inputs. I re-ran the benchmarks against How to run the benchmarks:# First create a `primitive-types` baseline to compare against
cargo bench -p ethnum-bench --features primitive-types -- --save-baseline p div
# Now run the benchmarks comparing against the baseline
cargo bench -p ethnum-bench -- --baseline p div Benchmark results on my early 2013 MBP:
To me these results signal that the implementation here deals better with the case where the divisor fits within a |
That sounds about right, Knuth is fastest for big ones. hm I didn't check earlier but this is something similar that intx (evmone) is doing: https://github.com/chfast/intx/blob/de3388d75bda494972c4dceb22ccd1b0f74e1e97/include/intx/intx.hpp#L1788-L1823 |
It looks like it. From what I understood, It has fast paths for division where the divisor is less than The code in |
This PR adds
serde
feature to theethnum
crate for addingserde
serialization and deserialization support to both signed and unsigned integer types. The default serialization implementation encodes/decodes0x
-prefixed hexadecimal strings.Additionally, new convenience conversion methods were added for parsing integers from optionally prefixed strings (so parsing
0x2a
and42
to the same value) as well as modules for use with#[serde(with = "...")]
for much the same purpose.For example:
would allow serializing the following JSON documents:
Finally, the hex, octal and binary formatting implementation supports the
-
formatting flag now. For rust standard integer types:This means, there is no way to print a sign when formatting in a non-decimal radix. So for
I256
, support was added so that you can write:Note that the
{:#x}
format specifier still behaves the same way as they do for primitive integer types (i.e. sign-extended 2's complement).