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

Refactor clib to avoid checking GMT version repeatedly and only check once when loading the GMT library #3254

Merged
merged 3 commits into from
Jul 29, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented May 16, 2024

Description of proposed changes

In the Session.__enter__ method, we check if the GMT library version is equal to or newer than the required version. This special method is called when entering the context manager, i.e., with Session() as lib. So it means we're repeatedly checking the GMT version.

This PR refactors the clib codes to check the GMT version only once when loading the GMT library.

I expect to see some performance improvements but the benchmarks say no. So I believe the performance improvement should be very little.

https://codspeed.io/GenericMappingTools/pygmt/branches/clib/check-version show an average 2% performance improvement.

@seisman seisman added the run/benchmark Trigger the benchmark workflow in PRs label May 16, 2024
@seisman seisman marked this pull request as draft May 16, 2024 16:34
@seisman seisman force-pushed the clib/check-version branch 4 times, most recently from d2ef07d to 90ac95d Compare May 21, 2024 05:31
pygmt/clib/session.py Outdated Show resolved Hide resolved
@seisman seisman force-pushed the clib/check-version branch 5 times, most recently from 07b06ac to b42b758 Compare May 29, 2024 05:38
@seisman seisman force-pushed the clib/check-version branch 2 times, most recently from 833efb4 to 9907af7 Compare July 24, 2024 09:35
@seisman seisman marked this pull request as ready for review July 24, 2024 09:37
@seisman seisman added this to the 0.13.0 milestone Jul 24, 2024
@seisman seisman added maintenance Boring but important stuff for the core devs needs review This PR has higher priority and needs review. labels Jul 24, 2024
@seisman seisman changed the title clib: Refactor to check the GMT version only once Refactor the clib module to check the GMT version only once when loading the GMT library Jul 24, 2024
@seisman seisman changed the title Refactor the clib module to check the GMT version only once when loading the GMT library Refactor the clib module to avoid checking GMT version repeatedly and only check once when loading the GMT library Jul 24, 2024
@seisman seisman force-pushed the clib/check-version branch from 9907af7 to f55d5d4 Compare July 24, 2024 09:42
func.restype = ctypes.c_float
major, minor, patch = ctypes.c_uint(0), ctypes.c_uint(0), ctypes.c_uint(0)
func(None, major, minor, patch)
return f"{major.value}.{minor.value}.{patch.value}"
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on returning an instance of packaging.version.Version instead of a str?

Copy link
Member Author

Choose a reason for hiding this comment

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

but __gmt_version__ should be a string.

Copy link
Member

Choose a reason for hiding this comment

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

Mm ok, could also do str(get_gmt_version()), but ok to just return a string itself. Just thought it could simplify some of the test code.

Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

Just two tiny suggestions. Having the code spread across clib/__init__.py, clib/loading.py and clib/session.py is a little confusing, but seems to make sense after going through it.

pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
pygmt/tests/test_clib.py Outdated Show resolved Hide resolved
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman seisman changed the title Refactor the clib module to avoid checking GMT version repeatedly and only check once when loading the GMT library Refactor clib to avoid checking GMT version repeatedly and only check once when loading the GMT library Jul 29, 2024
@seisman seisman merged commit 3a589fa into main Jul 29, 2024
19 checks passed
@seisman seisman deleted the clib/check-version branch July 29, 2024 02:25
@seisman seisman removed the needs review This PR has higher priority and needs review. label Jul 29, 2024
@seisman seisman removed the run/benchmark Trigger the benchmark workflow in PRs label Jul 29, 2024
@weiji14 weiji14 added enhancement Improving an existing feature and removed maintenance Boring but important stuff for the core devs labels Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants