Skip to content

Commit

Permalink
refactor: Re-use the lineGroup.joinedLines calculation. (#61)
Browse files Browse the repository at this point in the history
This should minimize any performance penalty we're getting from the fact
that joinedLines isn't cached.

https://github.com/google/keep-sorted/blob/65bfea4384f2e3570eb5653d6ebaaec3b7b286b1/keepsorted/line_group.go#L325-L327
  • Loading branch information
JeffFaer authored Jan 9, 2025
1 parent 1655d56 commit 7ae1dfa
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions keepsorted/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ func (b block) lessFn() cmpFunc[lineGroup] {
longestFirst := comparing(func(s string) int { return len(s) }).reversed()
prefixes := slices.SortedStableFunc(slices.Values(b.metadata.opts.PrefixOrder), longestFirst)

prefixOrder := comparing(func(lg lineGroup) int {
p, ok := b.metadata.opts.hasPrefix(lg.joinedLines(), slices.Values(prefixes))
prefixOrder := comparing(func(s string) int {
p, ok := b.metadata.opts.hasPrefix(s, slices.Values(prefixes))
if !ok {
return 0
}
Expand All @@ -416,17 +416,15 @@ func (b block) lessFn() cmpFunc[lineGroup] {
// foo_6
// Foo_45
// foo_123
transformOrder := comparingFunc(func(lg lineGroup) numericTokens {
l := lg.joinedLines()
l = b.metadata.opts.trimIgnorePrefix(l)
transformOrder := comparingFunc(func(s string) numericTokens {
s = b.metadata.opts.trimIgnorePrefix(s)
if !b.metadata.opts.CaseSensitive {
l = strings.ToLower(l)
s = strings.ToLower(s)
}
return b.metadata.opts.maybeParseNumeric(l)
return b.metadata.opts.maybeParseNumeric(s)
}, numericTokens.compare)

return commentOnlyBlock.
andThen(prefixOrder).
andThen(transformOrder).
andThen(comparingFunc(lineGroup.joinedLines, prefixOrder.andThen(transformOrder))).
andThen(lineGroup.less)
}

0 comments on commit 7ae1dfa

Please sign in to comment.