Do not copy data in readArray when possible#70
Open
remmel wants to merge 1 commit intoimage-js:mainfrom
Open
Conversation
Member
|
Hello! Thank you for the pull request (and sorry it took a bit long to answer you). It seems indeed useful to have this optimization available, but we'd like to ask you to implement it behind an option.
Proposed signature: /**
* Creates an array of corresponding to the type `type` and size `size`.
* For example type `uint8` will create a `Uint8Array`.
* @param size - size of the resulting array
* @param type - number type of elements to read
* @param as - Whether to copy the read data or return a view on the original ArrayBuffer. Note that in certain cases such as unaligned offsets, a view cannot be created and it will copy regardless of this option.
*/
public readArray<T extends keyof typeof typedArrays>(
size: number,
type: T,
as: 'copy' | 'view' = 'copy',
): InstanceType<TypedArrays[T]> |
Author
|
Hi! You're right, I didn't though about the 2nd edge case (reformulated: let's say we want to keep forever a subarray of a huge array, with my PR it will keep everything without duplicating/copying the subarray and without my PR it will keep only the duplicated/copied array) |
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.
To avoid copying data (using slice), the TypedArray can be created with the arrayBuffer. However it handles only when start offset is a multiple of its type bytes occupancy (otherwise it will trigger similar error :
start offset of Int16Array should be a multiple of 2). I'll use it when it is possible.However, since the data is not longer copied, this could have bad consequences if a depend library modified the read data.
I also move line
const slice = this.buffer.slice(offset, offset + bytes);as in some case the variable is not used (when entering right after in the if condition).Note that I'm using similar code https://github.com/remmel/volograms-js/blob/92e3166cebe1dcb823291c11ec50dfb226d7dc99/src/BinaryReader.js#L68