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

Performance improvement #161

Merged
merged 4 commits into from
Oct 17, 2024
Merged

Conversation

vincentsarago
Copy link
Member

while working on #160 I wanted to see what were the bottleneck and found that the TileMatrixSets.matrix(z: int) method was the first method slowing things down

import morecantile
import cProfile
import pstats

tms = morecantile.tms.get("WebMercatorQuad")

def get_tile_bounds():
    for n in range(10000):
        _ = tms.xy_bounds(1, 40, 7)
    return

prof = cProfile.Profile()
retval = prof.runcall(get_tile_bounds)
ps = pstats.Stats(prof)
ps.strip_dirs().sort_stats("tottime", "ncalls").print_stats()

         290002 function calls in 0.143 seconds

   Ordered by: internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    20000    0.047    0.000    0.047    0.000 models.py:766(matrix)
    30000    0.021    0.000    0.034    0.000 utils.py:13(_parse_tile_arg)
    10000    0.016    0.000    0.051    0.000 models.py:1024(_ul)
    10000    0.014    0.000    0.071    0.000 models.py:1052(_lr)
    10000    0.011    0.000    0.147    0.000 models.py:1081(xy_bounds)
    80000    0.010    0.000    0.010    0.000 {built-in method __new__ of type object at 0x102feea38}
    20000    0.008    0.000    0.013    0.000 models.py:817(_matrix_origin)
    30000    0.006    0.000    0.009    0.000 <string>:1(<lambda>)
    60000    0.004    0.000    0.004    0.000 {built-in method builtins.len}
        1    0.003    0.003    0.151    0.151 2108246623.py:1(get_tile_bounds)
    20000    0.001    0.000    0.001    0.000 {built-in method math.floor}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In this PR I'm adding a _tile_matrices_idx: Dict[str, int] private attribute, which will be used for faster lookup (using dictionary is faster than looping through a list

The second improvement proposed in this PR is the removal of duplicated calls to the _parse_tile_arg function by changing the input for the _ul/ul/_lr/lr methods which accepted Tuple or Tile objects. They now require Tile object.

results:

         290002 function calls (270002 primitive calls) in 0.097 seconds

   Ordered by: internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    20000    0.013    0.000    0.041    0.000 models.py:771(matrix)
    10000    0.012    0.000    0.041    0.000 models.py:1028(_ul)
    10000    0.012    0.000    0.040    0.000 models.py:1055(_lr)
    20000    0.010    0.000    0.011    0.000 fields.py:895(__getattr__)
    10000    0.009    0.000    0.101    0.000 models.py:1082(xy_bounds)
40000/20000    0.009    0.000    0.019    0.000 {built-in method builtins.hasattr}
    20000    0.008    0.000    0.012    0.000 models.py:821(_matrix_origin)
    20000    0.007    0.000    0.026    0.000 main.py:781(__getattr__)
    60000    0.005    0.000    0.005    0.000 {built-in method __new__ of type object at 0x103986a38}
    10000    0.004    0.000    0.008    0.000 utils.py:13(_parse_tile_arg)
        1    0.004    0.004    0.105    0.105 2108246623.py:1(get_tile_bounds)
    20000    0.002    0.000    0.002    0.000 {method 'get' of 'dict' objects}
    10000    0.002    0.000    0.003    0.000 <string>:1(<lambda>)
    20000    0.001    0.000    0.001    0.000 {built-in method builtins.len}
    20000    0.001    0.000    0.001    0.000 {built-in method math.floor}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

@vincentsarago
Copy link
Member Author

🤔 maybe instead of introducing breaking change I can update the functions called by bounds() and xy_bounds() method and not touch the other methods

morecantile/models.py Outdated Show resolved Hide resolved
morecantile/models.py Outdated Show resolved Hide resolved
@vincentsarago
Copy link
Member Author

with last commit

         170009 function calls (160009 primitive calls) in 0.059 seconds

   Ordered by: internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.020    0.000    0.059    0.000 models.py:1086(xy_bounds)
    10000    0.006    0.000    0.020    0.000 models.py:772(matrix)
    10000    0.005    0.000    0.006    0.000 fields.py:895(__getattr__)
