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 8 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.
Jump to
Jump to file
Failed to load files.
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.NewMember`'s performance. (#4743)
pellared marked this conversation as resolved.
Show resolved Hide resolved

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

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

"go.opentelemetry.io/otel/internal/baggage"
Expand All @@ -32,16 +31,6 @@
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 @@
//
// If key is invalid, an error will be returned.
func NewKeyProperty(key string) (Property, error) {
if !keyRe.MatchString(key) {
if !keyMatchString(key) {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidKey, key)
}

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

Expand All @@ -106,7 +95,7 @@
return newInvalidProperty(), nil
}

match := propertyRe.FindStringSubmatch(property)
match := propertyFindStringSubmatch(property)
if len(match) != 4 {
return newInvalidProperty(), fmt.Errorf("%w: %q", errInvalidProperty, property)
}
Expand All @@ -130,10 +119,10 @@
return fmt.Errorf("invalid property: %w", err)
}

if !keyRe.MatchString(p.key) {
if !keyMatchString(p.key) {
return errFunc(fmt.Errorf("%w: %q", errInvalidKey, p.key))
}
if p.hasValue && !valueRe.MatchString(p.value) {
if p.hasValue && !valueMatchString(p.value) {
return errFunc(fmt.Errorf("%w: %q", errInvalidValue, p.value))
}
if !p.hasValue && p.value != "" {
Expand Down Expand Up @@ -305,10 +294,10 @@
if err != nil {
return newInvalidMember(), fmt.Errorf("%w: %q", err, value)
}
if !keyRe.MatchString(key) {
if !keyMatchString(key) {
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidKey, key)
}
if !valueRe.MatchString(value) {
if !valueMatchString(value) {
return newInvalidMember(), fmt.Errorf("%w: %q", errInvalidValue, value)
}

Expand All @@ -323,10 +312,10 @@
return fmt.Errorf("%w: %q", errInvalidMember, m)
}

if !keyRe.MatchString(m.key) {
if !keyMatchString(m.key) {
return fmt.Errorf("%w: %q", errInvalidKey, m.key)
}
if !valueRe.MatchString(m.value) {
if !valueMatchString(m.value) {
return fmt.Errorf("%w: %q", errInvalidValue, m.value)
}
return m.properties.validate()
Expand Down Expand Up @@ -550,3 +539,118 @@
}
return strings.Join(members, listDelimiter)
}

// They must follow the following rules (regex syntax):
// 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]*)`
// propertyRe ^(?:\s*([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)\s*|\s*([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)\s*=\s*([\x21\x23-\x2b\x2d-\x3a\x3c-\x5B\x5D-\x7e]*)\s*)$.

func propertyFindStringSubmatch(s string) []string {
pellared marked this conversation as resolved.
Show resolved Hide resolved
pellared marked this conversation as resolved.
Show resolved Hide resolved
index := skipSpace(s, 0)

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

if keyStart == keyEnd {
return nil
}

index = skipSpace(s, keyEnd)

// this matches only the key
if index == len(s) {
return []string{s, s[keyStart:keyEnd], "", ""}
}

// now let see if it matches the key and the value
if s[index] != keyValueDelimiter[0] {
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Check warning on line 574 in baggage/baggage.go

View check run for this annotation

Codecov / codecov/patch

baggage/baggage.go#L573-L574

Added lines #L573 - L574 were not covered by tests

index = skipSpace(s, index+1)

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

index = skipSpace(s, valueEnd)
if index != len(s) {
return nil
}

// value can be empty, so no need to do the same check here
return []string{s, "", s[keyStart:keyEnd], s[valueStart:valueEnd]}
}

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 keyMatchString(s string) bool {
pellared marked this conversation as resolved.
Show resolved Hide resolved
if len(s) == 0 {
return false
}

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

return true
}

func keyValidChar(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 valueMatchString(s string) bool {
pellared marked this conversation as resolved.
Show resolved Hide resolved
for _, c := range s {
if !valueValidChar(c) {
return false
}
}

return true
}

func valueValidChar(c int32) bool {
return (c >= 0x23 && c <= 0x2b) ||
(c >= 0x2d && c <= 0x3a) ||
(c >= 0x3c && c <= 0x5b) ||
(c >= 0x5d && c <= 0x7e) ||
c == 0x21
}
5 changes: 2 additions & 3 deletions baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/internal/baggage"
)

Expand All @@ -45,7 +44,7 @@ func TestKeyRegExp(t *testing.T) {
}

for _, ch := range invalidKeyRune {
assert.NotRegexp(t, keyDef, fmt.Sprintf("%c", ch))
assert.False(t, keyValidChar(ch))
}
}

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

for _, ch := range invalidValueRune {
assert.NotRegexp(t, `^`+valueDef+`$`, fmt.Sprintf("invalid-%c-value", ch))
assert.False(t, valueValidChar(ch))
}
}

Expand Down