Skip to content

Reduce allocations in Parse and Format#6

Merged
benbardin merged 1 commit intomainfrom
parsing
Feb 23, 2026
Merged

Reduce allocations in Parse and Format#6
benbardin merged 1 commit intomainfrom
parsing

Conversation

@benbardin
Copy link
Contributor

Hoist compiled regexes and phase-name lookups to package level so they aren't rebuilt on every call. Fix two unescaped dots in parse patterns and a broken error message in the unknown-phase branch. Add tests and a benchmark.

@benbardin benbardin requested a review from dcrosta February 20, 2026 19:04
{
raw: "v23.2.0-cloudonly2",
phase: cloudonly,
phaseOrdinal: 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a valid version, there must be a . after "cloudonly". This should not parse successfully.

(I think? What did the old code version do with this version string?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this matches the existing pattern? That accepts cloudonly with optional digits and no dot — it predates this PR. Do you know if these versions exist in the wild? If not, we could tighten the pattern to require a dot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, you're right, and we have pre-existing test cases. My mistake! (I think as cloudonlys were being introduced there were some odd versions with inconsistent naming types; the set of tests in this package is based on a comprehensive review of the patterns of actual versions that existed in the CC control plane DB when I wrote this package, plus consultation with releng folks.)

version_test.go Outdated
}
}

func TestCompareTransitivitySmallSet(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the existing tests on sorting not already sufficiently prove these things? sorting uses Compare()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, dropped

regexp.MustCompile(`^v(?P<year>[1-9][0-9]*)\.(?P<ordinal>[1-9][0-9]*)\.(?P<patch>(?:[1-9][0-9]*|0))-(?P<phase>alpha|beta|rc|cloudonly).(?P<phaseOrdinal>[0-9]+)-(?P<customOrdinal>(?:[1-9][0-9]*|0))-g[a-f0-9]+(?:-fips)?$`),
regexp.MustCompile(`^v(?P<year>[1-9][0-9]*)\.(?P<ordinal>[1-9][0-9]*)\.(?P<patch>(?:[1-9][0-9]*|0))-(?P<phase>alpha|beta|rc|cloudonly).(?P<phaseOrdinal>[0-9]+)-cloudonly(-rc|\.)(?P<phaseSubOrdinal>(?:[1-9][0-9]*|0))$`),
regexp.MustCompile(`^v(?P<year>[1-9][0-9]*)\.(?P<ordinal>[1-9][0-9]*)\.(?P<patch>(?:[1-9][0-9]*|0))-(?P<phase>cloudonly)-rc(?P<phaseOrdinal>[0-9]+)$`),
regexp.MustCompile(`^v(?P<year>[1-9][0-9]*)\.(?P<ordinal>[1-9][0-9]*)\.(?P<patch>(?:[1-9][0-9]*|0))-(?P<phase>cloudonly)(?P<phaseOrdinal>[0-9]+)?$`),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I think? Is where cloudonly with no dot was allowed

Hoist compiled regexes and phase-name lookups to package level so they
aren't rebuilt on every call. Fix two unescaped dots in parse patterns
and a broken error message in the unknown-phase branch. Add tests and
a benchmark.
@benbardin
Copy link
Contributor Author

tftr!

@benbardin benbardin merged commit 6b1cf83 into main Feb 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants