Skip to content

Commit 3f309a0

Browse files
authored
Merge pull request #76 from CesiumGS/main
Optimize floatVectorToFloatArray for performance improvements
2 parents 169f943 + 08629c9 commit 3f309a0

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

packages/core/lib/spz-wasm/cppBufferUtil.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ import type { MainModule, VectorFloat32, VectorUInt8T } from "./build/main";
44
* create new Float32Array from cpp FloatVector
55
* @param wasmModule emscripten main module
66
* @param vec cpp float32 vector
7+
* @param enhancementFunc function accepting a number and returning a number to perform processing
78
* @returns copied float32 array
89
*/
910
export const floatVectorToFloatArray = (
1011
wasmModule: MainModule,
1112
vec: VectorFloat32,
12-
enhancementFunc: (n: number) => number = (n) => n,
13+
enhancementFunc?: (n: number) => number,
1314
): Float32Array => {
1415
const pointer = wasmModule.vf32_ptr(vec);
1516
const size = vec.size();
17+
const floatOffset = pointer >> 2;
1618

17-
const buffer = new Float32Array(wasmModule.HEAPF32.buffer, pointer, size);
18-
const copiedBuffer = buffer.map(enhancementFunc);
19+
const copiedBuffer = wasmModule.HEAPF32.slice(
20+
floatOffset,
21+
floatOffset + size,
22+
);
23+
24+
if (enhancementFunc !== undefined) {
25+
for (let i = 0; i < size; i++) {
26+
copiedBuffer[i] = enhancementFunc(copiedBuffer[i]);
27+
}
28+
}
1929

2030
return copiedBuffer;
2131
};

0 commit comments

Comments
 (0)