-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: keep the heap pointer in a dedicated Wasm global (#4064)
We now optimise the bump pointer allocator by keeping the heap pointer in a Wasm global, so that the Motoko side can easily access it. This opens up the possibility that for fixed-size allocations we can simply examine a few lower bits (comparing to zero) in order to figure out that a page-crossing has occurred. If so, we only need to "commit" the new page by calling `alloc_words 0`. For the Rust side accessor functions to the `HP` are provided. ### How it works We construct a mask at compile-time to detect page boundary crossings of the bumped pointer: - for a (byte) increment `0b1xxxxx` (where the `xxxxx` part is arbitrary) - and `< 0x8000` (so that the bit 15 is clear, when the LSBit is the 0th) - we treat this as a 16-bit unsigned integer - we count its leading zeros (will be `>= 1` and `< 16`), this is `N`. - the 32-bit HP mask the will be `0b000000000000000011..100..0 : u32`, where there are 16 upper zeros, then `N` ones, closing with `16-N` zeros - the bumped HP is masked with said number, and when zero, then the addition that happened while bumping the HP caused a rippling carry, and the page boundary has been crossed - in this case we have to make sure that the new page is backed physically, so we call `Heap.alloc 0` (and discard the result). Some benchmarks give cycle savings of 10% 😵💫💪! _Note_: Currently the change is only active for _classical GC_, the incremental part will be tackled in #4078. Further benefits: - the accessor functions for HP (that now live on the `codegen` side) can be inlined by `wasm-opt` - looks like `wasm-opt` (`-O3`) also specialises `call alloc_words 0; drop` to a very minimal kernel, speeding up the cold path too. ## Potential future optimisations: - keep HP as always _skewed_, then no more per-alloc `sub 1` is needed - extend the mask to the right by one bit — https://github.com/dfinity/motoko/pull/4064/files/4eb3dfeb7fdc10737605c631edff33b3f0976eff#r1250623654
- Loading branch information
Showing
10 changed files
with
108 additions
and
38 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (+268_435_456, 2_818_724_162) | ||
debug.print: (+268_435_456, 2_533_732_672) | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (+268_435_456, 2_818_572_610) | ||
debug.print: (+268_435_456, 2_533_581_120) | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (+268_435_456, 2_818_572_610) | ||
debug.print: (+268_435_456, 2_533_581_120) | ||
ingress Completed: Reply: 0x4449444c0000 |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (50_227, +91_377_860, 1_435_538_669) | ||
debug.print: (50_070, +102_586_000, 1_507_623_284) | ||
debug.print: (50_227, +91_377_860, 1_433_012_379) | ||
debug.print: (50_070, +102_586_000, 1_504_942_528) | ||
ingress Completed: Reply: 0x4449444c0000 |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (true, +1_188, 13_165) | ||
debug.print: (false, +1_188, 12_450) | ||
debug.print: (false, +1_188, 13_155) | ||
debug.print: (true, +868, 12_841) | ||
debug.print: (false, +868, 11_660) | ||
debug.print: (false, +868, 12_800) | ||
debug.print: (true, +1_188, 11_988) | ||
debug.print: (false, +1_188, 11_273) | ||
debug.print: (false, +1_188, 11_978) | ||
debug.print: (true, +868, 11_936) | ||
debug.print: (false, +868, 10_755) | ||
debug.print: (false, +868, 11_895) | ||
ingress Completed: Reply: 0x4449444c0000 |