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
1 change: 1 addition & 0 deletions rts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ TOMMATH_FLAGS = \
-DMP_FREE=mp_free \
-DMP_MEMSET=0 \
-DMP_FIXED_CUTOFFS \
-DMP_PREC=4 \
-DMP_NO_FILE \
-D__STDC_IEC_559__ \

Expand Down
7 changes: 6 additions & 1 deletion rts/motoko-rts/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).

allocation_barrier(ptr);
blob.payload_addr() as *mut u8
}
Expand Down Expand Up @@ -169,7 +170,11 @@ unsafe fn persist_bigint(i: mp_int) -> Value {
}
let r = BigInt::from_payload(i.dp);
if (*r).mp_int.alloc != i.alloc {
panic!("persist_bigint: alloc changed?");
panic!(
"persist_bigint: alloc changed? heap {} stack {}",
(*r).mp_int.alloc,
i.alloc
);
}
(*r).mp_int = i;
Value::from_ptr(r as usize)
Expand Down
1 change: 1 addition & 0 deletions rts/motoko-rts/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ pub(crate) unsafe fn block_size(address: usize) -> Words<u32> {

TAG_BIGINT => {
let bigint = address as *mut BigInt;
(*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.

size_of::<BigInt>() + bigint.len().to_words()
}

Expand Down
37 changes: 37 additions & 0 deletions test/bench/bignum.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//MOC-FLAG --force-gc
import { Array_tabulate; performanceCounter; rts_heap_size; debugPrint } = "mo:⛔";

actor Bignum {
var steps = 0;
var nat = 3;

func counters() : (Int, Nat64) = (rts_heap_size(), performanceCounter(0));

let arr : [var Nat] = [var 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var arr1 : ?[Nat] = null;

public func go() : async () {
let (size0, perf0) = counters();

while (steps < 12) {
nat := nat * 5 * nat;
arr[steps] := nat;
steps := steps + 1
};

let (size1, perf1) = counters();

debugPrint(debug_show {size = size1 - size0; cycles = perf1 - perf0 });
};

public func candid() : async () {
let (size0, perf0) = counters();
let ser = to_candid(Array_tabulate<Nat>(arr.size(), func i = arr[i]));
arr1 := from_candid(ser);
let (size1, perf1) = counters();
debugPrint(debug_show {size = size1 - size0; cycles = perf1 - perf0 });
}
}

//CALL ingress go 0x4449444C0000
//CALL ingress candid 0x4449444C0000
6 changes: 6 additions & 0 deletions test/bench/ok/bignum.drun-run.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101
ingress Completed: Reply: 0x4449444c0000
debug.print: {cycles = 2_494_559; size = +59_652}
ingress Completed: Reply: 0x4449444c0000
debug.print: {cycles = 103_059_688; size = +1_817_872}
ingress Completed: Reply: 0x4449444c0000
4 changes: 2 additions & 2 deletions test/bench/ok/heap-32.drun-run.ok
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101
ingress Completed: Reply: 0x4449444c0000
debug.print: (50_227, +91_377_860, 1_434_593_325)
debug.print: (50_070, +102_586_000, 1_506_674_763)
debug.print: (50_227, +30_261_252, 676_332_164)
debug.print: (50_070, +32_992_212, 730_267_012)
ingress Completed: Reply: 0x4449444c0000