Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Extending Gosu / [Texplay] Ruby GC make the game freeze when using Texplay
- By Gavos Date 2010-02-28 04:59
Hi everyone,

As people advised me to do in, I switched to Texplay for building a single image that is getting drawn to the screen. The performance is very good, except... when Ruby's garbage collector enter the scene!

Everytime it is called it makes a small (but very noticeable) freeze of about 100 ms. The GC seems to be run quite often (once every 2 seconds maybe) and I suppose it comes from the use I make of Texplay. The reason for that is that when I disable the GC ("GC.disable") there is no more freeze but my program reaches the gig in 1 minute, but if I disable the use of Texplay the memory consumption increases much slower (but maybe it is not a good clue as to whether Texplay is the one who makes the GC run that often?)

Now, I would like to know if there is a better way to use Texplay that would not be so memory consuming. Here is what I am doing:

It is quite simple, I read a list of squares to draw and I call "rect" on the picture on each one of them. But it could be done differently maybe? Or I could play with the cache? And if not, does anyone knows any good tricks to "smooth" the garbage collector? But to be honest, I cannot think of any good time to run it manually (if I could) because the game can be continuous, that is without any pause.

Thanks in advance for your answers!
- By banister Date 2010-02-28 09:32
Could you link to the entire game so i can play with it and see what's going on? Never experienced something like this before :)
- By Gavos Date 2010-02-28 13:28
Sure, you can get the "game" here:

We have not managed to make it interesting yet (still need some time). The goal is to cover the biggest surface on the screen. Right-click to create a "stain" anywhere, left-click to create on your on color.

The garbage collector is enabled by default. Just create a few stains in a row and you will notice the small freeze, like the stains pausing their progression and then resuming (it happens at last least once a second, actually). I forgot to say that I am running on Windows, maybe the GC perfoms a very different way on this platform?

The code responsible for the graphics part is in window.rb, and here are a few keystrokes we created for testing:

'E' -> enable garbage collector
'D' -> disable garbage collector

'C' -> clear the screen
'Esc' -> quit the game

'O' -> select round stain
'X' -> select cross shaped stain
'R' -> select red color
'B' -> select blue color
- By banister Date 2010-02-28 14:29

to be honest i did not get much of a noticeable slowdown at all on my system (running windows xp too). However you can try doing the following instead for a slight speedup:

@screen.paint { rect(cell.x*CellSize, cell.y*CellSize, cell.x*CellSize+CellSize-1, cell.y*CellSize+CellSize-1, :color => [1, 0, 0], :fill => true) }

WITH this:

@screen.rect(cell.x*CellSize, cell.y*CellSize, cell.x*CellSize+CellSize-1, cell.y*CellSize+CellSize-1, :color => [1, 0, 0], :fill => true)

This will prevent a temporary block object being created and also eliminate the need for an instance_eval (actually a gen_eval)
- By Gavos Date 2010-02-28 15:16
Hey, your "slight speedup" did the trick!

I am running on a quite old machine (Athlon XP 2000, 6 years old), which might be the reason why I notice the freezes so much. But with your code it is actually *much* smoother. I also tried it with the GC disabled and the memory increase is very slow now, pretty much the same as if I was not using Texplay, I think that confirms that the problem was coming from the way I used the lib. I am quite a beginner with Ruby so I am not very aware of the cost of creating a block object a running _eval.

Thanks a lot! I can get back to working on a better gameplay, now :-)
- By erisdiscord Date 2010-02-28 17:14
I don't know if you've caught this yet, but I clicked on the status bar on the right and the game crashed with this:

window.rb:102:in button_down': undefined method []' for nil:NilClass (NoMethodError)
  from PetriWars.rb:88:in show'
  from PetriWars.rb:88:in

I suspect a test to make sure the pointer is over the playfield might be in order. :)
- By Gavos Date 2010-03-01 18:31
Thanks for your feedback erisdiscord, I had not caught this one and it is now fixed :-)

We (my wife and myself) will probably present the game in the Showcase section when we will consider it to be finished, but if you want you can still try the version we published for the contest here:
- By jlnr (dev) Date 2010-03-01 21:42
Waiting for it—didn't understand it right now :)
- By banister Date 2010-03-02 22:06
I don't quite understand the game either
It all seems to be written in some ancient European language ;)

Post instructions soon, looking good though!
Up Topic Gosu / Extending Gosu / [Texplay] Ruby GC make the game freeze when using Texplay

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill