Skip to content

Commit 3b74187

Browse files
committed
fix(DataArray): avoid reallocation if sufficient size
During call to deepCopy() avoid reallocating the typedArray if the dataType is the same and the array has sufficient size to hold new data.
1 parent 32a1bb4 commit 3b74187

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Sources/Common/Core/DataArray/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,23 @@ function vtkDataArray(publicAPI, model) {
425425
};
426426

427427
publicAPI.deepCopy = (other) => {
428+
// Retain current dataType and array reference before shallowCopy call.
429+
const currentType = publicAPI.getDataType();
430+
const currentArray = model.values;
428431
publicAPI.shallowCopy(other);
429-
publicAPI.setData(other.getData().slice());
432+
433+
// Avoid array reallocation if size already sufficient
434+
// and dataTypes match.
435+
if (
436+
currentArray?.length >= other.getNumberOfValues() &&
437+
currentType === other.getDataType()
438+
) {
439+
currentArray.set(other.getData());
440+
model.values = currentArray;
441+
publicAPI.dataChange();
442+
} else {
443+
publicAPI.setData(other.getData().slice());
444+
}
430445
};
431446

432447
publicAPI.interpolateTuple = (

0 commit comments

Comments
 (0)