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

gid and tests #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

JaskRendix
Copy link
Contributor

So, I wanted to walk you through some improvements I made to the decode_gid function. One of the main things I did was implement a global cache. Essentially, a dictionary called flag_cache that stores the mapping between raw GIDs and their corresponding TileFlags objects.

This way, if we encounter the same GID again, we can just grab the cached result instead of recalculating it.

Everything else are additional tests.

Do we need to implement a ValueError if raw_gid < 0?

@joereynolds
Copy link

Out of curiosity did you profile/time this at any point?
Curious how big of an affect this has on loading a map

@JaskRendix
Copy link
Contributor Author

JaskRendix commented Sep 5, 2024

@joereynolds what do you think? I used max_gid 2**29-1, this because the TMX file format uses 29 bits to represent the gid value, with the remaining 3 bits being used for flags. Before I made a mistake by using 2**32-1. Note decode_gid2 is the old one.

    def test_cache_performance(self):
        num_gids = (see below calls)
        gids = [random.randint(0, 2**29 -1) for _ in range(num_gids)]
        with cProfile.Profile() as profile:
            for gid in gids:
                decode_gid(gid)
        stats = pstats.Stats(profile)
        stats.strip_dirs()
        stats.sort_stats("cumulative")
        print("Old decode gid (no cache)")
        stats.print_stats()
        [...]

with 10'000'000 calls

Old decode gid (no cache) --> 10000002 function calls in 1.335 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 10000000    1.335    0.000    1.335    0.000 pytmx.py:147(decode_gid2)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

New decode gid --> 10000002 function calls in 1.338 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 10000000    1.338    0.000    1.338    0.000 pytmx.py:121(decode_gid)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

with 1'000'000 calls

Old decode gid (no cache) --> 1000002 function calls in 0.129 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.128    0.000    0.128    0.000 pytmx.py:147(decode_gid2)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

New decode gid --> 1000002 function calls in 0.125 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.125    0.000    0.125    0.000 pytmx.py:121(decode_gid)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

with 100'000 calls

Old decode gid (no cache)  --> 100002 function calls in 0.013 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100000    0.012    0.000    0.012    0.000 pytmx.py:147(decode_gid2)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

New decode gid --> 100002 function calls in 0.013 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100000    0.013    0.000    0.013    0.000 pytmx.py:121(decode_gid)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

with 10'000 calls

Old decode gid (no cache) --> 10002 function calls in 0.001 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.001    0.000    0.001    0.000 pytmx.py:147(decode_gid2)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

New decode gid --> 10002 function calls in 0.001 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.001    0.000    0.001    0.000 pytmx.py:121(decode_gid)
        1    0.000    0.000    0.000    0.000 cProfile.py:119(__exit__)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

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