Skip to content

perf(stringutil): optimize Format #1583

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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?

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