20000/10000    0.005    0.000    0.010    0.000 {built-in method builtins.hasattr}
    10000    0.004    0.000    0.008    0.000 utils.py:13(_parse_tile_arg)
    10000    0.004    0.000    0.007    0.000 models.py:822(_matrix_origin)
    10000    0.004    0.000    0.014    0.000 main.py:781(__getattr__)
        1    0.003    0.003    0.062    0.062 195867796.py:7(get_tile_bounds)
    30000    0.003    0.000    0.003    0.000 {built-in method __new__ of type object at 0x103232a38}
    10000    0.002    0.000    0.003    0.000 <string>:1(<lambda>)
    20000    0.001    0.000    0.001    0.000 {built-in method builtins.len}
    20000    0.001    0.000    0.001    0.000 {built-in method math.floor}
    10002    0.001    0.000    0.001    0.000 {method 'get' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 functools.py:973(__get__)
        1    0.000    0.000    0.000    0.000 models.py:168(ordered_axis_inverted)
        1    0.000    0.000    0.000    0.000 models.py:580(_invert_axis)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method '__exit__' of '_thread.RLock' objects}
        1    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}

I've reverted the change to _ul and _lr method to directly do the calculation in xy_bounds and

origin_x
+ (math.floor(t.x / cf) + 1) * matrix.cellSize * cf * matrix.tileWidth
)
bottom = origin_y - (t.y + 1) * matrix.cellSize * matrix.tileHeight
Copy link
Member Author

@vincentsarago vincentsarago Oct 16, 2024

Choose a reason for hiding this comment

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

do all the calculation here instead of calling _ul and _lr method which will duplicate some stuff (matrix lookup, origin, coalecense)

Copy link
Member Author

Choose a reason for hiding this comment

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

cons: we end up having duplicated code

t = _parse_tile_arg(*tile)
_left, _bottom, _right, _top = self.xy_bounds(*tile)
left, top = self.lnglat(_left, _top)
right, bottom = self.lnglat(_right, _bottom)
Copy link
Member Author

Choose a reason for hiding this comment

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

use the fastest xy_bounds methods instead of ul and lr methods

