Skip to content
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

baggage: Improve performance #4743

Merged
merged 26 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Improve `go.opentelemetry.io/otel/trace.TraceState`'s performance. (#4722)
- Improve `go.opentelemetry.io/otel/propagation.TraceContext`'s performance. (#4721)
- Improve `go.opentelemetry.io/otel/baggage` performance. (#4743)

## [1.21.0/0.44.0] 2023-11-16

Expand Down
163 changes: 132 additions & 31 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"

"go.opentelemetry.io/otel/internal/baggage"
Expand All @@ -32,16 +31,6 @@ const (
listDelimiter = ","
keyValueDelimiter = "="
propertyDelimiter = ";"

keyDef = `([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)`
valueDef = `([\x21\x23-\x2b\x2d-\x3a\x3c-\x5B\x5D-\x7e]*)`
keyValueDef = `\s*` + keyDef + `\s*` + keyValueDelimiter + `\s*` + valueDef + `\s*`
)

var (
keyRe = regexp.MustCompile(`^` + keyDef + `$`)
valueRe = regexp.MustCompile(`^` + valueDef + `$`)
propertyRe = regexp.MustCompile(`^(?:\s*` + keyDef + `\s*|` + keyValueDef + `)$`)
)

var (
Expand All @@ -67,7 +56,7 @@ type Property struct {
//
// If key is invalid, an error will be returned.
func NewKeyProperty(key string) (Property, error) {
if !keyRe.MatchString(key) {
if !validateKey(key) {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
}

Expand All @@ -79,10 +68,10 @@ func NewKeyProperty(key string) (Property, error) {
//
// If key or value are invalid, an error will be returned.
func NewKeyValueProperty(key, value string) (Property, error) {
if !keyRe.MatchString(key) {
if !validateKey(key) {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
}
if !valueRe.MatchString(value) {
if !validateValue(value) {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidValue, value)
}

Expand All @@ -106,20 +95,11 @@ func parseProperty(property string) (Property, error) {
return newInvalidProperty(), nil
}

match := propertyRe.FindStringSubmatch(property)
if len(match) != 4 {
p, ok := parsePropertyInternal(property)
if !ok {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidProperty, property)
}

var p Property
if match[1] != "" {
p.key = match[1]
} else {
p.key = match[2]
p.value = match[3]
p.hasValue = true
}

return p, nil
}

Expand All @@ -130,10 +110,10 @@ func (p Property) validate() error {
return fmt.Errorf("invalid property: %w", err)
}

if !keyRe.MatchString(p.key) {
if !validateKey(p.key) {
return errFunc(fmt.Errorf("%w: %q", errInvalidKey, p.key))
}
if p.hasValue && !valueRe.MatchString(p.value) {
if p.hasValue && !validateValue(p.value) {
return errFunc(fmt.Errorf("%w: %q", errInvalidValue, p.value))
}
if !p.hasValue && p.value != "" {
Expand Down Expand Up @@ -305,10 +285,10 @@ func parseMember(member string) (Member, error) {
if err != nil {
return newInvalidMember(), fmt.Errorf("%w: %q", err, value)
}
if !keyRe.MatchString(key) {
if !validateKey(key) {
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidKey, key)
}
if !valueRe.MatchString(value) {
if !validateValue(value) {
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
}

Expand All @@ -323,10 +303,10 @@ func (m Member) validate() error {
return fmt.Errorf("%w: %q", errInvalidMember, m)
}

if !keyRe.MatchString(m.key) {
if !validateKey(m.key) {
return fmt.Errorf("%w: %q", errInvalidKey, m.key)
}
if !valueRe.MatchString(m.value) {
if !validateValue(m.value) {
return fmt.Errorf("%w: %q", errInvalidValue, m.value)
}
return m.properties.validate()
Expand Down Expand Up @@ -550,3 +530,124 @@ func (b Baggage) String() string {
}
return strings.Join(members, listDelimiter)
}

// parsePropertyInternal attempts to decode a Property from the passed string.
func parsePropertyInternal(s string) (p Property, ok bool) {
// Attempting to parse the key.
index := skipSpace(s, 0)

keyStart := index
keyEnd := index
for _, c := range s[keyStart:] {
if !validateKeyChar(c) {
break
}
keyEnd++
}

if keyStart == keyEnd {
// Empty string after skipping whitespaces.
return
}

index = skipSpace(s, keyEnd)

p.key = s[keyStart:keyEnd]
pellared marked this conversation as resolved.
Show resolved Hide resolved

if index == len(s) {
// There is only a key (no value).
ok = true
return
}

if s[index] != keyValueDelimiter[0] {
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
// Bad key-value delimiter.
pellared marked this conversation as resolved.
Show resolved Hide resolved
return
}

// Attempting to parse the value.
index = skipSpace(s, index+1)

valueStart := index
valueEnd := index
for _, c := range s[valueStart:] {
if !validateValueChar(c) {
break
}
valueEnd++
}

index = skipSpace(s, valueEnd)
if index != len(s) {
// Invalid value.
return
}

ok = true
// If there is a delimiter, we set hasValue to true even if the value is empty.
p.hasValue = true
p.value = s[valueStart:valueEnd]
return
}

func skipSpace(s string, offset int) int {
i := offset
for ; i < len(s); i++ {
c := s[i]
if c != ' ' &&
c != '\t' &&
c != '\n' &&
c != '\v' &&
c != '\f' &&
c != '\r' {
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
return i
}

func validateKey(s string) bool {
if len(s) == 0 {
return false
}

for _, c := range s {
if !validateKeyChar(c) {
return false
}
}

return true
}

func validateKeyChar(c int32) bool {
return (c >= 0x23 && c <= 0x27) ||
(c >= 0x30 && c <= 0x39) ||
(c >= 0x41 && c <= 0x5a) ||
(c >= 0x5e && c <= 0x7a) ||
c == 0x21 ||
c == 0x2a ||
c == 0x2b ||
c == 0x2d ||
c == 0x2e ||
c == 0x7c ||
c == 0x7e
}

func validateValue(s string) bool {
for _, c := range s {
if !validateValueChar(c) {
return false
}
}

return true
}

func validateValueChar(c int32) bool {
return (c >= 0x23 && c <= 0x2b) ||
(c >= 0x2d && c <= 0x3a) ||
(c >= 0x3c && c <= 0x5b) ||
(c >= 0x5d && c <= 0x7e) ||
c == 0x21
}
4 changes: 2 additions & 2 deletions baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestKeyRegExp(t *testing.T) {
}

for _, ch := range invalidKeyRune {
assert.NotRegexp(t, keyDef, fmt.Sprintf("%c", ch))
assert.False(t, validateKeyChar(ch))
pellared marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -60,7 +60,7 @@ func TestValueRegExp(t *testing.T) {
}

for _, ch := range invalidValueRune {
assert.NotRegexp(t, `^`+valueDef+`$`, fmt.Sprintf("invalid-%c-value", ch))
assert.False(t, validateValueChar(ch))
pellared marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading