Skip to content

Commit

Permalink
fix: make digest64 5x slower
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Mar 1, 2024
1 parent 2a9171f commit eef3955
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 167 deletions.
1 change: 1 addition & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
branches:
- master
- benchmark # For debugging
- tuyen/chain_w_computation_test
pull_request:
branches:
- master
Expand Down
24 changes: 13 additions & 11 deletions packages/as-sha256/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,17 @@ export function digest(length: i32): void {
}

export function digest64(inPtr: usize, outPtr: usize): void {
init();
hashBlocks(wPtr,inPtr);
hashPreCompW(w64Ptr);
store32(outPtr, 0, bswap(H0));
store32(outPtr, 1, bswap(H1));
store32(outPtr, 2, bswap(H2));
store32(outPtr, 3, bswap(H3));
store32(outPtr, 4, bswap(H4));
store32(outPtr, 5, bswap(H5));
store32(outPtr, 6, bswap(H6));
store32(outPtr, 7, bswap(H7));
for (let i = 0; i < 5; i++) {
init();
hashBlocks(wPtr,inPtr);
hashPreCompW(w64Ptr);
store32(outPtr, 0, bswap(H0));
store32(outPtr, 1, bswap(H1));
store32(outPtr, 2, bswap(H2));
store32(outPtr, 3, bswap(H3));
store32(outPtr, 4, bswap(H4));
store32(outPtr, 5, bswap(H5));
store32(outPtr, 6, bswap(H6));
store32(outPtr, 7, bswap(H7));
}
}
Binary file modified packages/as-sha256/build/optimized.wasm
Binary file not shown.
89 changes: 51 additions & 38 deletions packages/as-sha256/build/optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1541,44 +1541,57 @@
global.set $assembly/index/H7
)
(func $assembly/index/digest64 (param $0 i32) (param $1 i32)
call $assembly/index/init
global.get $assembly/index/wPtr
local.get $0
call $assembly/index/hashBlocks
global.get $assembly/index/w64Ptr
call $assembly/index/hashPreCompW
local.get $1
global.get $assembly/index/H0
call $~lib/polyfills/bswap<i32>
i32.store
local.get $1
global.get $assembly/index/H1
call $~lib/polyfills/bswap<i32>
i32.store offset=4
local.get $1
global.get $assembly/index/H2
call $~lib/polyfills/bswap<i32>
i32.store offset=8
local.get $1
global.get $assembly/index/H3
call $~lib/polyfills/bswap<i32>
i32.store offset=12
local.get $1
global.get $assembly/index/H4
call $~lib/polyfills/bswap<i32>
i32.store offset=16
local.get $1
global.get $assembly/index/H5
call $~lib/polyfills/bswap<i32>
i32.store offset=20
local.get $1
global.get $assembly/index/H6
call $~lib/polyfills/bswap<i32>
i32.store offset=24
local.get $1
global.get $assembly/index/H7
call $~lib/polyfills/bswap<i32>
i32.store offset=28
(local $2 i32)
loop $for-loop|0
local.get $2
i32.const 5
i32.lt_s
if
call $assembly/index/init
global.get $assembly/index/wPtr
local.get $0
call $assembly/index/hashBlocks
global.get $assembly/index/w64Ptr
call $assembly/index/hashPreCompW
local.get $1
global.get $assembly/index/H0
call $~lib/polyfills/bswap<i32>
i32.store
local.get $1
global.get $assembly/index/H1
call $~lib/polyfills/bswap<i32>
i32.store offset=4
local.get $1
global.get $assembly/index/H2
call $~lib/polyfills/bswap<i32>
i32.store offset=8
local.get $1
global.get $assembly/index/H3
call $~lib/polyfills/bswap<i32>
i32.store offset=12
local.get $1
global.get $assembly/index/H4
call $~lib/polyfills/bswap<i32>
i32.store offset=16
local.get $1
global.get $assembly/index/H5
call $~lib/polyfills/bswap<i32>
i32.store offset=20
local.get $1
global.get $assembly/index/H6
call $~lib/polyfills/bswap<i32>
i32.store offset=24
local.get $1
global.get $assembly/index/H7
call $~lib/polyfills/bswap<i32>
i32.store offset=28
local.get $2
i32.const 1
i32.add
local.set $2
br $for-loop|0
end
end
)
(func $~start
i32.const 1348
Expand Down
Binary file modified packages/as-sha256/build/untouched.wasm
Binary file not shown.
249 changes: 132 additions & 117 deletions packages/as-sha256/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2849,124 +2849,139 @@
(local $23 i32)
(local $24 i32)
(local $25 i32)
call $assembly/index/init
global.get $assembly/index/wPtr
local.get $0
call $assembly/index/hashBlocks
global.get $assembly/index/w64Ptr
call $assembly/index/hashPreCompW
local.get $1
local.set $2
(local $26 i32)
i32.const 0
local.set $3
global.get $assembly/index/H0
call $~lib/polyfills/bswap<u32>
local.set $4
local.get $2
local.get $3
i32.const 2
i32.shl
i32.add
local.get $4
i32.store
local.get $1
local.set $5
i32.const 1
local.set $6
global.get $assembly/index/H1
call $~lib/polyfills/bswap<u32>
local.set $7
local.get $5
local.get $6
i32.const 2
i32.shl
i32.add
local.get $7
i32.store
local.get $1
local.set $8
i32.const 2
local.set $9
global.get $assembly/index/H2
call $~lib/polyfills/bswap<u32>
local.set $10
local.get $8
local.get $9
i32.const 2
i32.shl
i32.add
local.get $10
i32.store
local.get $1
local.set $11
i32.const 3
local.set $12
global.get $assembly/index/H3
call $~lib/polyfills/bswap<u32>
local.set $13
local.get $11
local.get $12
i32.const 2
i32.shl
i32.add
local.get $13
i32.store
local.get $1
local.set $14
i32.const 4
local.set $15
global.get $assembly/index/H4
call $~lib/polyfills/bswap<u32>
local.set $16
local.get $14
local.get $15
i32.const 2
i32.shl
i32.add
local.get $16
i32.store
local.get $1
local.set $17
i32.const 5
local.set $18
global.get $assembly/index/H5
call $~lib/polyfills/bswap<u32>
local.set $19
local.get $17
local.get $18
i32.const 2
i32.shl
i32.add
local.get $19
i32.store
local.get $1
local.set $20
i32.const 6
local.set $21
global.get $assembly/index/H6
call $~lib/polyfills/bswap<u32>
local.set $22
local.get $20
local.get $21
i32.const 2
i32.shl
i32.add
local.get $22
i32.store
local.get $1
local.set $23
i32.const 7
local.set $24
global.get $assembly/index/H7
call $~lib/polyfills/bswap<u32>
local.set $25
local.get $23
local.get $24
i32.const 2
i32.shl
i32.add
local.get $25
i32.store
local.set $2
loop $for-loop|0
local.get $2
i32.const 5
i32.lt_s
if
call $assembly/index/init
global.get $assembly/index/wPtr
local.get $0
call $assembly/index/hashBlocks
global.get $assembly/index/w64Ptr
call $assembly/index/hashPreCompW
local.get $1
local.set $3
i32.const 0
local.set $4
global.get $assembly/index/H0
call $~lib/polyfills/bswap<u32>
local.set $5
local.get $3
local.get $4
i32.const 2
i32.shl
i32.add
local.get $5
i32.store
local.get $1
local.set $6
i32.const 1
local.set $7
global.get $assembly/index/H1
call $~lib/polyfills/bswap<u32>
local.set $8
local.get $6
local.get $7
i32.const 2
i32.shl
i32.add
local.get $8
i32.store
local.get $1
local.set $9
i32.const 2
local.set $10
global.get $assembly/index/H2
call $~lib/polyfills/bswap<u32>
local.set $11
local.get $9
local.get $10
i32.const 2
i32.shl
i32.add
local.get $11
i32.store
local.get $1
local.set $12
i32.const 3
local.set $13
global.get $assembly/index/H3
call $~lib/polyfills/bswap<u32>
local.set $14
local.get $12
local.get $13
i32.const 2
i32.shl
i32.add
local.get $14
i32.store
local.get $1
local.set $15
i32.const 4
local.set $16
global.get $assembly/index/H4
call $~lib/polyfills/bswap<u32>
local.set $17
local.get $15
local.get $16
i32.const 2
i32.shl
i32.add
local.get $17
i32.store
local.get $1
local.set $18
i32.const 5
local.set $19
global.get $assembly/index/H5
call $~lib/polyfills/bswap<u32>
local.set $20
local.get $18
local.get $19
i32.const 2
i32.shl
i32.add
local.get $20
i32.store
local.get $1
local.set $21
i32.const 6
local.set $22
global.get $assembly/index/H6
call $~lib/polyfills/bswap<u32>
local.set $23
local.get $21
local.get $22
i32.const 2
i32.shl
i32.add
local.get $23
i32.store
local.get $1
local.set $24
i32.const 7
local.set $25
global.get $assembly/index/H7
call $~lib/polyfills/bswap<u32>
local.set $26
local.get $24
local.get $25
i32.const 2
i32.shl
i32.add
local.get $26
i32.store
local.get $2
i32.const 1
i32.add
local.set $2
br $for-loop|0
end
end
)
(func $~start
call $start:assembly/index
Expand Down
Loading

0 comments on commit eef3955

Please sign in to comment.