Increase world size and profile slowdown
Description
We can't increase the size of our world too much before we run into performance problems. At the existing 20x20 size, we're only rendering 400 blocks each frame. If we bump this to 100x100, we're up to 10,000 blocks per frame.
On my machine, I start to notice the FPS dropping at around 200x200. Cranking it even further to 400x400 brings my FPS down to around 10 frames per second.
Before we try out various optimizations, I was curious what it would look like if we profiled the application. Flamegraphs are a visualization of where the program is spending its time.
The cargo-flamegraph command can be used to generate flamegraphs for Cargo-based projects. Depending on your OS, you'll have to install profiling tools; see the installation guide for details.
Once the command is installed, running cargo flamegraph
will run the
application in release mode and start capturing performance data. Once
the application is closed, the command will aggregate the data into a
flamegraph.svg file.
I've included the flamegraph from my machine running Linux with X11, so if you generate one it could look very different. One thing that wasn't obvious at first is that the X-axis is not ordered by time.
Near the base the graph splits into two large chunks: iridium
and
iridium:gdrv0
. The latter is a separate thread created by a
lower-level graphics library (this Unix SE comment helped me track this
down). Most of the calls on this thread are into the Radeon graphics
driver which I don't really know how to investigate.
In subsequent commits we'll investigate the other side of the graph to see where we should optimize.
Screenshot
Commands
git clone git@github.com:atsheehan/iridium
cd iridium
git checkout 8df142194489d716b78f08fb073dbce37a9bf4b3
cargo run --release
Code Changes
Modified Cargo.tomlGitHub
@@ -15,3 +15,6 @@1515 glutin-winit = "0.4.2"
1616 raw-window-handle = "0.5.2"
1717 gl = "0.14.0"
18+
19+ [profile.release]
20+ debug = true
@@ -15,3 +15,6 @@15 glutin-winit = "0.4.2"
16 raw-window-handle = "0.5.2"
17 gl = "0.14.0"
@@ -15,3 +15,6 @@15 glutin-winit = "0.4.2"
16 raw-window-handle = "0.5.2"
17 gl = "0.14.0"
18+
19+ [profile.release]
20+ debug = true
Added analysis/flamegraph.svgGitHub
Modified src/main.rsGitHub
@@ -24,7 +24,7 @@2424 let event_loop = EventLoop::new().unwrap();
2525 let mut renderer = Renderer::new(&event_loop, options.windowed);
2626
27- let mut world = World::new(20, 20);
27+ let mut world = World::new(400, 400);
2828
2929 let mut last_instant = Instant::now();
3030 let mut fps_counter = FrameCounter::new(last_instant);
@@ -24,7 +24,7 @@24 let event_loop = EventLoop::new().unwrap();
25 let mut renderer = Renderer::new(&event_loop, options.windowed);
26
27- let mut world = World::new(20, 20);
28
29 let mut last_instant = Instant::now();
30 let mut fps_counter = FrameCounter::new(last_instant);
@@ -24,7 +24,7 @@24 let event_loop = EventLoop::new().unwrap();
25 let mut renderer = Renderer::new(&event_loop, options.windowed);
26
27+ let mut world = World::new(400, 400);
28
29 let mut last_instant = Instant::now();
30 let mut fps_counter = FrameCounter::new(last_instant);