bevyengine / bevyengine/bevy

prepare_window hangs for long frame hitches when experiencing background CPU load

Open
#8,934 1 comment 0 reactions 0 assignees View on GitHub
A-Rendering A-Tasks C-Bug C-Performance S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Bevy version

10.1

## Relevant system information

```ignore
`AdapterInfo { name: "NVIDIA GeForce GTX 1080 Ti", vendor: 4318, device: 6918, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "516.94", backend: Vulkan }`
`SystemInfo { os: "Windows 10 Pro", kernel: "19045", cpu: "Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz", core_count: "4", memory: "32.0 GiB" }`
```

## What you did

Ran this code with Chrome or some other task in the background, creating periods of higher background CPU usage.
```rust
use bevy::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Hitch".to_string(),
..Default::default()
}),
..Default::default()
}))
.insert_resource(FrameTimer::new())
.add_system(frame_system)
.run();
}

#[derive(Resource)]
struct FrameTimer {
last: std::time::Instant,
}

impl FrameTimer {
fn new() -> Self {
Self {
last: std::time::Instant::now(),
}
}
}

fn frame_system(mut timer: ResMut) {
let now = std::time::Instant::now();
let duration = now - timer.last;
if duration.as_millis() > 30 {
println!("{}", duration.as_millis());
}
timer.last = now;
}
```

## What went wrong

At higher CPU loads, bevy will get stuck in long hitches -- tracy shows this hang occurring in window::prepare_window.

![image](https://github.com/bevyengine/bevy/assets/22198616/86dface0-e28f-4489-b324-277501f1b6a9)

## Additional information

This seems like there's some sort of thread contention issue. There's an element of "well, duh" to this where certainly performance will degrade as CPU usage rises, but this is a pretty drastic hitch rather than a gradual framerate drop. I haven't observed behavior like this in other engines, so I'm wondering if there's some trick here to avoiding this issue and presenting a smoother experience without such a frame time contrast?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.