@@ -765,9 +771,8 @@ def custom(

def matrix(self, zoom: int) -> TileMatrix:
Copy link
Member Author

Choose a reason for hiding this comment

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

why not use lru_cache here?

I've read some article that mentioned that using lru cache for class method might result in memory leaks 🤷

@vincentsarago
Copy link
Member Author

# main
%timeit tms.xy_bounds(1, 40, 7)
7.44 µs ± 45.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# This PR 
%timeit tms.xy_bounds(1, 40, 7)
3.8 µs ± 63.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

@vincentsarago vincentsarago changed the title add matrix lookup dict and remove duplicated call to parse_tile_arg Performance improvement Oct 16, 2024
@vincentsarago vincentsarago marked this pull request as ready for review October 17, 2024 20:57
@vincentsarago
Copy link
Member Author

## Main 

----------------------------------------- benchmark 'morecantile.bounds': 7 tests ------------------------------------------
Name (time in us)                                  Min                 Max                Mean              Median          
----------------------------------------------------------------------------------------------------------------------------
morecantile.bounds-Tile(x=0,y=0,z=0)           35.0760 (1.0)       75.6420 (1.00)      35.8244 (1.0)       35.5670 (1.0)    
morecantile.bounds-Tile(x=1,y=0,z=1)           35.4560 (1.01)     107.6420 (1.43)      36.6891 (1.02)      36.0780 (1.01)   
morecantile.bounds-Tile(x=1,y=1,z=1)           35.7070 (1.02)      75.4010 (1.0)       36.5613 (1.02)      36.3080 (1.02)   
morecantile.bounds-Tile(x=1,y=40,z=7)          36.5790 (1.04)      93.9770 (1.25)      37.4655 (1.05)      37.1600 (1.04)   
morecantile.bounds-Tile(x=486,y=332,z=10)      37.3100 (1.06)      82.6840 (1.10)      38.1708 (1.07)      37.8910 (1.07)   
morecantile.bounds-Tile(x=486,y=332,z=20)      38.5020 (1.10)     117.4410 (1.56)      39.9799 (1.12)      39.4940 (1.11)   
morecantile.bounds-Tile(x=486,y=332,z=30)     126.5080 (3.61)     221.2950 (2.93)     130.1808 (3.63)     128.8810 (3.62)   
----------------------------------------------------------------------------------------------------------------------------

---------------------------------------- benchmark 'morecantile.xy_bounds': 7 tests ----------------------------------------
Name (time in us)                                    Min                 Max               Mean             Median          
----------------------------------------------------------------------------------------------------------------------------
morecantile.xy_bounds-Tile(x=0,y=0,z=0)           6.0110 (1.0)       28.1430 (1.0)       6.2460 (1.0)       6.2110 (1.0)    
morecantile.xy_bounds-Tile(x=1,y=1,z=1)           6.1510 (1.02)      30.9480 (1.10)      6.3539 (1.02)      6.3120 (1.02)   
morecantile.xy_bounds-Tile(x=1,y=0,z=1)           6.2020 (1.03)      28.4640 (1.01)      6.4252 (1.03)      6.3920 (1.03)   
morecantile.xy_bounds-Tile(x=1,y=40,z=7)          7.1630 (1.19)      44.2430 (1.57)      7.3898 (1.18)      7.3440 (1.18)   
morecantile.xy_bounds-Tile(x=486,y=332,z=10)      7.7040 (1.28)      31.9600 (1.14)      7.9613 (1.27)      7.9050 (1.27)   
morecantile.xy_bounds-Tile(x=486,y=332,z=20)      9.3570 (1.56)      45.5250 (1.62)      9.6248 (1.54)      9.5670 (1.54)   
morecantile.xy_bounds-Tile(x=486,y=332,z=30)     89.9480 (14.96)    219.0900 (7.78)     92.0518 (14.74)    91.2410 (14.69)  
----------------------------------------------------------------------------------------------------------------------------

#############################################################################################################################
## THIS PR 

---------------------------------------- benchmark 'morecantile.bounds': 7 tests ----------------------------------------
Name (time in us)                                 Min                 Max               Mean             Median          
-------------------------------------------------------------------------------------------------------------------------
morecantile.bounds-Tile(x=0,y=0,z=0)          38.2120 (1.0)      106.0990 (1.33)     39.4564 (1.00)     38.8320 (1.0)    
morecantile.bounds-Tile(x=486,y=332,z=20)     38.3320 (1.00)     114.1940 (1.43)     39.4760 (1.00)     39.0740 (1.01)   
morecantile.bounds-Tile(x=1,y=40,z=7)         38.4920 (1.01)      90.2390 (1.13)     40.0610 (1.02)     39.0040 (1.00)   
morecantile.bounds-Tile(x=1,y=0,z=1)          38.6720 (1.01)      88.0750 (1.10)     39.4304 (1.0)      39.1730 (1.01)   
morecantile.bounds-Tile(x=486,y=332,z=10)     38.7320 (1.01)      79.8900 (1.0)      39.5147 (1.00)     39.2840 (1.01)   
morecantile.bounds-Tile(x=1,y=1,z=1)          38.8930 (1.02)      93.1950 (1.17)     39.7670 (1.01)     39.5040 (1.02)   
morecantile.bounds-Tile(x=486,y=332,z=30)     83.1960 (2.18)     161.5130 (2.02)     86.2243 (2.19)     85.4500 (2.20)   
-------------------------------------------------------------------------------------------------------------------------

---------------------------------------- benchmark 'morecantile.xy_bounds': 7 tests ----------------------------------------
Name (time in us)                                    Min                 Max               Mean             Median          
----------------------------------------------------------------------------------------------------------------------------
morecantile.xy_bounds-Tile(x=1,y=40,z=7)          3.8170 (1.0)       25.7680 (1.0)       3.9653 (1.00)      3.9270 (1.0)    
morecantile.xy_bounds-Tile(x=0,y=0,z=0)           3.8370 (1.01)      41.6990 (1.62)      3.9729 (1.00)      3.9470 (1.01)   
morecantile.xy_bounds-Tile(x=1,y=1,z=1)           3.8370 (1.01)      42.4190 (1.65)      3.9807 (1.00)      3.9470 (1.01)   
morecantile.xy_bounds-Tile(x=1,y=0,z=1)           3.8470 (1.01)      27.8820 (1.08)      3.9614 (1.0)       3.9370 (1.00)   
morecantile.xy_bounds-Tile(x=486,y=332,z=10)      3.8670 (1.01)      34.8660 (1.35)      4.0187 (1.01)      3.9580 (1.01)   
morecantile.xy_bounds-Tile(x=486,y=332,z=20)      3.8770 (1.02)      41.7390 (1.62)      4.0604 (1.03)      4.0170 (1.02)   
morecantile.xy_bounds-Tile(x=486,y=332,z=30)     44.0920 (11.55)    163.7570 (6.36)     46.0675 (11.63)    45.1150 (11.49)  
----------------------------------------------------------------------------------------------------------------------------

@vincentsarago vincentsarago merged commit b2704d2 into main Oct 17, 2024
7 checks passed
@vincentsarago vincentsarago deleted the feature/performance-improvement branch October 17, 2024 21:04
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.

2 participants