Skip to content

Conversation

@Starbuck5
Copy link
Member

@Starbuck5 Starbuck5 commented Jun 1, 2025

Take the code pygame.Vector2((10,20)) * 30. This operation is handled in the function vector_generic_math.

Before this PR, the code checks if PyFloat_AsDouble will work using the RealNumber_Check function, then it calls PyFloat_AsDouble. After this PR, it checks if PyFloat_AsDouble will work by calling PyFloat_AsDouble and handling the error if it fails. This avoids a call by us into RealNumber_Check (PyNumber_Check and !PyComplex_Check). I checked the Python source code, the code in PyNumber_Check is checking the same things PyFloat_AsDouble does, and PyComplex doesn't provide the right internal functions to be converted to a float/double.

Test results:

Code Speedup
Vector2 * float 5.7%
Vector2.elementwise + float 5.8%

Results are quite noisy, especially since this is a relatively small optimization. I still it's worth checking in though!

Basic testing script
import pygame
import time
import random

l = [pygame.Vector2(random.random() * 100, random.random() * 100) for _ in range(200)]
le = [v.elementwise() for v in l]

start = time.time()
for _ in range(10000):
    for v in le:
        v + 3.956
print(time.time()-start)

start = time.time()
for _ in range(10000):
    for v in l:
        v * 3.956
print(time.time()-start)

@Starbuck5 Starbuck5 requested a review from a team as a code owner June 1, 2025 10:24
@Starbuck5 Starbuck5 added Performance Related to the speed or resource usage of the project math pygame.math labels Jun 1, 2025
PyFloat_AsDouble does basically the same checks internally, so since this is the last case checked in the generic_math functions in math.c, we can just try to convert directly and handle if if the conversion fails.
@Starbuck5 Starbuck5 force-pushed the math-optimize-remove-generic-math-num-check branch from 885f5c3 to f5702b6 Compare June 1, 2025 10:33
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM thanks! 🎉

@ankith26 ankith26 added this to the 2.5.5 milestone Jun 1, 2025
@Starbuck5 Starbuck5 removed this from the 2.5.5 milestone Jun 7, 2025
@ankith26 ankith26 added this to the 2.5.6 milestone Jun 7, 2025
Copy link
Member

@damusss damusss left a comment

Choose a reason for hiding this comment

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

Thank you for the optimization! 🎉 ❤️

@damusss damusss merged commit d836ecf into pygame-community:main Jun 7, 2025
27 checks passed
@Starbuck5 Starbuck5 deleted the math-optimize-remove-generic-math-num-check branch June 7, 2025 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

math pygame.math Performance Related to the speed or resource usage of the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants