lima icon indicating copy to clipboard operation
lima copied to clipboard

pkg: refactor to compile regexp only once

Open alexandear opened this issue 1 year ago • 2 comments

This PR moves the compiled regexp outside the functions to prevent it from being recompiled each time these functions are called. Some regexp moved out of loops to save readability.

alexandear avatar May 08 '24 20:05 alexandear

to prevent it from being recompiled each time these functions are called

All these regexes are only used once, or very few times, so the time spent recompiling them is immaterial.[^1]

[^1]: And if you are using VZ emulation instead of QEMU, then you now compile a regex that otherwise you wouldn't. Others may also be unused, depending on the subcommand you are running. None of this matters, of course.

On the other hand, you now introduce additional global variables with longer names in a wider scope, and the actual text of the regex is now further away in the source code from the location where is being used.

Making the code even slightly harder to understand/maintain is not worth it to me to get a non-noticeable performance gain.

I don't feel strongly about this particular instance, so I'll leave it to other maintainers to decide if they want to accept this change.

jandubois avatar May 08 '24 22:05 jandubois

Moving it out of the loops sounds good (if loops are long), not sure about making it global (as mentioned above)

afbjorklund avatar May 10 '24 05:05 afbjorklund

What's the current status?

AkihiroSuda avatar May 30 '24 16:05 AkihiroSuda

@alexandear (Off-topic: what's your CNCF slack ID?)

AkihiroSuda avatar May 30 '24 16:05 AkihiroSuda

@alexandear (Off-topic: what's your CNCF slack ID?)

https://cloud-native.slack.com/team/U075V4TMQPM

alexandear avatar May 30 '24 17:05 alexandear