luno / jettison

Jettison provides structured logging and errors over gRPC
MIT License
19 stars 5 forks source link

Replacing '+=' with String Builder to the Jettison `normalise()` to #1

Open martinomburajr opened 4 years ago

martinomburajr commented 4 years ago

Hey there!

I was going through the jettison library and noticed a += being used to append strings. I thought i would make the minor change and use a strings.Builder instead that is recommended by the go team.

I ran a benchmark and there is a nominal improvement to the execution time with the strings.Builder object rather than the +=

image

martinomburajr commented 4 years ago

I can't make a PR so here are the simple changes to the jettison.go file L48


// as the keys have to be transmittable over the wire (in contexts, for
// instance).
// See https://godoc.org/google.golang.org/grpc/metadata#New.
func normalise(key string) string {
    // Uppercase characters are normalised to lower case.
    key = strings.ToLower(key)

    // Keys beginning with 'grpc-' are disallowed.
    key = strings.TrimPrefix(key, "grpc-")

    var res strings.Builder
    for _, ch := range key {
        // Remove illegal characters from the key.
        if !allowedCharsMap[ch] {
            continue
        }

        res.WriteRune(ch)
    }

    return res.String()
}