Skip to content

Conversation

@cspotcode
Copy link
Collaborator

This is a draft meant to accompany Discord conversation, but is not meant to be merged. Individual commits will, I hope, be useful and can be cherry-picked into separate pull requests.

The reports are confusing, I need to clean them up. But basically, every commit on this branch was checked out, and then the benchmark was run. The results show most recent commits first, older commits at the bottom. If you see a noticeable change in performance between one commit and the previous commit, then you know it made things faster/slower, and you can look at the changes within that specific commit. We can choose the commits that were most beneficial and extract them into a separate pull request.

If you try to run these benchmarks yourself, you'll need to tweak bench.sh and install hyperfine. When I come back to this, I'll add proper instructions.

Over two benchmark runs, these are my results.

The big improvements came from:

Command Mean [s] Min [s] Max [s] Relative
python -m benchmarks.collisions.bench d93ba54ace7feb49cd949856a28d4e88977b1265 17.273 ± 0.263 17.087 17.458 1.12 ± 0.02
python -m benchmarks.collisions.bench 055c42413039aa40c458e93663c60053f1dd66e9 15.485 ± 0.091 15.421 15.550 1.00
python -m benchmarks.collisions.bench 8d5d889a0f0eb9cfcf1d61122b902b61bfdc7d46 18.247 ± 0.281 18.048 18.445 1.18 ± 0.02
python -m benchmarks.collisions.bench 39633527f4a2d3ab8454a74dd284a89b9ceb2bef 18.573 ± 0.059 18.531 18.615 1.20 ± 0.01
python -m benchmarks.collisions.bench 68a5c838dad6afd5e996ee8ea3a7d2c96d243333 18.911 ± 0.214 18.759 19.063 1.22 ± 0.02
python -m benchmarks.collisions.bench 5fcbcb68600d07b4957a0f797351ba87deb16620 18.868 ± 0.134 18.774 18.963 1.22 ± 0.01
python -m benchmarks.collisions.bench d5cbb312c4c8a83388acfb96f83a683a527d87fc 18.868 ± 0.153 18.760 18.976 1.22 ± 0.01
python -m benchmarks.collisions.bench e1aebe50f5f79908f361ce587736e4b5c3d09001 20.312 ± 0.538 19.932 20.693 1.31 ± 0.04
python -m benchmarks.collisions.bench 89d03fd156376d9c1ff8772ce11d3c062f74c1de 19.919 ± 0.163 19.804 20.034 1.29 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
python -m benchmarks.collisions.bench d93ba54ace7feb49cd949856a28d4e88977b1265 16.523 ± 0.381 16.253 16.792 1.05 ± 0.02
python -m benchmarks.collisions.bench 055c42413039aa40c458e93663c60053f1dd66e9 15.762 ± 0.047 15.729 15.795 1.00
python -m benchmarks.collisions.bench 8d5d889a0f0eb9cfcf1d61122b902b61bfdc7d46 18.625 ± 0.114 18.544 18.706 1.18 ± 0.01
python -m benchmarks.collisions.bench 39633527f4a2d3ab8454a74dd284a89b9ceb2bef 18.350 ± 0.028 18.330 18.369 1.16 ± 0.00
python -m benchmarks.collisions.bench 68a5c838dad6afd5e996ee8ea3a7d2c96d243333 18.650 ± 0.043 18.620 18.680 1.18 ± 0.00
python -m benchmarks.collisions.bench 5fcbcb68600d07b4957a0f797351ba87deb16620 18.926 ± 0.124 18.839 19.013 1.20 ± 0.01
python -m benchmarks.collisions.bench d5cbb312c4c8a83388acfb96f83a683a527d87fc 18.871 ± 0.149 18.766 18.976 1.20 ± 0.01
python -m benchmarks.collisions.bench e1aebe50f5f79908f361ce587736e4b5c3d09001 19.841 ± 0.107 19.765 19.916 1.26 ± 0.01
python -m benchmarks.collisions.bench 89d03fd156376d9c1ff8772ce11d3c062f74c1de 19.939 ± 0.102 19.867 20.011 1.26 ± 0.01

@cspotcode cspotcode force-pushed the collision-detection-optimizations branch from d93ba54 to 5080434 Compare February 10, 2023 20:13
@cspotcode
Copy link
Collaborator Author

I added some new optimization experiments, I improved the benchmark script, and here's an updated report:

Command Mean [s] Min [s] Max [s] Relative
50804348 HACK for common hitboxes with parallel sides 24.684 ± 0.353 24.331 25.173 1.01 ± 0.02
0c0d5480 top/bottom/left/right: pass generator to min/max 24.430 ± 0.292 24.231 24.858 1.00 ± 0.01
2e32c8f8 get_adjusted_hit_box: avoid rounding off coords after rotation 24.427 ± 0.137 24.232 24.526 1.00
c9a17ce4 sprite.py: avoid math. attribute access by importing individual items at top of module 27.563 ± 0.201 27.311 27.780 1.13 ± 0.01
c2e8d482 check_for_collision_with_list: micro-opt 27.768 ± 0.318 27.526 28.237 1.14 ± 0.01
d1ca44d0 get_adjusted_hit_box: replace generator expression with list comprehension, even though is immediately converted to a tuple 27.413 ± 0.209 27.170 27.597 1.12 ± 0.01
537b2026 _check_for_collision: optimize broadphase 28.061 ± 0.284 27.782 28.319 1.15 ± 0.01
574c2c92 are_polygons_intersecting: misc tweaks 33.429 ± 0.099 33.342 33.544 1.37 ± 0.01
694acb88 are_polygons_intersecting: remove cast calls 33.726 ± 0.230 33.517 33.967 1.38 ± 0.01
7fb53939 get_adjusted_hit_box: use positional-only params in local function 34.284 ± 0.279 33.868 34.455 1.40 ± 0.01
f76be2f2 get_adjusted_hit_box: inline rotate_point() call 34.454 ± 0.191 34.258 34.646 1.41 ± 0.01
303c5056 get_adjusted_hit_box: avoid self. attr lookup in _adjust_point closure 36.349 ± 0.108 36.271 36.509 1.49 ± 0.01
195461c4 Add benchmark script 36.194 ± 0.281 35.917 36.583 1.48 ± 0.01

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.

1 participant