-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
perf: math: make Int.Size() faster by computation not len(MarshalledBytes) #16263
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"encoding" | ||
"encoding/json" | ||
"fmt" | ||
stdmath "math" | ||
"math/big" | ||
"strings" | ||
"sync" | ||
|
@@ -421,9 +422,89 @@ func (i *Int) Unmarshal(data []byte) error { | |
} | ||
|
||
// Size implements the gogo proto custom type interface. | ||
func (i *Int) Size() int { | ||
bz, _ := i.Marshal() | ||
return len(bz) | ||
// Reduction power of 10 is the smallest power of 10, than 1<<64-1 | ||
// | ||
// 18446744073709551615 | ||
// | ||
// and the next value fitting with the digits of (1<<64)-1 is: | ||
// | ||
// 10000000000000000000 | ||
var ( | ||
big10Pow19, _ = new(big.Int).SetString("1"+strings.Repeat("0", 19), 10) | ||
log10Of2 = stdmath.Log10(2) | ||
) | ||
|
||
func (i *Int) Size() (size int) { | ||
sign := i.Sign() | ||
if sign == 0 { // It is zero. | ||
// log*(0) is undefined hence return early. | ||
return 1 | ||
} | ||
|
||
ii := i.i | ||
alreadyMadeCopy := false | ||
if sign < 0 { // Negative sign encountered, so consider len("-") | ||
// The reason that we make this comparison in here is to | ||
// allow checking for negatives exactly once, to reduce | ||
// on comparisons inside sizeBigInt, hence we make a copy | ||
// of ii and make it absolute having taken note of the sign | ||
// already. | ||
size++ | ||
// We already accounted for the negative sign above, thus | ||
// we can now compute the length of the absolute value. | ||
ii = new(big.Int).Abs(ii) | ||
alreadyMadeCopy = true | ||
} | ||
|
||
// From here on, we are now dealing with non-0, non-negative values. | ||
return size + sizeBigInt(ii, alreadyMadeCopy) | ||
} | ||
|
||
func sizeBigInt(i *big.Int, alreadyMadeCopy bool) (size int) { | ||
// This code assumes that non-0, non-negative values have been passed in. | ||
bitLen := i.BitLen() | ||
|
||
res := float64(bitLen) * log10Of2 | ||
ires := int(res) | ||
if diff := res - float64(ires); diff == 0.0 { | ||
return size + ires | ||
} else if diff >= 0.3 { // There are other digits past the bitLen, this is a heuristic. | ||
return size + ires + 1 | ||
} | ||
|
||
// Use Log10(x) for values less than (1<<64)-1, given it is only defined for [1, (1<<64)-1] | ||
if bitLen <= 64 { | ||
return size + 1 + int(stdmath.Log10(float64(i.Uint64()))) | ||
} | ||
// Past this point, the value is greater than (1<<64)-1 and 10^19. | ||
|
||
// The prior above computation of i.BitLen() * log10Of2 is inaccurate for powers of 10 | ||
// and values like "9999999999999999999999999999"; that computation always overshoots by 1 | ||
// hence our next alternative is to just go old school and keep dividing the value by: | ||
// 10^19 aka "10000000000000000000" while incrementing size += 19 | ||
|
||
// At this point we should just keep reducing by 10^19 as that's the smallest multiple | ||
// of 10 that matches the digit length of (1<<64)-1 | ||
var ri *big.Int | ||
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 we pass this in as a scratch variable each loop, to avoid re-allocations? 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. ping @odeke-em |
||
if alreadyMadeCopy { | ||
ri = i | ||
} else { | ||
ri = new(big.Int).Set(i) | ||
alreadyMadeCopy = true | ||
} | ||
|
||
for ri.Cmp(big10Pow19) >= 0 { // Keep reducing the value by 10^19 and increment size by 19 | ||
ri = ri.Quo(ri, big10Pow19) | ||
size += 19 | ||
} | ||
|
||
if ri.Sign() == 0 { // if the value is zero, no need for the recursion, just return immediately | ||
return size | ||
} | ||
|
||
// Otherwise we already know how many times we reduced the value, so its | ||
// remnants less than 10^19 and those can be computed by again calling sizeBigInt. | ||
return size + sizeBigInt(ri, alreadyMadeCopy) | ||
} | ||
|
||
// Override Amino binary serialization by proxying to protobuf. | ||
|
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.
I am a bit confused, since this feels like we could solve this with more pre-computation. E.g. for every bitlen, store "markers" in a map for values of that bitlength that corresspond to different sizes.
(And then we'd have 0 alloc's per size call)
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.
Nah, it isn't as simple. I had independently tried this approach out, there are a bunch of numbers which are in the boundary of bit lengths but require you to search between the closest values: and roughly I also checked for the allocations reduction and it wasn't significant. I shall refine it in the future, but for purpose of clear wins this approach as is will work great: the approach to get accuracy is to:
This was the code for my prior experiment which I'll refine after this PR lands: