bevyengine / bevyengine/bevy

macOS and iOS CPU ussage fix

Open
#5,713 15 comments 1 reaction 0 assignees View on GitHub
A-Windowing C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

increasing iOS and macOS game performance whilst saving battery live is a hard task but can be done quite easily, since macOS and iOS usually fix application framerate (with the exclusion to scrollviews or when programmatically demmanded). Currently a basic bevy app uses ~100% cpu on mac, which is a shame because it can be lower

## What solution would you like?

Since application framerate is capped, using CADisplayLink on iOS and CVDisplayLink on macOS will give you a non-blocking 60 tick per second callback

## What alternative(s) have you considered?

There's no other way on macOS , because the most common run loop [NSApp getevent.....] is really slow and CoreVideo bypasses all of the objc event system

## Additional context

As someone who has implemented both CADisplayLink and CVDisplayLink once in the past. I can assure you it saves alot on cpu usage going from ~>100% to <20% which in turn saves on a lot of battery life. Also because apple ctr-alt-deleted there good old CoreVideo documention here’s a quick overview in obj-c (even though corevideo is mostly a c-api):
```obj-c
//create a display link
CVDisplayLinkRef displayLink;
CGDirectDisplayID displayID = CGMainDisplayID();
CVReturn error = kCVReturnSuccess;
error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLink);
if (error){
NSLog(@"error:%d", error);
displayLink = NULL;
}
//link callback
CVDisplayLinkSetOutputCallback(displayLink, renderCallback, (__bridge void *)self);
//start
CVDisplayLinkStart(displayLink);

//callback
CVReturn renderCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp *inNow,
const CVTimeStamp *inOutputTime,
CVOptionFlags flagsIn,
CVOptionFlags *flagsOut,
void *displayLinkContext){
@autoreleasepool {
update();
//get access to the main thread when drawing to screen (this method is also blocking but way less demanding than the NSApp runloop)
dispatch_sync(dispatch_get_main_queue(), ^{
[gview setNeedsDisplay:YES];
});
}
return 0;
}

```

Contributor guide

Open the contributing guide

Research direction

Start by locating Bevy's macOS and iOS application-loop or frame-scheduling entry points, then review how the current loop drives rendering. Compare the platform behavior with the proposed CADisplayLink and CVDisplayLink callbacks. Done means CPU usage is reduced on both platforms without breaking frame updates, input, or rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, macos, rust
Domain
desktop-dev, mobile-dev, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.