Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deactivate the CameraServer by default. #104232

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

Ivorforce
Copy link
Member

@Ivorforce Ivorforce commented Mar 16, 2025

I profiled the launch of tps demo. I noticed that launching the CameraServer took 12% (120ms) of the total CPU time (on macOS). I found this curious because tps-demo should not access my camera.

I propose deactivating the CameraServer by default. I think this is appropriate for most games: Most do not need to monitor cameras, and monitoring the feeds as a game that does not use cameras is questionable at best.

The CameraServer can be activated in games that need it with set_is_monitoring_feeds, and queried with is_monitoring_feeds.
Most games will not activate monitoring and thus won't pay for it. For games that do need the camera, I think it is still appropriate to be able to activate and deactive feed monitoring, because they may not need to monitor the feeds all the time.

Notes

This PR should be tested on Linux before merging. It should also be tested if the changes break camera monitoring, because I have no project to conveniently test it with.

It's also likely this PR (in its current state) will cause problems with the Godot editor itself, which may want to monitor feeds for editor purposes. I'm open to suggestions for where / how to support that.

This PR is a breaking change to games that use the camera feed. When they update, they'll need to start activating the server somewhere in their code.

Profiling

I profiled tps demo and found it to launch 8% (0.14s) faster on average on macOS:

Benchmark 1: bin/godot.macos.editor.arm64.master --path /Users/lukas/dev/godot/tps-demo --quit
  Time (mean ± σ):      1.772 s ±  0.034 s    [User: 0.597 s, System: 0.224 s]
  Range (min … max):    1.710 s …  1.859 s    25 runs

Benchmark 2: bin/godot.macos.editor.arm64 --path /Users/lukas/dev/godot/tps-demo --quit
  Time (mean ± σ):      1.635 s ±  0.041 s    [User: 0.558 s, System: 0.168 s]
  Range (min … max):    1.537 s …  1.715 s    25 runs

Summary
  bin/godot.macos.editor.arm64 --path /Users/lukas/dev/godot/tps-demo --quit ran
    1.08 ± 0.03 times faster than bin/godot.macos.editor.arm64.master --path /Users/lukas/dev/godot/tps-demo --quit

Linux uses an asynchronous approach to monitoring feeds, so it should benefit asynchronously instead of on launch time.
Windows does not benefit because CameraServer is not implemented for it.

Addendum

It may be appropriate to move the CameraServer to a GDExtension because it is very self-contained, and it is questionable that code to monitor cameras even ships with all Godot games in the first place.
However, this is out of scope for this PR, and likely won't provide much more tangible benefit than what is suggested here (as the camera module is very small).

@Ivorforce Ivorforce requested a review from a team as a code owner March 16, 2025 12:10
@Ivorforce Ivorforce force-pushed the camera-server-shutdown branch 3 times, most recently from 8d55644 to 5d94cdc Compare March 16, 2025 13:12
@Ivorforce Ivorforce requested a review from a team as a code owner March 16, 2025 13:12
@Repiteo Repiteo added this to the 4.x milestone Mar 16, 2025
@BastiaanOlij
Copy link
Contributor

Just to add my two cents here, agree this is a good approach. I think having this as a properly exposed property is fine. Whether we set a property or call a setter, the effect remains the same, the delay on MacOS because we're (currently) not implementing this asynchronously or in a separate thread shouldn't be a factor here.

Off course it would be great to do a follow up PR at some point to make the detection asynchronous or use a thread so we don't stall the app. It's been too long since I worked on this to know how feasible this change is but its worth investigating.

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally (rebased on top of master 97241ff), it works as expected.

Benchmark

PC specifications
  • CPU: Intel Core i9-13900K
  • GPU: NVIDIA GeForce RTX 4090
  • RAM: 64 GB (2×32 GB DDR5-5800 C30)
  • SSD: Solidigm P44 Pro 2 TB
  • OS: Linux (Fedora 41)

Time taken for an empty project to start and quit:

❯ hyperfine -iw1 -m50 "bin/godot.linuxbsd.template_release.x86_64.master --path /tmp/4 --quit" "bin/godot.linuxbsd.template_release.x86_64 --path /tmp/4 --quit"
Benchmark 1: bin/godot.linuxbsd.template_release.x86_64.master --path /tmp/4 --quit
  Time (mean ± σ):      2.135 s ±  0.493 s    [User: 0.210 s, System: 0.356 s]
  Range (min … max):    1.581 s …  2.648 s    50 runs
 
Benchmark 2: bin/godot.linuxbsd.template_release.x86_64 --path /tmp/4 --quit   <--- This PR
  Time (mean ± σ):      1.654 s ±  0.150 s    [User: 0.212 s, System: 0.351 s]
  Range (min … max):    0.725 s …  1.770 s    50 runs

@Ivorforce Ivorforce force-pushed the camera-server-shutdown branch 2 times, most recently from 5873cc7 to a2c5266 Compare March 20, 2025 14:14
@akien-mga akien-mga modified the milestones: 4.x, 4.5 Mar 20, 2025
Copy link
Contributor

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a very painful thing to discover for some developers that upgrade to 4.5 so let's remember to make note of it

Add `monitoring_feeds` property to `CameraServer`.
This saves resources for games that don't use a physical camera.
@Ivorforce Ivorforce force-pushed the camera-server-shutdown branch from a2c5266 to 54685c3 Compare March 21, 2025 06:59
@akien-mga akien-mga merged commit f51ea67 into godotengine:master Mar 21, 2025
20 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants