Skip to content

Improve validateUserAttribute allocation #1087

@fggarcia

Description

@fggarcia

Summary

avoid interface{} allocation

Desired Behaviour

when val is a string and call to truncateStringValueIfLong, if not truncated, no create a new interface{}

if str, ok := val.(string); ok {
	val = interface{}(truncateStringValueIfLong(str))
}
func truncateStringValueIfLong(val string) string {
	if len(val) > attributeValueLengthLimit {
		return stringLengthByteLimit(val, attributeValueLengthLimit)
	}
	return val
}

Possible Solution

truncateStringValueIfLong maybe return if apply truncate or not

I create an approch solution.
Benchmark with success case without truncate

BenchmarkValidateUserAttribute
BenchmarkValidateUserAttribute/alloc
BenchmarkValidateUserAttribute/alloc-8         	75147321	        15.60 ns/op	      16 B/op	       1 allocs/op
BenchmarkValidateUserAttribute/no_alloc
BenchmarkValidateUserAttribute/no_alloc-8      	487201981	         2.426 ns/op	       0 B/op	       0 allocs/op

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions