feat(DataArray): add preserveTypedArrays option to getState()#3472
Open
PaulHax wants to merge 1 commit intoKitware:masterfrom
Open
feat(DataArray): add preserveTypedArrays option to getState()#3472PaulHax wants to merge 1 commit intoKitware:masterfrom
PaulHax wants to merge 1 commit intoKitware:masterfrom
Conversation
finetjul
reviewed
Apr 7, 2026
finetjul
reviewed
Apr 7, 2026
getState({ preserveTypedArrays: true }) preserves TypedArray values
without converting and copying to plain Arrays. This avoids
out-of-memory crashes for large data arrays (e.g. 268M-element
labelmaps where Array.from() would allocate ~1.6GB of boxed Numbers).
The default behavior is unchanged — getState() without options still
returns JSON-safe plain Arrays.
d23a17b to
f123de2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
vtkDataArray.getState()callsArray.from()on typed array values, which creates a plain JS Array with Numbers. For large arrays this causes an out-of-memory crash (RangeError: Invalid array length).Real-world case: Kitware/VolView#852 — saving a session with a 1024×1024×256 labelmap (268M Uint8 elements) crashes because
Array.from()tries to allocate ~1.6GB of heap for the boxed Number array.The
Array.from()conversion in the basegetState()was added in #2927 to fix JSON serialization ofFloat64Arrayproperties likedirection(Kitware/glance#480).Related: see this Discourse thread on web workers and this thread on REST API serialization. The
preserveTypedArraysoption formalizes that pattern in the existing API.If this gets merged, should open another breaking change PR to remove
getStateArrayMapFuncfrommacros.js.Results
Before: No built-in way to serialize DataArrays without copying TypedArrays to plain Arrays.
After:
getState({ preserveTypedArrays: true })preserves TypedArray values without converting and copying. DefaultgetState()behavior is unchanged.Changes
macros.js: AddedpreserveTypedArraysoption to the basegetState(). When true, skipsArray.from()on TypedArrays and propagates the option to nested objects.DataArray/index.js: UpdatedgetState()to conditionally skipArray.from()on values whenpreserveTypedArraysis true.FieldData.js: UpdatedgetState()to forward options to nested DataArrays.interfaces.d.ts: AddedGetStateOptionstype and updatedgetState()signature.DataArray/index.d.ts: UpdatedgetState()signature.testDataArray.js: Added tests forpreserveTypedArraysoption.Documentation and TypeScript definitions were updated to match those changes
PR and Code Checklist
npm run reformatto have correctly formatted codeTesting