Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/Common/Core/CellArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,16 @@ function vtkCellArray(publicAPI, model) {
cellPointIds = cell;
}
const cellId = publicAPI.getNumberOfCells();
publicAPI.insertNextTuples([cellPointIds.length, ...cellPointIds]);
// Build the [count, ...pointIds] record without the spread operator, which
// would walk the iterator protocol and allocate an extra intermediate for
// every inserted cell.
const numberOfPoints = cellPointIds.length;
const cellRecord = new Array(numberOfPoints + 1);
cellRecord[0] = numberOfPoints;
for (let i = 0; i < numberOfPoints; ++i) {
cellRecord[i + 1] = cellPointIds[i];
}
publicAPI.insertNextTuples(cellRecord);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following is faster and shorter:

const cellTuple = new Uint32Array(numberOfPoints + 1);
cellTuple[0] = numberOfPoints;
cellTuple.set(cellPointIds, 1); // bulk copy, faster than a loop

@daker daker Jun 24, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Uint32Array(numberOfPoints +1) zero fills the buffer first, then .set() overwrites it, i benchmarked this Array + forloop is faster

Chrome:
image

Firefox
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.
Did you try on Chrome or Firefox?
How about:
const cellTuple = Array.from(cellPointIds).unshift(numberOfPoints) ?

@daker daker Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmark.html

Chrome:
image

FF:
image

Edge:
image

// By computing the number of cells earlier, we made sure that numberOfCells is defined
++model.numberOfCells;
if (model.cellSizes != null) {
Expand Down
13 changes: 7 additions & 6 deletions Sources/Common/DataModel/CellLinks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ function vtkCellLinks(publicAPI, model) {
* will be built.
*/
publicAPI.allocate = (numLinks, ext = 1000) => {
model.array = Array(numLinks)
.fill()
.map(() => ({
ncells: 0,
cells: null,
}));
// Populate in a single pass. Array(n).fill().map() walks the array three
// times (allocate sparse, fill, map) before producing the link objects.
const array = new Array(numLinks);
for (let i = 0; i < numLinks; ++i) {
array[i] = { ncells: 0, cells: null };
}
model.array = array;
model.extend = ext;
model.maxId = -1;
};
Expand Down
Loading
Loading