Skip to content

Conversation

camc314
Copy link
Contributor

@camc314 camc314 commented Aug 14, 2025

This PR improves the performance of stringutils::Format, by removing the slow regex, in favour of a manual implementation.

Screenshot 2025-08-14 at 13 43 54

)

var placeholderRegexp = regexp.MustCompile(`{(\d+)}`)
const maxInt = int(^uint(0) >> 1)
Copy link
Member

Choose a reason for hiding this comment

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

I'd eliminate this and just use math.MaxInt.

var b strings.Builder
b.Grow(len(text) + len(text)/4)

for i := 0; i < len(text); {
Copy link
Member

Choose a reason for hiding this comment

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

I feel wary about the amount of code used here; it's a lot of string/index finagling.

Can this instead be replaced by a loop where we use strings.IndexByte or strings.Cut to find the next { or }, along with slicing text as we go along?

@jakebailey
Copy link
Member

A bit surprised this matters, given your profile only shows this taking a fraction of a percent?

@camc314
Copy link
Contributor Author

camc314 commented Oct 8, 2025

A bit surprised this matters, given your profile only shows this taking a fraction of a percent?

yeah we can probably close this. i just noticed this sticking out at me in the flame graph. but it is only a fraction of wall time

@camc314 camc314 closed this Oct 8, 2025
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