Skip to content

Performance degradation with Pygame 2.0.0 and 2.1.0 on Raspberry Pi #2883

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

Open
salem-ok opened this issue Dec 4, 2021 · 5 comments
Open
Labels
bug Performance Related to the speed or resource usage of the project Platform: Raspberry Pi Issues with the Raspberry Pi platform

Comments

@salem-ok
Copy link

salem-ok commented Dec 4, 2021

Environment:

You can get some of this info from the text that pops up in the console when you run a pygame program.

  • Operating system: Raspbian GNU/Linux 10 (buster)
  • Python version : 3.7.3
  • SDL version : 2.0.0
  • PyGame version: 2.0.0 same issue reproduced on 2.1.0
  • Relevant hardware : Raspberry Pi 4 4Gb RAM

Current behavior:
My game runs at 18 FPS on average with pygame 2.0.0 and 2.1.0

Expected behavior:

My game runs at 30 FPS on average with pygame 1.9.4

Screenshots

N/A

Steps to reproduce:

Please explain the steps required to duplicate the issue, especially if you are able to provide a sample application.
if the bug is caused by a specific file (image, font, sound, level, please upload it as an attachment

  1. Clone my repository https://github.com/salem-ok/PySprint.git
  2. run pysprint.py with various versions of pygame on a Raspberry Pi 4 running Buster
  3. Check the FPS counter at bottom right during a race
@salem-ok salem-ok added the bug label Dec 4, 2021
@MyreMylar
Copy link
Contributor

MyreMylar commented Dec 4, 2021 via email

@MyreMylar
Copy link
Contributor

MyreMylar commented Dec 4, 2021 via email

@ankith26
Copy link
Contributor

ankith26 commented Dec 5, 2021

hmm, while the new blitter might be responsible for some of the slowdown on the pi (due to the fact that neon optimisations are disabled by default), I don't think it fully accounts for the slowdown on the pi, there might be more stuff that causes this performance degrade. I will do some benchmarking and profiling on my pi later and post my findings here

@ankith26
Copy link
Contributor

ankith26 commented Dec 5, 2021

Ok, I've done some benchmarking and profiling on my pi3 that runs PiOS bullseye (latest) and the results are not-so-good for pygame.
I used this game to benchmark pygame, but I believe most pygame apps on PiOS experience a significant slowdown.
The game I am testing plays well without any struggle at 60 FPS on my windows laptop (the FPS is capped at 60 by the game).
To test on the pi, I had to reduce the size of the game window to 360x480 to fit my display

On the pi

  • Default pygame 2.1.0: The games struggles between 7-8 FPS, it's quite low and the game is unplayable on the pi at this point
  • Default pygame 2.1.0 + SDL blitter enabled with PYGAME_BLEND_ALPHA_SDL2: Slightly better, 14-15 FPS
  • I then compile pygame from source with -enable-arm-neon which enables alphablit SIMD for the pi, and now the game plays between 18-30 FPS. There seem to be a lot of fluctuations in FPS with lag spikes dropping the FPS to 18, but atleast the game is more playable now
  • Then finally, ye olde pygame 1.9.6. I had to make a few minor source changes to get this to work, but the game plays at 50-60 FPS on pygame 1.9.6

To investigate the reason for the slowdown and eliminate any other sources of potential slowdown, I profile the game (using pygame 2.1.0 at this point with pygame blitter at default without SIMD) and indeed, Surface.blit is the culpit. In a game that ran for 20 seconds, Surface.blit seems to run for 13.5 seconds, almost 2/3rds of the CPU time is spent blitting, which is quite a lot, no other function even comes close to this, the next most expensive function is display.flip which has a cumulative time of 3 ish seconds. Profiling the same game on windows shows that most of the time is spent with the CPU sleeping while capping the FPS at 60 in Clock.tick
image

Comparing time per call of Surface.blit, it is 2.156e-05 s on my windows laptop and 2.089e-03 s on the Pi, that's literally almost a 100x slowdown! And display.flip seems to be 10x slower on the Pi than windows

@MyreMylar
Copy link
Contributor

MyreMylar commented Dec 5, 2021

There was a bit of memory jogging over on the discord in which we recalled that SDL disabled their arm SIMD blitters by default a while ago (2.0.14 maybe?), and they haven't been re-enabled again yet.

So, if you want the best Arm/Pi performance, and don't care about visual impacts/differences to non-arm, then you need to either:

a) build SDL 2 yourself on the Pi with the arm blitters enabled.
b) find a sufficiently old version of SDL 2 that the SIMD blitters were still turned on.

then use the pygame environment variable PYGAME_BLEND_ALPHA_SDL2 to switch to using the SDL 2 blitter over the pygame default one.

You could also explore using pre-multiplied alpha-blending (flags=pygame.BLEND_PREMULTIPLIED on blits) in your application.

Relevant SDL PRs & issue here:

Arm SIMD blitters disabled: libsdl-org/SDL@363fd52

Ongoing work to re-enable them: libsdl-org/SDL#4484

At least that should do it.

@ankith26 ankith26 added Platform: Raspberry Pi Issues with the Raspberry Pi platform Performance Related to the speed or resource usage of the project labels Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Performance Related to the speed or resource usage of the project Platform: Raspberry Pi Issues with the Raspberry Pi platform
Projects
None yet
Development

No branches or pull requests

3 participants