Skip to content

Commit

Permalink
ipc/schema/array: avoid unnecessary allocations from Schema.Fields
Browse files Browse the repository at this point in the history
Some for loops can be replaced with an iteration over schema field indices
rather than a copy of the fields. This reduces allocations.
  • Loading branch information
asubiotto committed Nov 28, 2023
1 parent 054464a commit cea1ffc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions go/arrow/array/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (rec *simpleRecord) validate() error {
return nil
}

if len(rec.arrs) != len(rec.schema.Fields()) {
if len(rec.arrs) != rec.schema.NumFields() {
return fmt.Errorf("arrow/array: number of columns/fields mismatch")
}

Expand Down Expand Up @@ -288,8 +288,8 @@ func NewRecordBuilder(mem memory.Allocator, schema *arrow.Schema) *RecordBuilder
fields: make([]Builder, schema.NumFields()),
}

for i, f := range schema.Fields() {
b.fields[i] = NewBuilder(b.mem, f.Type)
for i := 0; i < schema.NumFields(); i++ {
b.fields[i] = NewBuilder(b.mem, schema.Field(i).Type)
}

return b
Expand Down Expand Up @@ -397,8 +397,8 @@ func (b *RecordBuilder) UnmarshalJSON(data []byte) error {
}
}

for i, f := range b.schema.Fields() {
if !keylist[f.Name] {
for i := 0; i < b.schema.NumFields(); i++ {
if !keylist[b.schema.Field(i).Name] {
b.fields[i].AppendNull()
}
}
Expand Down
4 changes: 2 additions & 2 deletions go/arrow/ipc/file_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ func newRecord(schema *arrow.Schema, memo *dictutils.Memo, meta *memory.Buffer,

pos := dictutils.NewFieldPos()
cols := make([]arrow.Array, schema.NumFields())
for i, field := range schema.Fields() {
data := ctx.loadArray(field.Type)
for i := 0; i < schema.NumFields(); i++ {
data := ctx.loadArray(schema.Field(i).Type)
defer data.Release()

if err := dictutils.ResolveFieldDict(memo, data, pos.Child(int32(i)), mem); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go/arrow/ipc/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,8 +1102,8 @@ func schemaFromFB(schema *flatbuf.Schema, memo *dictutils.Memo) (*arrow.Schema,
func schemaToFB(b *flatbuffers.Builder, schema *arrow.Schema, memo *dictutils.Mapper) flatbuffers.UOffsetT {
fields := make([]flatbuffers.UOffsetT, schema.NumFields())
pos := dictutils.NewFieldPos()
for i, field := range schema.Fields() {
fields[i] = fieldToFB(b, pos.Child(int32(i)), field, memo)
for i := 0; i < schema.NumFields(); i++ {
fields[i] = fieldToFB(b, pos.Child(int32(i)), schema.Field(i), memo)
}

flatbuf.SchemaStartFieldsVector(b, len(fields))
Expand Down
4 changes: 2 additions & 2 deletions go/arrow/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (s *Schema) AddField(i int, field Field) (*Schema, error) {
func (s *Schema) String() string {
o := new(strings.Builder)
fmt.Fprintf(o, "schema:\n fields: %d\n", s.NumFields())
for i, f := range s.Fields() {
for i, f := range s.fields {
if i > 0 {
o.WriteString("\n")
}
Expand All @@ -282,7 +282,7 @@ func (s *Schema) Fingerprint() string {

var b strings.Builder
b.WriteString("S{")
for _, f := range s.Fields() {
for _, f := range s.fields {
fieldFingerprint := f.Fingerprint()
if fieldFingerprint == "" {
return ""
Expand Down

0 comments on commit cea1ffc

Please sign in to comment.