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

clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatedly #3261

Merged
merged 9 commits into from
May 22, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented May 19, 2024

The __getitem__ method is a magic method that makes the Session class behave like a dictionary so that we can access the value of GMT constants like self["GMT_IS_DATASET"]. self["GMT_IS_DATSET"] is translated to the statement self.__get_item__("GMT_IS_DATASET"), which then calls the GMT C API function GMT_Get_Enum.

Currently, the __getitem__ method calls the GMT_Get_Enum function everytime a constant is accessed, so it's not efficient. To check how many times constants are accessed, I've added a print statement in the __getitem__ method.

In [1]: import pygmt
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL

In [2]: fig = pygmt.Figure()
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL

In [3]: fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL

In [4]: fig.show()
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL
__getitem__ GMT_PAD_DEFAULT
__getitem__ GMT_SESSION_EXTERNAL

As you can see, even in the simplest script, GMT_PAD_DEFAULT and GMT_SESSION_EXTERNAL are called 7 times each.

This PR refactors the __getitem__ method by storing the values of GMT constants in the GMT_CONSTANTS dictionary. When a constant is accessed for the first time, GMT_Get_Enum is called to the get its value and the value is stored in GMT_CONSTANTS. When the constant is accessed later, it will get the value from the GMT_CONSTANTS dictionary directly.

Below is a simple benchmark showing the performance improvement.

Main branch:

In [1]: from pygmt.clib import Session

In [2]: with Session() as lib:
   ...:     %timeit lib["GMT_IS_FILE"]
   ...:
848 ns ± 2.14 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

This branch:

In [1]: from pygmt.clib import Session

In [2]: with Session() as lib:
   ...:     %timeit lib["GMT_IS_FILE"]
   ...:
66 ns ± 0.0665 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

From the benchmark result, we can conclude that:

  • Accessing a constant for the first time takes about 850 ns
  • Accessing the same constant from the GMT_CONSTANTS dictionary takes about 66 ns, which is 10x faster

This PR doesn't affect our "Benchmarks" workflow, because in our tests, __get_item__ is usually called a few times (like 10), so the execution time may decrease by only 10 ms, which has minor effects on the "Benchmarks" results.

@seisman seisman added run/benchmark Trigger the benchmark workflow in PRs and removed run/benchmark Trigger the benchmark workflow in PRs labels May 19, 2024
@GenericMappingTools GenericMappingTools deleted a comment from codspeed-hq bot May 19, 2024
@seisman seisman added the enhancement Improving an existing feature label May 19, 2024
@seisman seisman added this to the 0.13.0 milestone May 19, 2024
@seisman seisman marked this pull request as ready for review May 19, 2024 07:05
@seisman seisman added the needs review This PR has higher priority and needs review. label May 19, 2024
@seisman seisman requested a review from a team May 21, 2024 05:19
seisman and others added 2 commits May 21, 2024 15:32
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels May 21, 2024
@seisman seisman changed the title clib.Session: Refactor the __getitem__ special method for performance clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatly May 22, 2024
@seisman seisman changed the title clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatly clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatedly May 22, 2024
@seisman seisman removed final review call This PR requires final review and approval from a second reviewer run/benchmark Trigger the benchmark workflow in PRs labels May 22, 2024
@seisman seisman merged commit 176693c into main May 22, 2024
20 checks passed
@seisman seisman deleted the refactor/get_enum branch May 22, 2024 15:16
@seisman seisman changed the title clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatedly clib.Session: Refactor the __getitem__ special method to avoid calling API function GMT_Get_Enum repeatedly May 25, 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.

None yet

3 participants