Experiments with new low latency PyPy garbage collector in a thread.
TLDR; less performance spikes, faster average performance, PyPy is a good Python for games and multimedia apps on desktop systems, running GC in a thread is a good idea for GIL releasing apps.
In March 2018 at the PyPy winter sprint I spent some days working on improving pygame support on PyPy. Part of this was investigating pypy frame drop. In there I found PyPy had both lower average performance in this pygame-using-CPython-API benchmark, and had spikes which caused frame drops. Remember that in latency sensitive applications performance spikes are not allowed. Note that not very much of this code is pure python (where PyPy is fastest), so it's almost a worst case benchmark for PyPy performance.
Thanks to Antonio, the PyPy team and their supporters — things have much improved since then. One improvement in particular is that PyPy now allows more control of when the Garbage Collection does its work. Read more about it in the blog post "PyPy for low latency systems".
In this post I'm going to compare my experiments with pygame with the experiments done last year. Additionally I'm going to try a new thing to improve performance which wasn't possible last year.
Running garbage collection in a thread.
Now that we can call garbage collection when we want, how about we do it when we release the GIL and do some other processing in C? PyPy doesn't run the garbage collector in a thread by default, but we can do that ourselves.Remember that python has a GIL that can be released when you want to do IO or CPU intensive work. Here is an example program which has a thread waiting to be told when to run the incremental garbage collection step.
# Run pypy GC in background.
from Queue import Queue from threading import Thread import pygame def gc_collect_step(q): """ this calls the gc in a thread. """ while True: print '5) GC THREAD: Waiting to do gc.' ready_to_go = q.get() print '4) GC THREAD: calling gc_collect_step' if hasattr(gc, "collect_step"): gc.collect_step() q.task_done() gc_queue = Queue() worker = Thread(target=gc_collect_step, args=(gc_queue,)) worker.setDaemon(True) worker.start() pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() while True: print('1) at top of loop') pygame.event.get() gc_queue.put(True) print('2) tell GC thread to collect') print('3) flipping display. GIL released') pygame.display.flip() print ('6) finished flipping. GIL grabbed') clock.tick() print(clock.get_fps())
This is how the program executes. Notice how it does display.flip and waits on the OS to do VSYNC at some refresh rate (usually 60FPS). In this spot many games and multimedia apps have some spare time to do other work.
Waiting for VSYNC is the perfect spot to do GC!
Trace of program execution when doing GC in background thread.
1) at top of loopOn my bench-marking setup this gives an increase of 11% frames per second on average. From 42 FPS to 47 FPS on average. Note, for this benchmark python2.7 gets 51 FPS on average.
2) tell GC thread to collect
3) flipping display. GIL released
4) GC THREAD: calling gc_collect_step
5) GC THREAD: Waiting to do gc.
6) finished flipping. GIL grabbed
1) at top of loop
2) tell GC thread to collect
3) flipping display. GIL released
4) GC THREAD: calling gc_collect_step
5) GC THREAD: Waiting to do gc.
6) finished flipping. GIL grabbed
1) at top of loop
2) tell GC thread to collect
3) flipping display. GIL released
4) GC THREAD: calling gc_collect_step
5) GC THREAD: Waiting to do gc.
6) finished flipping. GIL grabbed
Running garbage collection in a thread should be able to improve the performance on pypy of many apps which wait on the GIL.
Our performance goal.
Our performance goal is very little variance between frames and no spikes above 0.013. 0.013 is about the amount of time we have to do our work before we ask the frame to be flipped at 60 times per second.
Python 2.7 baseline measurement.
So we have something to compare against, here is a measurement of Python 2.7.
Note various spikes in performance, 51 FPS on average with many frames falling in the 0.005 to 0.007 range (below the required 0.013). The the funny spike at the start. I'm not sure why this is happening now compared to when I measured last year. The underlying OS has changed, or perhaps some other software is running on my computer.
Remember: Our performance goal is very little variance between frames and no spikes above 0.013. 0.013 is about the amount of time we have to do our work before we ask the frame to be flipped.
Python 2.7 isn't that bad here. It doesn't spike over 0.013 but it does have quite a lot of variance in some frames.
PyPy 6.0.0 (last release, does not include new GC stuff).
What about PyPy 6.0.0 which came out some months after my experiments last year. Here we see 0.005 to 0.010 with some spikes of around 0.0125 and higher. This older version of PyPy fails the performance goal.PyPy 6.0.0 gc.disable().
What if we just disable the garbage collector entirely? (Apart from memory growing until the program starts swapping or crashes). After a few seconds it just starts to slow down a lot.
Time per frame on PyPy nightly (6.1.0 alpha0). gc.disable(), gc.collect_step().
This is using gc.disable() at the top of the main loop, and calling the new gc.collect_step() just before display.flip when most of the work has been done already. Remember that display.flip releases the GIL and waits for VSYNC at 60FPS.
Here we see the average FPS at around 42 with times mostly between 0.006 and 0.008. At the beginning and end there are some spikes. But mostly things stay below the magical 0.015 seconds required to get below 60 FPS. Here is another measurement.
With this second measurement we see that something is going on at the start and at the end again. Again, mostly there are few spikes above 0.015 and things mostly stay between 0.006 and 0.008. This capture does show one spike, but I found this hard to reproduce.
This is a great improvement over CPython in all except average FPS. The timings in the graphs are the work that is done before display.flip is called.
PyPy nightly (6.1.0 alpha) GC in a thread, gc.disable().
Finally we have the GC running in a thread. This gives us 47.5 FPS on average. Most times stay between 0.6 and 0.8, however now there is a large number of spikes. None of them go above 0.015, but quite a few go above our goal of 0.013.
Why are these spikes here? I'm not sure. It needs to be investigated some more.
I ran the benchmark a few more times, and it seemed to spike less.
So then I switched to using threading.Event instead of threading.Queue, and there seemed to be less spikes over multiple runs. Probably using a lower level synchronization primitive (like threading.Lock) could improve things more. Also using a lower level primitive at C level would probably work even better. Additionally I'm not using the pypy36 which has some (hopefully) improved threading.Event and threading.Queue implementations.
Here's the example using Event instead of Queue.
from threading import Event, Thread, Lock import pygame def gc_collect_step(event): """ this calls the gc in a thread. """ while True: print '5) GC THREAD: Waiting to do gc.' event.wait() print '4) GC THREAD: calling gc_collect_step' if hasattr(gc, "collect_step"): gc.collect_step() event.clear() gc_event = Event() worker = Thread(target=gc_collect_step, args=(gc_event,)) worker.setDaemon(True) worker.start() pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() while True: print('1) at top of loop') pygame.event.get() gc_event.set() print('2) tell GC thread to collect') print('3) flipping display. GIL released') pygame.display.flip() print ('6) finished flipping. GIL grabbed') clock.tick() print(clock.get_fps())
Also, you can find the pypy-bench-testsprite.py in a gist. It needs to be run within the pygame examples folder.
PyPy meets performance goals for games
In conclusion:/Happy dance/The results that Antonio posted in "PyPy for low latency systems" also appear in the experiments I made. Worst case performance can be increased, and the performance spikes can be removed.
PyPy meets the performance goals for games now if you disable the gc at the top of the main loop, and call the gc collection every frame. The spikes that were there before are gone. The variance in performance is actually less than CPython.
Calling GC in a thread is a viable option which increases the average FPS very close to CPython levels. However some more work needs to be done to try and find a way that avoids the increased performance jitter. Note, that this jitter is still at acceptable levels.
Running the GC in a thread can probably be used for a peak performance increase by many apps which release the GIL for IO or CPU intensive operations.
I'd like to remind people that this benchmark is a worst case for PyPy and games. In that there is also CPython API used rather than CFFI(fasted ffi on PyPy), and the benchmark contained not much pure python code. If you were also doing physics, collision detection and other stuff in pure python PyPy would pull ahead. Also this was using pygame with SDL1 so that I could sort-of compare performance to the previous time. Pygame 2 with SDL2 runs this benchmark much faster because of hardware acceleration. Another reminder that PyPy 6.1.0 isn't released yet, so the results will differ compared to the nightly version.
The combination of PyPy's JIT with the Cython ahead of time compiler makes a formidable combination. I'm looking forward to using them all with the hardware acceleration that pygame 2 provides (Vulkan, Metal, OpenGL, Direct3d, ...).
Comments
Attempt office.com/setup which is extremely simple to install, download and recover. Utilization of it is additionally straightforward and the client can become familiar with the office.com/setup utilization of it without any problem. Online Support&help alternative is likewise accessible in all application which gives a moment rule.Microsoft Office Setup is the office.com/setup complete bundle of Microsoft programs as it takes to the a variety of jobs, servers, and affiliations like PowerPoint, Excel, Word, Outlook, Publisher, OneNote, and Access.A progressing Microsoft statement gives some comprehension of how the association designs the office.com/setup landing of new Office 365 ProPlus features. Microsoft uses the equivalent "channels" language for MS Office 365 ProPlus incorporates revives as it does with Windows 10 feature invigorates.
Download norton antivirus to make your computer virus norton.com/setup free with the best support and tech team. Feel free to contact us
Avg retail activation the technical world is improvising day by avg.com/retail day and as per the technology is improvising the online.A Complete Guide of avg.com/retail antivirus. AVG antivirus is the most trusted and prominent brand among other antivirus.AVG Antivirus, developed avg.com/retail AVG Technologies, is antivirus software that comes with highly advanced security tools. AVG is good for malware protection for AVG.
Prime Video was previously called Amazon Video and before that it was called Amazon Instant Video. Through Amazon Prime you can enjoy new and old movies and TV service. Amazon Prime is very primevideo.com/mytv well known for its name as well as its services. Amazon Prime is the best American Internet video on demand service.
Amazon.com/mytv is a video streaming service available for Amazon Prime members. With an eligible Amazon Prime membership, you have access to thousands of Prime Video titles at no additional cost.Choose Register option on the Amazon website- to get a 5–6 character code, then sign in to your Amazon account and enter your code to enjoy watching your favorite movies at. amazon.com/mytv If you are facing any kind of issue regarding amazon tv registration, login or any other troubleshooting visit to get the best and simple way to resolve all your issues. We provide 24*7 services to our users.
my.Roku.com is the free official site to link, activate, set-up and manage roku.com/link your Roku player or Roku TV. Roku never charges for linking or set-up support.Steps to activate Roku Device · Open web browser, go to the Roku.com/link · Enter the 4 digit code displayed on your TV /Device· roku.com/link Fill in all the details like name.
Amazon.com mytv is a registration portal of Amazon Prime amazon.com/mytv from which members can watch thousands of movies and TV shows.
Giving users an unparalleled streaming experience, Team roku.com/link takes great pride in being the number one streaming service providers in the world.
To Activating Hulu, you have to visit www.hulu.com/activate. Enter the code offered on your hulu.com/activate TV screen and clue in to your Hulu account. Article by Activate.
Norton web security is commonly used antivirus gives the least requesting to use and most intutive affirmation for your PC and your mobiles .present it and negligence viruses,spyware,root-units - , Download norton 360 hackers.for more nuances visit. norton.com/setup To enact the Norton setup, select the Activate Now option at the base. To recharge the membership norton.com/setup for Norton, select the Help choice and snap on Enter item key. Cautiously type the right Norton item key in the clear. Snap on the Next catch.Go through with for more details norton.com/setup Download Norton Mobile Security and Antivirus application that can shield your records from getting affected from any online malware or contamination norton setup product key.
Go to the Norton Security Online page and click Get Norton Security Online. Type in your Xfinity ID and password, if you're asked. Create a Norton account, then sign in. Choose whether you want to install [norton.com/setup it on this device or another one. Start your installation. Click Run. Let the program run. If Windows asks for permission, click Yes.Norton help to verify your devices against antiviruses and help to progress and keep up your information from compitetior. Present norton.com/setup norton in your pc click here for nuances.To install Norton Security Online on a different computer, log in to that computer and then download Norton. norton.com/setup Comcast now offers Norton Security Online instead of Norton Security Suite, and it is available at the Xfinity website. You are not required to update to Norton Security Online. Norton Security Online has a new look and feel.
The HBO Max content offering is a superset of what was available hbomax.com/tvsignin under the Now moniker, with the same $14.99 monthly subscription price.HBO Max is a new streaming service with an extensive library. hbomax.com/tvsignin Here's an HBO Max show and movies list including original series and more.To complete the HBO Max TV sign in process,hbomax.com/tvsignin choose the button below and then enter the code from your TV. The HBO Store offers free products sometimes ...
Complete the HBO Max TV sign in process by entering the code from your device. hbomax.com/tvsignin Watch and chat now with millions of other fans from around the world.HBO Max is the streaming platform that bundles all of HBO together with even hbomax.com/tvsignin more of your favorite TV series, blockbuster movies, plus new Max Originals.Complete the HBO Max TV sign in process by entering the code from your device. hbomax.com/tvsignin Catch hit movies, popular shows, live news, sports & more the web or on your ..
HBO Max is the latest streaming service to take on Netflix and Disney+. It includes hbomax.com/tvsignin content from HBO Go and HBO Now, WarnerMedia. HBO Max is a new subscription video streaming service launching on Samsung Smart TV on May 27th, which brings the best of HBO with more original hbomax.com/tvsignin content, past seasons of your favorite shows, and movies.Visit HBOMax.com or download the app on your hbomax.com/tvsignin Apple or Android device and login with your Optimum or Altice One account.
security questions correctly. Then you will receive a password reset link on your provided mobile number or email ID. Do exactly as instructed, and the way to Account Live password reset will become smoother.
Call our Windows 10 Support Number: 1-877-814-4455
Check Full Steps here- Change Windows 10 password
dragon naturally speaking
house cleaning service near me
German Language Course in Chennai
German Online Classes
German Language Course in Bangalore
You are also a Verizon user. If you have forgotten your Verizon email password or need help to Recover Verizon Email Password, Qwik Aid Team is here to help. Be that because it may, if you face any problem, you'll contact Qwik Aid experts to assist you together with your Technical inquiry.
With skilled and trained electricians, Todd Peters Electric as a Electrician Corona Ca service provider has come to your doorstep with extreme electrical solutions. We provide quality Electrical Services with careful analysis and observation of client's demands and budgets. If you would like to hire an electrician, simply dial our toll-free number or visit our website.
Assisted Living Aurora
Assisted living centennial
Castle Rock assisted living
Lakewood Gardens Care Homestead
Assisted Living Lakewood
Independent Living Lakewood
Independent Living
The blog was really Excellent Keep up the good work.....
Event management services in chennai
How do I recover my cash app account without a password? If you want to recover your cash app account after losing your password then this is the best place for you. Here you will know how to recover a cash app account without a password you have to do nothing to know this except a click on this link.
ambbet
write-up and also the rest of the site is very good.
토토사이트
토토
토토
토토사이트
토토사이트
I might want to thank you for the efforts you have made in composing this post.
Bra nettside : One Piece
Bra nettside : Zonahobisaya
Bra nettside : Zonahobisaya
Bra nettside : Resep Masakan
Bra nettside : Terbanyak
Bra nettside : lambang
Bra nettside : Zonahobisaya
touchtunes promo codes
asap promo code today
For anyone looking to organize game development events, hackathons, or gaming tournaments, I'd highly recommend checking out this Event Management Agency to help make your event a success!
mejores abogados de divorcio en nueva jersey
abogado dui amelia virginia
qué es un divorcio de mutuo acuerdo en virginia
buen abogado de accidentes automovilísticos
abogado dui dinwiddie virginia
abogado testamentario y testamentario
abogado dui southampton virginia