dcmjs-org / dcmjs

Javascript implementation of DICOM manipulation
https://dcmjs.netlify.com/
MIT License
292 stars 111 forks source link

Zero Copy ArrayBuffer #279

Closed Ouwen closed 2 years ago

Ouwen commented 2 years ago

This PR changes the ReadBufferStream to not allocate new memory ArrayBuffers when calling .more The idea being that ReadBufferStreams can be treated as a "View" on top of some underlying ArrayBuffer sitting in memory.

Thoughts? :+1: :-1:

pieper commented 2 years ago

From a quick look this seems like a nice improvement, thanks 👍

@Punzo or @hackermd or anyone could have a look to see if there are any implications for your use? Seems like this should improve memory usage on big images.

Punzo commented 2 years ago

I did not tested, but yes for large images definitely this would help.

pieper commented 2 years ago

Do you think we should test before merging? If there's a regression would it cause any problems for you?

Punzo commented 2 years ago

I will test this tomorrow !

Ouwen commented 2 years ago

Probably makes sense to remove the view option since making the DataView object is pretty negligible.

Punzo commented 2 years ago

tested over OHIF and Slim on a couple of studies, no regression found

image image

pieper commented 2 years ago

Thank you for testing @Punzo 👍

@Ouwen I see you took out the view feature. Is everything else in place and you think this is ready to merge?

Ouwen commented 2 years ago

@pieper I made some changes to the buffer returns so that Uint8Array views are returned instead of new buffer slices.

The tests pass locally for me.

I did have to make a change to the ReadBufferStream to check for Uint8Array and slice an array buffer to get the DicomDict writing to work.

Additionally, it seemed like the normailzer also depended on an ArrayBuffer return instead of a Uint8Array view so performed a similar Uint8Array slice conversion.

This is a bit of a larger API change so we can make it an optional param in DicomMessage.readFile in case there is worries on a regression.

pieper commented 2 years ago

This is a bit of a larger API change so we can make it an optional param in DicomMessage.readFile in case there is worries on a regression.

Yes, I'm concerned about regressions in general since people are using the code in various places and lately there have been several improvement PRs, that are much appreciated but also difficult to evaluate since I don't use the code on a daily basis myself. Of course we've never released a 1.0 version so everything is technically changeable but stability would be preferred.

Making the improvements without changing the API would be preferred as long as the code doesn't end up being too ugly.

Ouwen commented 2 years ago

Sounds good, I'll go ahead and remove the Uint8Array view returns and stick to the original array buffer copies to keep the same API. Will rebase to the most recent master and push up

Ouwen commented 2 years ago

@pieper should be ready for merge

richard-viney commented 2 years ago

After looking at rebasing my PR #276 onto this one a few things came up:

I've updated my PR #276 to address both of these issues, and tighten up the parsing compliance further. I've avoided all copies where we can, given the restrictions of needing to return ArrayBuffer instances. As noted in previous comments, if we returned Uint8Array instead then we could avoid some copies, but this is a public API change that I suspect may break things in the library that don't currently have full test coverage, so I think a change to Uint8Array is best handled separately. For now it seems reasonable to stick with ArrayBuffer, avoiding copies where we can.

I've also added some additional tests around the return type of binary tags.

See #276 for details.

Thoughts?

pieper commented 2 years ago

Oof, thanks for bringing this up @richard-viney. These are the kind of side effects I worry about and thanks for your work to resolve them 👍

@Ouwen can you please have a look?

Ouwen commented 2 years ago

@richard-viney many thanks for giving this a review, will look at your PR now