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

Made Surface.set_at() fastcall #2330

Merged
merged 8 commits into from
Jul 28, 2023

Conversation

itzpr3d4t0r
Copy link
Member

This PR aims to get a quick and easy perf improvement with fastcall as well as removing a redundant check.

Results:
TLDR, about 34-40% improvement on my machine.

OLD
mean: 0.226506186666514
mean: 0.8316974133334346
mean: 3.5231991799999682
NEW
mean: 0.16023285333330933
mean: 0.6178482066666523
mean: 2.6118607199999437

Code Used:

from statistics import mean
from timeit import repeat

import pygame

pygame.init()

screen = pygame.display.set_mode((10, 10))

c = (255, 23, 1)
G = globals()
reps = 15
positions = [
    (0, 0), (1, 0), (0, 1), (1, 1),
    (0, 0), (1, 0), (0, 1), (1, 1),
    (0, 0), (1, 0), (0, 1), (1, 1),
    (0, 0), (1, 0), (0, 1), (1, 1),
]

color = pygame.Color(255, 23, 1)


def test(stmt: str):
    data = repeat(stmt, globals=G, repeat=reps)
    print(f"mean: {mean(data)}")


test("screen.set_at((0, 0), color)")
test_str = """
screen.set_at((0, 0), color)
screen.set_at((1, 0), color)
screen.set_at((0, 1), color)
screen.set_at((1, 1), color)
"""
test(test_str)
test_str = """
for pos in positions:
    screen.set_at(pos, color)
"""
test(test_str)

pygame.quit()

@itzpr3d4t0r itzpr3d4t0r added Performance Related to the speed or resource usage of the project Surface pygame.Surface labels Jul 20, 2023
@itzpr3d4t0r itzpr3d4t0r requested a review from a team as a code owner July 20, 2023 10:04
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.

set_at is typically called in tight loops, so I think this optimisation is pretty good.

The CI is failing on a warning, which I'm sure you figured by now, just needs an explicit cast.

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 now, thanks! 👍

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

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

This conflicts with Scriptline's PR #2111.

Lets try to get that in first, then you can rebase this against that?

Edit: resolved, earlier PR merged.

@ankith26 ankith26 added this to the 2.3.1 milestone Jul 21, 2023
@zoldalma999
Copy link
Member

I'll convert it draft for now, feel free to open it back when the mentioned pr is merged

@zoldalma999 zoldalma999 marked this pull request as draft July 22, 2023 08:14
@Starbuck5 Starbuck5 marked this pull request as ready for review July 22, 2023 09:53
@Starbuck5 Starbuck5 marked this pull request as draft July 22, 2023 09:53
@Starbuck5 Starbuck5 dismissed their stale review July 22, 2023 09:54

Resolved.

@itzpr3d4t0r itzpr3d4t0r marked this pull request as ready for review July 22, 2023 10:16
src_c/surface.c Outdated Show resolved Hide resolved
src_c/surface.c Outdated Show resolved Hide resolved
src_c/surface.c Outdated Show resolved Hide resolved
@ankith26
Copy link
Member

To the person merging this: squash and merge this PR so that it doesn't conflict with my other PR. It would probably add me as a co-author of this PR, which you can remove if you want

Copy link
Member

@zoldalma999 zoldalma999 left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks! Nice speed ups!

@zoldalma999 zoldalma999 merged commit e9c4115 into pygame-community:main Jul 28, 2023
31 of 32 checks passed
@itzpr3d4t0r itzpr3d4t0r deleted the set_at_fastcall branch July 30, 2023 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Related to the speed or resource usage of the project Surface pygame.Surface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants