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

perf: lower the default allocation for bignums #4102

Merged
merged 14 commits into from
Jul 12, 2023
Merged

Conversation

ggreif
Copy link
Contributor

@ggreif ggreif commented Jul 5, 2023

this addresses the most pressing aspect of #4100, but more improvements are to come

Possible flanking measures:

  • if the HP still points at the end of the recently allocated bignum after all arithmetic is done, and the bignum's capacity is bigger than its payload, we can cut back the HP (this probably only makes sense from specific callsites)
  • shrink bignums in the GC — this has been backed out in 3b0eb10, as more work is needed
  • Candid conversions for bignums seem cycle-hungry

@@ -921,6 +921,10 @@ pub(crate) unsafe fn block_size(address: usize) -> Words<u32> {

TAG_BIGINT => {
let bigint = address as *mut BigInt;
if (*bigint).mp_int.used != (*bigint).mp_int.alloc {
println!(100, "used:{}, alloc: {}", (*bigint).mp_int.used, (*bigint).mp_int.alloc);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Building up the arr (wins back 1-2 words)

debug.print: used:3, alloc: 5
debug.print: used:5, alloc: 7
debug.print: used:9, alloc: 11
debug.print: used:18, alloc: 19
debug.print: used:36, alloc: 37
debug.print: used:72, alloc: 73
debug.print: used:143, alloc: 145
debug.print: used:286, alloc: 287
debug.print: used:572, alloc: 573

Deserialising from Candid blob (wins back 1 word consistently)

debug.print: used:3, alloc: 4
debug.print: used:5, alloc: 6
debug.print: used:9, alloc: 10
debug.print: used:18, alloc: 19
debug.print: used:36, alloc: 37
debug.print: used:72, alloc: 73
debug.print: used:143, alloc: 144
debug.print: used:286, alloc: 287
debug.print: used:572, alloc: 573

rts/motoko-rts/src/types.rs Outdated Show resolved Hide resolved
if (*bigint).mp_int.used != (*bigint).mp_int.alloc {
println!(100, "used:{}, alloc: {}", (*bigint).mp_int.used, (*bigint).mp_int.alloc);
}
(*bigint).mp_int.alloc = (*bigint).mp_int.used;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ggreif ggreif requested a review from crusso July 5, 2023 15:17
@ggreif ggreif force-pushed the gabor/bignum-space branch from 7185dc5 to d4a6d4a Compare July 6, 2023 07:44
@ggreif ggreif force-pushed the gabor/bignum-space branch from d4a6d4a to 50420e0 Compare July 6, 2023 12:52
luc-blaeser and others added 2 commits July 6, 2023 18:09
But probably better to redesign the logic and make BigInt shortening independent of `word_size()`
@@ -51,6 +51,7 @@ unsafe fn mp_alloc<M: Memory>(mem: &mut M, size: Bytes<u32>) -> *mut u8 {
let size = size.as_usize();
debug_assert_eq!((size % core::mem::size_of::<mp_digit>()), 0);
(*blob).mp_int.alloc = (size / core::mem::size_of::<mp_digit>()) as i32;
(*blob).mp_int.used = (*blob).mp_int.alloc; // Otherwise, `block_size() in `allocation_barrier()` may already shorten the object.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with this might be that this changes the value of the bignum (mantissa).

@ggreif ggreif added the opportunity More optimisation opportunities inside label Jul 11, 2023
@ggreif ggreif marked this pull request as ready for review July 11, 2023 10:30
@ggreif ggreif requested a review from nomeata July 11, 2023 11:52
@github-actions
Copy link

Comparing from 9f6a8f2 to d61ddee:
In terms of gas, 3 tests improved and the mean change is -1.5%.
In terms of size, no changes are observed in 5 tests.

Copy link
Contributor

@luc-blaeser luc-blaeser left a comment

Choose a reason for hiding this comment

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

Thank you very much. This already improves the BigNum size overhead problem substantially.

@ggreif ggreif added the automerge-squash When ready, merge (using squash) label Jul 12, 2023
@mergify mergify bot merged commit 3012773 into master Jul 12, 2023
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Jul 12, 2023
@mergify mergify bot deleted the gabor/bignum-space branch July 12, 2023 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
opportunity More optimisation opportunities inside
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants