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

Avoid creating reference cycles on every frame #245

Closed
nbraud opened this issue Apr 30, 2019 · 4 comments
Closed

Avoid creating reference cycles on every frame #245

nbraud opened this issue Apr 30, 2019 · 4 comments

Comments

@nbraud
Copy link
Contributor

nbraud commented Apr 30, 2019

While working on instrumenting the main loop (#244), I discovered that we create a reference cycles involving 7 cells on each frame, resulting in the GC actually needing to run (every 100 frames in the default configuration):

[<cell at 0x7fc417d84fa8: function object at 0x7fc417d9c8c8>, 
 <cell at 0x7fc417d84f78: And object at 0x7fc417d98fd0>,
 <function ParserElement.__mul__.<locals>.makeOptionalList at 0x7fc417d9c8c8>,
 (<cell at 0x7fc417d84fa8: function object at 0x7fc417d9c8c8>,
  <cell at 0x7fc417d84f78: And object at 0x7fc417d98fd0>)]

This seems to come from ppb itself (or a dependency), as hugs.py does not seem to create cycles (based on manual analysis of the ways its different datatypes interact).
Not creating reference-cycles means that well-behaved games could avoid running the GC entirely, resulting in lower jitter in the graphics output.

In any case, I believe this is quite low-priority, but I felt that it would be useful to create an issue to record/document the finding. Here is the patch I used to find which cells were involved in reference loops.

@pathunstrom
Copy link
Collaborator

What are the cells and parser elements?

@nbraud
Copy link
Contributor Author

nbraud commented May 1, 2019

@pathunstrom I have no clue; that's the contents of the list gc.garbage (where the CPython GC puts unreachable objects when DEBUG_SAVEALL is set) but it doesn't tell me what allocated it.

@nbraud
Copy link
Contributor Author

nbraud commented May 1, 2019

Nevermind, I just found out what was going on:

  • the 7 cells/frame being in a reference loop were being allocated by the instrumentation code (or rather, by pandas when appending a new row to the dataframe);
  • the adhoc instrumentation was printing the same garbage objects over and over: it clears them gc.garbage, making those objects unreachable... until the garbage collector finds that cycle again, collects it, and adds it to gc.garbage. 😿

Conclusion: ppb already avoids creating reference cycles continuously, which is good :)

@nbraud nbraud closed this as completed May 1, 2019
@nbraud
Copy link
Contributor Author

nbraud commented May 1, 2019

PS: I did fix the GC instrumentation while confirming was what was going on: 1421110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants