zeroshade commented on code in PR #1131:
URL: https://github.com/apache/arrow-go/pull/1131#discussion_r3752416251


##########
arrow/datatype_nested.go:
##########
@@ -734,10 +734,15 @@ func (t *unionType) validate(fields []Field, typeCodes 
[]UnionTypeCode, _ UnionM
                return errors.New("arrow: union types should have the same 
number of fields as type codes")
        }
 
+       seen := make(map[UnionTypeCode]struct{}, len(typeCodes))

Review Comment:
   Minor: this allocates a map on every union construction, but the key space 
is small and fixed — `MaxUnionTypeCode` is 127, and the loop above already 
rejects anything outside `0..MaxUnionTypeCode` before this point.
   
   A stack array avoids the allocation entirely:
   
   ```go
   var seen [MaxUnionTypeCode + 1]bool
   for _, c := range typeCodes {
        if c < 0 || c > MaxUnionTypeCode {
                return errors.New("arrow: union type code out of bounds")
        }
        if seen[c] {
                return errors.New("arrow: union type codes must be unique")
        }
        seen[c] = true
   }
   ```
   
   There's precedent right in this type — `unionType` already carries `childIDs 
[int(MaxUnionTypeCode) + 1]int` for the same reason. 128 bytes on the stack 
versus a heap map is a reasonable trade for a function on the type-construction 
path.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to