Skip to content

ENH: improve performance of deprecate_positional#2283

Merged
mwtoews merged 9 commits intoshapely:mainfrom
Zaczero:main
May 3, 2025
Merged

ENH: improve performance of deprecate_positional#2283
mwtoews merged 9 commits intoshapely:mainfrom
Zaczero:main

Conversation

@Zaczero
Copy link
Copy Markdown
Contributor

@Zaczero Zaczero commented Apr 23, 2025

Closes #2282
Closes #2280

It's my first time contributing here.

  • I'm aware that shapely is generally untyped.
  • Types used here are very similar to shapely/strtree.py, and because of that I decided to include them.
  • Allow edits by maintainers is checked so feel free to push commits for minor issues

Potentially useful info for reviewers:

__code__ docs reference (from Python 3.9): https://docs.python.org/3.9/library/inspect.html

  • co_argcount: number of arguments (not including keyword only arguments, * or ** args)
  • co_varnames: tuple of names of arguments and local variables

This lets us skip the heavy inspect logic.

Another big optimization is the n > warn_from check and LRU cache of the messages, so people who don't resolve the deprecation warning aren't affected too much performance-wise.

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14639955201

Details

  • 31 of 31 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 88.218%

Totals Coverage Status
Change from base Build 14238979238: 0.3%
Covered Lines: 2733
Relevant Lines: 3098

💛 - Coveralls

Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Thanks for your first contribution, it looks great! Have you verified the timing to see if it improves the bottlenecks?

Shapely is mostly untyped for no good reason, perhaps just a low priority. But for new(ish) methods, adding typing hints is welcome from my perspective.

I'll find some more time in the next few days to take deeper look at this PR, but it looks pretty close to ready.

@Zaczero
Copy link
Copy Markdown
Contributor Author

Zaczero commented Apr 28, 2025

Have you verified the timing to see if it improves the bottlenecks?

No. I expect there to be no more measurable performance overhead (except for the typical decorator wrap overhead) as there is now only one len(...) call, which is very lightweight, compared to the previous full signature machinery. I'll run a benchmark if I find time (or if you insist). I would be surprised if there was anything more to it.

@jorisvandenbossche
Copy link
Copy Markdown
Member

@Zaczero thanks a lot for the PR!

I had checked the timing and profile when checking #2280, so re-running that confirms your assumption there is now basically no overhead left:

In [1]: import shapely

In [2]: %%timeit
   ...: for _ in range(1000):
   ...:     coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
   ...:     polygon = shapely.Polygon(coords)
   ...: 
21.7 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)   # shapely 2.0.6
107 ms ± 8.73 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)     # shapely main
22.5 ms ± 1.52 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)    # this branch

@jorisvandenbossche jorisvandenbossche added this to the 2.1.1 milestone May 2, 2025
Copy link
Copy Markdown
Member

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Finally got around to checking code, and indeed it restores the performance. Addition of missing tests is also very welcome. Thanks for the time assembling this PR, we should see this in a patch release anticipated soon!

@mwtoews mwtoews merged commit c1e3e7d into shapely:main May 3, 2025
26 checks passed
hillwithsmallfields pushed a commit to hillwithsmallfields/shapely that referenced this pull request Dec 9, 2025
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.

deprecate_positional is a performance bottleneck (300%-1000% slowdown) in Shapely 2.1 2.1 Polygon creation is much slower than 2.0.7

4 participants