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

RTS: fix pages calculation in grow_memory #2633

Merged
merged 3 commits into from
Jun 30, 2021
Merged

Commits on Jun 30, 2021

  1. RTS: fix pages calculation in grow_memory

    Originally discussed here: #2573 (comment)
    
    Previously grow_memory would allocate one more page than necessary when
    the argument is a multiple of Wasm page size. New calculation fixes
    that. The code is basically
    
        (ptr + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE
    
    I.e. divide argument by WASM_PAGE_SIZE, round up. However we do this in
    u64 to avoid overflows in `ptr` is larger than `u32::MAX -
    WASM_PAGE_SIZE + 1`
    
    Diff of mo-rts.wasm before and after:
    
        --- mo-rts.wat  2021-06-30 08:50:10.319312072 +0300
        +++ ../../motoko_2/rts/mo-rts.wat       2021-06-30 08:50:08.507322577 +0300
        @@ -18760,12 +18760,14 @@
           (func $motoko_rts::alloc::alloc_impl::grow_memory::hd1a4f99ad46d13e9 (type 7) (param i32)
             block  ;; label = @1
               local.get 0
        -      i32.const 16
        -      i32.shr_u
        +      i64.extend_i32_u
        +      i64.const 65535
        +      i64.add
        +      i64.const 16
        +      i64.shr_u
        +      i32.wrap_i64
               memory.size
               i32.sub
        -      i32.const 1
        -      i32.add
               local.tee 0
               i32.const 1
               i32.lt_s
    
    So two extra instructions.
    
    CanCan backend benchmark reports 0.004% increase in cycles (42,055,656
    to 42,057,358, +1,702).
    osa1 committed Jun 30, 2021
    Configuration menu
    Copy the full SHA
    d6b9c42 View commit details
    Browse the repository at this point in the history
  2. RTS: Slightly improve grow_memory performance

    Improves cycles in a benchmark from 42,057,358 to 42,056,507 (-0.002%),
    moving `total_pages - current_pages` to the slow path.
    osa1 committed Jun 30, 2021
    Configuration menu
    Copy the full SHA
    8a563a1 View commit details
    Browse the repository at this point in the history
  3. Update rts/motoko-rts/src/alloc/gc.rs

    Co-authored-by: Claudio Russo <claudio@dfinity.org>
    osa1 and crusso authored Jun 30, 2021
    Configuration menu
    Copy the full SHA
    0039cfa View commit details
    Browse the repository at this point in the history