Skip to content

Commit

Permalink
Optimize string field accumulation to do fewer allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dolak authored and petee-d committed Dec 5, 2022
1 parent 3de599c commit d03a0d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion nodes.go
Expand Up @@ -734,9 +734,14 @@ func setField(tokens []lexer.Token, strct reflect.Value, field structLexerField,
if err != nil {
return err
}
if len(fieldValue) == 0 {
return nil
}
accumulated := f.String()
for _, v := range fieldValue {
f.Set(reflect.ValueOf(f.String() + v.String()).Convert(f.Type()))
accumulated += v.String()
}
f.SetString(accumulated)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion parser_test.go
Expand Up @@ -201,8 +201,9 @@ func TestRepetitionAcrossFields(t *testing.T) {
}

func TestAccumulateString(t *testing.T) {
type customString string
type testAccumulateString struct {
A string `@"."+`
A customString `@"."+`
}

parser := mustTestParser[testAccumulateString](t)
Expand Down

0 comments on commit d03a0d2

Please sign in to comment.