Skip to content
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

micro-optimizations for is_windows() et al. #14206

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

bruchar1
Copy link
Member

Use the lazy_property decorator to replace the is_windows() function with attribute lookup. Since those functions are used everywhere in many loops, this may lead to a small performance improvement.

Comment on lines -33 to -36
def is_windows() -> bool:
platname = platform.system().lower()
return platname == 'windows'

Copy link
Member

Choose a reason for hiding this comment

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

We could also just make this a global constant just like _in_ci, no need to make it specifically be a class attribute on the logger.

Copy link
Member

Choose a reason for hiding this comment

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

Same with all the rest, really.

@bruchar1 bruchar1 force-pushed the optimize-is_windows-et-al branch from 903c3ab to cadbdcd Compare January 29, 2025 16:21
Comment on lines 294 to 299
@lazy_property
def is_windows(self) -> bool:
"""
Machine is windows?
"""
return self.system == 'windows'
Copy link
Member

Choose a reason for hiding this comment

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

In particular, all the envconfig functions literally translate a string comparison against a single property to a named function.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, what do you suggest? To drop that commit, or to precompute them in the __init__ function?

Copy link
Member

@eli-schwartz eli-schwartz Jan 29, 2025

Choose a reason for hiding this comment

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

We could also just make this a global constant just like _in_ci, no need to make it specifically be a class attribute on the logger.

Same with all the rest, really.

Here, it didn't used to be a global constant, and the point is just that if we're going to convert it from a method to a property (lazy or not) we might as well make it a precomputed property

I'm assuming the string comparison doesn't show up in profiling, but tons of function calls do... Performing a small handful of string comparisons at startup should be really cheap.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is better like that? If so, I will just squash the 2 commits.

@bruchar1 bruchar1 force-pushed the optimize-is_windows-et-al branch from cadbdcd to e16bbee Compare January 29, 2025 16:48
@@ -350,7 +350,7 @@ def get_target_filename_for_linking(self, target: T.Union[build.Target, build.Cu
if isinstance(target, build.SharedLibrary):
link_lib = target.get_import_filename() or target.get_filename()
# In AIX, if we archive .so, the blibpath must link to archived shared library otherwise to the .so file.
if mesonlib.is_aix() and target.aix_so_archive:
if mesonlib.Platform.is_aix and target.aix_so_archive:
Copy link
Member

Choose a reason for hiding this comment

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

This is sorta tangental, but I'm not sure the use of meson.is* is actually correct here, I think this needs to be Environment.machines[target.for_machine].is_aix

Copy link
Member

Choose a reason for hiding this comment

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

Maybe cross-compiling to AIX is less common than cross compiling between Windows and Linux. :D

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely true :D

@dcbaker
Copy link
Member

dcbaker commented Jan 29, 2025

Very side note here, the only place where it's guaranteed to be correct to use the mesonlib.is_* functions is in the unittests, basically every other use should be replaced by an Environment.machines[machine].is_*, to be cross compile safe.

@bruchar1 bruchar1 force-pushed the optimize-is_windows-et-al branch from ac23f65 to 249f924 Compare January 29, 2025 18:18
It is better like that? If so, I will squash it with the other commit
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.

3 participants