boskee/Minecraft — pull request #36 by BertrandBordage (2013-04-07 02:30:50+00:00)
Title: Optimize world.py using Cython.

Voilà, some static typing.

I chose to do this on world.py because this file contains what consumes most CPU (apart from pyglet).

According to cProfile, during the same in-game time (60 seconds of rendered game), and on the same (large) world, while flying on different directions to force hundreds of sectors to load and unload, and with VISIBLE_SECTORS_RADIUS = 16 and DRAW_DISTANCE = 120.0:

what was taking time:
- pyglet.graphics.Batch.add
- World.add_block during initialization
- World.is_exposed during game

Model.initialize:
- took 17 seconds, with 11.5 seconds taken by World.add_block
- now takes 10 seconds, with 4.7 seconds taken by World.add_block

during game:
- pyglet.graphics.Batch.add took and still takes 23 seconds.
- World.is_exposed took 25 seconds
- World.is_exposed now takes 19 seconds
- Displaying 3D, labels, and sleeping takes 80% of the rest of the time

For now, I think that the main part of the game is enough optimized. Of course, there is still some little annoying freezes, but they are mainly due to a bad synchronization between the rendering and the queue. I'll work on that later.

So this is good news, because even when asking hundreds/thousands of chunks updates to the game in one minute, we still have **8.5 % of the time that is spent sleeping**.

When doing nothing, **57 % of the time is spent sleeping**!

So have fun and develop lots of outstanding features, performance will follow :)