Skip to content

Commit 98a517c

Browse files
committed
Reduce allocations during readNativeFrames with frame channel
1 parent 8f1f151 commit 98a517c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

read.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ func (r *reader) readPixelData(vl uint32, d *Dataset, fc chan<- *frame.Frame) (V
256256

257257
if fc != nil {
258258
fc <- &f
259+
} else {
260+
image.Frames = append(image.Frames, &f)
259261
}
260-
261-
image.Frames = append(image.Frames, &f)
262262
}
263263
image.IntentionallySkipped = r.opts.skipPixelData
264264
return &pixelDataValue{PixelDataInfo: image}, nil
@@ -440,18 +440,23 @@ func (r *reader) readNativeFrames(parsedData *Dataset, fc chan<- *frame.Frame, v
440440
image.Frames = make([]*frame.Frame, nFrames)
441441
bo := r.rawReader.ByteOrder()
442442
pixelBuf := make([]byte, bytesAllocated)
443+
nativeDataBuf := make([][]int, pixelsPerFrame)
444+
buf := make([]int, pixelsPerFrame*samplesPerPixel)
443445
for frameIdx := 0; frameIdx < nFrames; frameIdx++ {
446+
if fc == nil {
447+
nativeDataBuf = make([][]int, pixelsPerFrame)
448+
buf = make([]int, pixelsPerFrame*samplesPerPixel)
449+
}
444450
// Init current frame
445451
currentFrame := frame.Frame{
446452
Encapsulated: false,
447453
NativeData: frame.NativeFrame{
448454
BitsPerSample: bitsAllocated,
449455
Rows: MustGetInts(rows.Value)[0],
450456
Cols: MustGetInts(cols.Value)[0],
451-
Data: make([][]int, pixelsPerFrame),
457+
Data: nativeDataBuf,
452458
},
453459
}
454-
buf := make([]int, pixelsPerFrame*samplesPerPixel)
455460
if bitsAllocated == 1 {
456461
if err := fillBufferSingleBitAllocated(buf, r.rawReader, bo); err != nil {
457462
return nil, bytesToRead, err
@@ -482,9 +487,10 @@ func (r *reader) readNativeFrames(parsedData *Dataset, fc chan<- *frame.Frame, v
482487
currentFrame.NativeData.Data[pixel] = buf[pixel*samplesPerPixel : (pixel+1)*samplesPerPixel]
483488
}
484489
}
485-
image.Frames[frameIdx] = &currentFrame
486490
if fc != nil {
487491
fc <- &currentFrame // write the current frame to the frame channel
492+
} else {
493+
image.Frames[frameIdx] = &currentFrame
488494
}
489495
}
490496
if skipFinalPaddingByte {

0 commit comments

Comments
 (0)