Skip to content

Commit

Permalink
Reduce allocations during readNativeFrames
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar committed Dec 18, 2020
1 parent 6add2b0 commit ae94f35
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ release:
tar -zcvf ${BINARY}-linux-amd64.tar.gz ${BINARY}-linux-amd64; \
tar -zcvf ${BINARY}-darwin-amd64.tar.gz ${BINARY}-darwin-amd64; \
zip -r ${BINARY}-windows-amd64.exe.zip ${BINARY}-windows-amd64.exe;

bench-diff:
go test -bench . -count 5 > bench_current.txt
git checkout main
go test -bench . -count 5 > bench_main.txt
benchstat bench_main.txt bench_current.txt
git checkout -
10 changes: 5 additions & 5 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,30 +223,30 @@ func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Fr
Data: make([][]int, int(pixelsPerFrame)),
},
}
buf := make([]int, int(pixelsPerFrame)*samplesPerPixel)
for pixel := 0; pixel < int(pixelsPerFrame); pixel++ {
currentPixel := make([]int, samplesPerPixel)
for value := 0; value < samplesPerPixel; value++ {
if bitsAllocated == 8 {
val, err := d.ReadUInt8()
if err != nil {
return nil, bytesRead, errors.New("")
}
currentPixel[value] = int(val)
buf[pixel+value] = int(val)
} else if bitsAllocated == 16 {
val, err := d.ReadUInt16()
if err != nil {
return nil, bytesRead, errors.New("")
}
currentPixel[value] = int(val)
buf[pixel+value] = int(val)
} else if bitsAllocated == 32 {
val, err := d.ReadUInt32()
if err != nil {
return nil, bytesRead, errors.New("")
}
currentPixel[value] = int(val)
buf[pixel+value] = int(val)
}
}
currentFrame.NativeData.Data[pixel] = currentPixel
currentFrame.NativeData.Data[pixel] = buf[pixel*samplesPerPixel : (pixel+1)*samplesPerPixel]
}
image.Frames[frameIdx] = currentFrame
if fc != nil {
Expand Down

0 comments on commit ae94f35

Please sign in to comment.