-
Notifications
You must be signed in to change notification settings - Fork 549
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
Field: More efficient conversion into Bignum_bigint.t #14599
Merged
deepthiskumar
merged 6 commits into
MinaProtocol:rampup
from
openmina:optimization/efficient-bignum-bigint-conv
Dec 4, 2023
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3022675
When comparing `Field.t` values, avoid the conversion to bigint
tizoc 5651bce
Use `into_repr()` when comparing fields in Rust to ensure compatibili…
tizoc 6fcd939
Field: More efficient conversion of into Bignum_bigint.t
tizoc b570c8f
Do not use binprot serialization when converting bigint fields, use Z…
tizoc baa6986
Merge branch 'rampup' into optimization/efficient-bignum-bigint-conv
deepthiskumar 498eb98
Merge branch 'rampup' into optimization/efficient-bignum-bigint-conv
deepthiskumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ module type Input_intf = sig | |
|
||
val is_square : t -> bool | ||
|
||
val compare : t -> t -> int | ||
|
||
val equal : t -> t -> bool | ||
|
||
val print : t -> unit | ||
|
@@ -177,24 +179,34 @@ module Make (F : Input_intf) : | |
let of_sexpable = of_bigint | ||
end) | ||
|
||
let zero = of_int 0 | ||
|
||
let to_bignum_bigint n = | ||
let rec go i two_to_the_i acc = | ||
if Int.equal i size_in_bits then acc | ||
else | ||
let acc' = | ||
if Bigint.test_bit n i then Bignum_bigint.(acc + two_to_the_i) | ||
else acc | ||
in | ||
go (i + 1) Bignum_bigint.(two_to_the_i + two_to_the_i) acc' | ||
in | ||
go 0 Bignum_bigint.one Bignum_bigint.zero | ||
|
||
let hash_fold_t s x = | ||
Bignum_bigint.hash_fold_t s (to_bignum_bigint (to_bigint x)) | ||
(* For non-zero values, conversion is done by creating the bin_prot representation | ||
of the [Bignum_bigit.t] value, and then parsing it with bin_prot. *) | ||
match compare n zero with | ||
| 0 -> | ||
Bignum_bigint.zero | ||
| c -> | ||
(* Tag for positive values is 1, negative is 2: | ||
https://github.com/janestreet/bignum/blob/6c63419787a4e209e85befd3d823fff2790677e0/bigint/src/bigint.ml#L27-L30 *) | ||
let tag_byte = if c > 0 then '\x01' else '\x02' in | ||
let bytes = to_bytes n in | ||
let len = Bytes.length bytes in | ||
let size_byte = Char.of_int_exn len in | ||
let buf = Bytes.create (2 + len) in | ||
(* First byte is the tag, second the amount of bytes *) | ||
Bytes.unsafe_set buf 0 tag_byte ; | ||
Bytes.unsafe_set buf 1 size_byte ; | ||
(* Copy the bytes representation of the value, skip the tag and size bytes *) | ||
Bytes.unsafe_blit ~src:bytes ~src_pos:0 ~dst_pos:2 ~len ~dst:buf ; | ||
Bin_prot.Reader.of_bytes Bignum_bigint.Stable.V1.bin_reader_t buf | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not use |
||
|
||
let hash_fold_t s x = Bignum_bigint.hash_fold_t s (to_bignum_bigint x) | ||
|
||
let hash = Hash.of_fold hash_fold_t | ||
|
||
let compare t1 t2 = Bigint.compare (to_bigint t1) (to_bigint t2) | ||
let compare = compare | ||
|
||
let equal = equal | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this necessary? The bigint representation will always be positive, so it should be fine (and actually more efficient) to just special-case zero using an equality check.
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.
@mrmr1993 not at all, the only purpose of all this logic is to:
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.
@mrmr1993 btw, are there any values that are common and are worth short-circuiting like zero? do you think it is worth adding
one
too ?