From 426c87f84b5d5eaae524edaa1c4ea63f1481728f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Dec 2025 07:17:50 +0000 Subject: [PATCH] Initialize metaElems with initial capacity in read.go Set the initial capacity of `metaElems` slice to 20 to avoid unnecessary allocations when reading DICOM headers. This addresses a TODO in the code. 20 is chosen as a reasonable upper bound for standard File Meta Information elements (Group 0002). --- read.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/read.go b/read.go index 5366c332..83ffa446 100644 --- a/read.go +++ b/read.go @@ -216,7 +216,11 @@ func (r *reader) readHeader() ([]*Element, error) { return nil, fmt.Errorf("error reading DICOM header element: %w", err) } - metaElems := []*Element{maybeMetaLen} // TODO: maybe set capacity to a reasonable initial size + // Initialize with a capacity of 20 to avoid reallocations for standard DICOM headers. + // 20 is chosen as a reasonable upper bound for the number of File Meta Information elements + // typically found (Group 0002). + metaElems := make([]*Element, 0, 20) + metaElems = append(metaElems, maybeMetaLen) metaElementGroupLengthDefined := true if maybeMetaLen.Tag != tag.FileMetaInformationGroupLength || maybeMetaLen.Value.ValueType() != Ints { // MetaInformationGroupLength is not present or of the wrong value type.