Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) #163

Merged
merged 10 commits into from
Dec 22, 2020

Conversation

kaxap
Copy link
Contributor

@kaxap kaxap commented Dec 20, 2020

addressing #161

@kaxap
Copy link
Contributor Author

kaxap commented Dec 20, 2020

Benchmarks looking nice

ReadNativeFrames/10x10,_10_frames,_1_sample/pixel-2    1.89µs ± 6%  2.32µs ± 3%  +23.20%  (p=0.000 n=8+8)
ReadNativeFrames/100x100,_10_frames,_1_sample/pixel-2  86.1µs ± 2%  83.9µs ± 2%   -2.56%  (p=0.002 n=7+7)
ReadNativeFrames/512x512,_10_frames,_1_sample/pixel-2  2.34ms ± 3%  1.65ms ± 6%  -29.48%  (p=0.000 n=8+8)
ReadNativeFrames/512x512,_10_frames,_5_sample/pixel-2   814ms ± 6%     4ms ± 7%  -99.51%  (p=0.000 n=8+8)
Parse/1.dcm-2                                          2.62ms ± 3%  1.33ms ± 2%  -49.34%  (p=0.000 n=8+8)
Parse/2.dcm-2                                          2.69ms ± 3%  1.32ms ± 4%  -50.96%  (p=0.001 n=7+7)
Parse/3.dcm-2                                          2.53ms ± 3%  1.19ms ± 2%  -52.91%  (p=0.000 n=8+8)
Parse/4.dcm-2                                           268ms ± 7%   116ms ± 3%  -56.75%  (p=0.000 n=8+8)
Parse/5.dcm-2                                          38.2ms ± 2%  17.7ms ± 2%  -53.79%  (p=0.000 n=7+8)

@suyashkumar suyashkumar self-requested a review December 20, 2020 05:08
@suyashkumar suyashkumar self-assigned this Dec 20, 2020
@suyashkumar suyashkumar added enhancement New feature or request performance labels Dec 20, 2020
Copy link
Owner

@suyashkumar suyashkumar left a comment

Choose a reason for hiding this comment

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

Thank you @kaxap for the contribution! The improvement looks great from avoiding binary.Read which allocates a slice each time and instead manually unpacking the bytes into the right uint by reusing a once-allocated buffer.

Generally looks good, however I had some comments for you to consider before I merge.

Thanks again for the improvement :D

pkg/dicomio/reader.go Outdated Show resolved Hide resolved
read.go Outdated Show resolved Hide resolved
read.go Outdated Show resolved Hide resolved
read.go Outdated
}
if n < bitsAllocated/8 {
return nil, bytesRead,
fmt.Errorf("not enough bytes in the input to read uint%d", bitsAllocated)
Copy link
Owner

Choose a reason for hiding this comment

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

just a thought: can we create a sentinel error for this (e.g ErrorIncompleteRead or something) and wrap that error with the right bitsAllocated info?

Copy link
Owner

Choose a reason for hiding this comment

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

also, could we maybe add a test case here to trigger this and expect we get this type of error? we would have to update the test to use errors.Is. Also, I can send a change for this later too if we prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pkg/dicomio/reader.go Outdated Show resolved Hide resolved
kaxap and others added 4 commits December 20, 2020 16:03
Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>
Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>
Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>
Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>
read.go Outdated Show resolved Hide resolved
kaxap and others added 2 commits December 20, 2020 16:09
Add test for ErrorIncompleteRead
minor refactoring
@kaxap
Copy link
Contributor Author

kaxap commented Dec 20, 2020

@suyashkumar appreciate your suggestions, implemented all of them I believe.

Copy link
Owner

@suyashkumar suyashkumar left a comment

Choose a reason for hiding this comment

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

Thanks so much for making those updates! I think it's looking pretty much good to go, I just had one final set of thoughts for you to consider and then let's get this merged. Thank you again for the contribution 😄

read.go Outdated
val, err := d.ReadUInt8()
if err != nil {
return nil, bytesRead, err

Copy link
Owner

Choose a reason for hiding this comment

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

nit: no need for a new line here (but I can also just fix in a cleanup pr later, don't need to block this).

Suggested change

read.go Outdated
if err != nil {
return nil, bytesRead, err

n, err := d.Read(pixelBuf)
Copy link
Owner

@suyashkumar suyashkumar Dec 21, 2020

Choose a reason for hiding this comment

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

Thought about this a little more the second time around (sorry for not bringing this up earlier). Looking at the io.Reader interface I think a single call to read may read "up to len(p)" bytes. Perhaps we should use io.ReadFull to try to read at least the size of the buffer.

In reality for values like 16 or 32 bits, calls to Read may still work no problem, but it may be more "correct" to use something like io.ReadFull which won't be expensive in the case that Read would've worked on the first try. This may also insulate us from the exact implementation of the reader under the hood we're reading from, since there is no garuntee that it must fill the buffer on the first call to Read (but it's possible it may fill the buffer correctly on subsequent calls to Read--see the implementation of ReadAtLeast).

It also looks like based on the implementation, it may return a nice ErrUnexpectedEOF if it reads less than n bytes, saving us an error, but we can leave it in there for not and not depend on that to make it easy and get this merged for now.

WDYT?

Suggested change
n, err := d.Read(pixelBuf)
n, err := io.ReadFull(d, pixelBuf)

Copy link
Contributor Author

@kaxap kaxap Dec 21, 2020

Choose a reason for hiding this comment

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

Good catch. Read(buffer) can in fact return less than n bytes when reading from a network socket, which is expected behaviour.

read.go Outdated Show resolved Hide resolved
@suyashkumar suyashkumar changed the title pre-allocate buffer for reading pixel data in readNativeFrames Use a manual once-allocated buffer for reading pixel values instead of using binary.Read (which allocates a slice under the hood) Dec 21, 2020
@suyashkumar suyashkumar changed the title Use a manual once-allocated buffer for reading pixel values instead of using binary.Read (which allocates a slice under the hood) Use a manual once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Dec 21, 2020
@suyashkumar suyashkumar changed the title Use a manual once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Use a manual once-allocated buffer per frame for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Dec 21, 2020
@suyashkumar suyashkumar changed the title Use a manual once-allocated buffer per frame for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Use a manual once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Dec 21, 2020
Remove ErrorIncompleteRead as a consequence
Update ReadNativeFrames test accordingly
Copy link
Owner

@suyashkumar suyashkumar left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for making the suggested updates and thanks for the contribution!

@suyashkumar suyashkumar changed the title Use a manual once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Use a once-allocated buffer for reading native pixel values instead of using binary.Read (which allocates a slice under the hood) Dec 22, 2020
@suyashkumar suyashkumar merged commit a6f29f2 into suyashkumar:main Dec 22, 2020
@suyashkumar
Copy link
Owner

🎉 merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants