-
Notifications
You must be signed in to change notification settings - Fork 17
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
Performance improvements for formatPath #194
base: master
Are you sure you want to change the base?
Conversation
…tests for that in formatPath Adds the original implementation of formatPath for comparison in tests and benchmarks
func originalFormatPath(pattern string, m Attributes) (string, bool) { | ||
allFound := true | ||
paths := strings.Split(pattern, "/") | ||
result := strings.Builder{} | ||
for _, path := range paths { | ||
if len(path) == 0 { | ||
continue | ||
} | ||
result.WriteString("/") | ||
if strings.HasPrefix(path, ":") { | ||
attr := m.GetAttribute(path[1:]) | ||
if attr == nil { | ||
s := path | ||
attr = &s | ||
allFound = false | ||
} | ||
result.WriteString(*attr) | ||
} else { | ||
result.WriteString(path) | ||
} | ||
} | ||
return result.String(), allFound | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the original implementation temporarily as a point of comparison in tests and benchmarks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results are doc'd can remove this from the PR now.
Benchmark Results
|
func formatPath(pattern string, m Attributes) (string, bool) { | ||
sb := new(strings.Builder) | ||
// If the formatted string can fit into 2x the length of the pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cmt should be 2.5x
now
@@ -20,6 +20,7 @@ type regexKeyMatcher struct { | |||
|
|||
type keyMatch interface { | |||
GetAttribute(name string) *string | |||
FindAttribute(name string) (string, bool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a cmt to the GetAttribute that it's been replaced by FindAttribute?
@jcopi Thx for all your work here. We would like to get this in now. I added a few last minor updates and then can create the formal PR for finals reviews. |
The change reduces memory usage of the
formatPath
function by:Grow
after creating astrings.Builder
to reduce the number of future allocationsstrings.Split
, instead if iterates over the segments usingstrings.Cut
so that there is no need to allocate a string header per path segment.Attributes
so that theGetAttribute
function returns(string, bool)
rather than*string
. This eliminates the need for a heap allocation of the attribute value in most cases.The result of this is that formatPath is faster and has a single allocation (assuming the formatted string is <= 2x the length of the pattern).
This also adds benchmarks and tests for the the
formatPath
function to ensure that functionality has not changedENSURE THE FOLLOWING ARE MET:
Below is a brief summary of PR requirements (full list).
By opening this PR for review, the author has agreed that these criteria must be met.
By approving this PR, the reviewers have also agreed these criteria have been met and it is ready to be merged.