Skip to content

Commit

Permalink
Refactor makeHEAPView to use getHeapOffset. NFC (#21332)
Browse files Browse the repository at this point in the history
The code for calculating the offsets was being duplicated here.

Also, optimize getHeapOffset to avoid any shifting when size is 1.
  • Loading branch information
sbc100 authored Feb 13, 2024
1 parent 81bf014 commit 24697df
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 42 deletions.
44 changes: 26 additions & 18 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,17 @@ function getNativeTypeSize(type) {

function getHeapOffset(offset, type) {
const sz = getNativeTypeSize(type);
const shifts = Math.log(sz) / Math.LN2;
if (sz == 1) {
return offset;
}
if (MEMORY64 == 1) {
return `((${offset})/${2 ** shifts})`;
} else if (CAN_ADDRESS_2GB) {
return `((${offset})/${sz})`;
}
const shifts = Math.log(sz) / Math.LN2;
if (CAN_ADDRESS_2GB) {
return `((${offset})>>>${shifts})`;
} else {
return `((${offset})>>${shifts})`;
}
return `((${offset})>>${shifts})`;
}

function ensureDot(value) {
Expand Down Expand Up @@ -427,19 +430,24 @@ function makeSetValueImpl(ptr, pos, value, type) {
}

function makeHEAPView(which, start, end) {
const size = parseInt(which.replace('U', '').replace('F', '')) / 8;
const shift = Math.log2(size);
let mod = '';
if (size != 1) {
if (MEMORY64) {
mod = '/' + size;
} else if (CAN_ADDRESS_2GB) {
mod = '>>>' + shift;
} else {
mod = '>>' + shift;
}
}
return `HEAP${which}.subarray((${start})${mod}, (${end})${mod})`;
// The makeHEAPView, for legacy reasons, takes a heap "suffix"
// rather than the heap "type" that used by other APIs here.
const type = {
'8': 'i8',
'U8': 'u8',
'16': 'i16',
'U16': 'u16',
'32': 'i32',
'U32': 'u32',
'64': 'i64',
'U64': 'u64',
'F32': 'float',
'F64': 'double',
}[which];
const heap = getHeapForType(type);
start = getHeapOffset(start, type);
end = getHeapOffset(end, type);
return `${heap}.subarray((${start}), ${end})`;
}

// Given two values and an operation, returns the result of that operation.
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.js": 7086,
"a.js.gz": 3000,
"a.js": 7080,
"a.js.gz": 2999,
"a.wasm": 11433,
"a.wasm.gz": 5725,
"total": 19192,
"total_gz": 9156
"total": 19186,
"total_gz": 9155
}
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9959
9953
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24516
24510
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9944
9939
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24484
24478
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11032
11034
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28406
28382
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9925
9921
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24409
24403
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28407
28383
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9959
9953
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24516
24510
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5250
5245
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12128
12122
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2448
2445
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5212
5206
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2415
2411
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5142
5136
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
58128
58100
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31591
31563
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
57039
57011

0 comments on commit 24697df

Please sign in to comment.