Skip to content

Optimize tag value parsing #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion tag_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,36 @@ func (tv *TagValue) init(tag Tag, value []byte) {
}

func (tv *TagValue) parse(rawFieldBytes []byte) error {
sepIndex := bytes.IndexByte(rawFieldBytes, '=')
var sepIndex int

// Most of the Fix tags are 4 or less characters long, so we can optimize
// for that by checking the 5 first characters without looping over the
// whole byte slice.
if len(rawFieldBytes) >= 5 {
if rawFieldBytes[1] == '=' {
sepIndex = 1
goto PARSE
} else if rawFieldBytes[2] == '=' {
sepIndex = 2
goto PARSE
} else if rawFieldBytes[3] == '=' {
sepIndex = 3
goto PARSE
} else if rawFieldBytes[4] == '=' {
sepIndex = 4
goto PARSE
}
}

sepIndex = bytes.IndexByte(rawFieldBytes, '=')
switch sepIndex {
case -1:
return fmt.Errorf("tagValue.Parse: No '=' in '%s'", rawFieldBytes)
case 0:
return fmt.Errorf("tagValue.Parse: No tag in '%s'", rawFieldBytes)
}

PARSE:
parsedTag, err := atoi(rawFieldBytes[:sepIndex])
if err != nil {
return fmt.Errorf("tagValue.Parse: %s", err.Error())
Expand Down
Loading