Skip to content

Commit

Permalink
Merge pull request #16 from subalterngames/input_optimizations
Browse files Browse the repository at this point in the history
Input optimizations
  • Loading branch information
subalterngames authored Nov 27, 2023
2 parents f3dc1aa + 39e3746 commit 288dbc0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
12 changes: 5 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ default-features = false
features = ["derive"]

[workspace.dependencies.macroquad]
version = "0.4.2"
version = "0.4.4"
default-features = false
features = []
git = "https://github.com/not-fl3/macroquad.git"
rev = "6d9685d"

[workspace.dependencies.oxisynth]
version = "0.0.3"
Expand Down
9 changes: 6 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

## 0.2.0

Cacophony uses a lot of CPU resources even when it's idle. It shouldn't do that! I reduced Cacophony's idle CPU usage by around 20-50% and by 15-50% while playing music; the exact percentage varies depending on the CPU and the OS. This update is the first big CPU optimization, and probably the most significant; I think all other subsequent optimizations will chip away at the problem.
Cacophony uses a lot of CPU resources even when it's idle. It shouldn't do that! I reduced Cacophony's CPU usage by around 50%; the exact percentage varies depending on the CPU and the OS. This update is the first big CPU optimization, and probably the most significant.

This update doesn't have any new features or bug fixes. In the future, I'm going to reserve major releases (0.x.0) for big new features, but I had to rewrite so much of Cacophony's code, and the results are such a big improvement, that I'm making this a major release anyway.
These are the optimizations:

- In `audio`, `Synthesizer` (which no longer exists) ran a very fast infinite loop to send samples to `Player`, and the loop needlessly consumed CPU resources. I replaced this loop with a bunch of `Arc<Mutex<T>>` values that are shared between `Conn` and `Player`. As a result of removing `Synthesizer` I had to reorganize all of the exporter code. There are a lot of small changes that I'm not going to list here because let's be real, no one reads verbose changelogs, but the most noticeable change is that `Exporter` is a field in `Conn` and is no longer shared (there is no `Arc<Mutex<Exporter>>` anywhere in the code). This change affects a *lot* of the codebase, but it's mostly just refactoring with zero functional differences.
- In `input`, `Input` checked for key downs and presses very inefficently. I only had to change two lines of code to make it much faster. This optimizes CPU usage by roughly 10%.

*(Backend notes)* The problem was that the `Synthesizer` struct (which no longer exists) ran a very fast infinite loop to send samples to `Player`, and the loop needlessly consumed CPU resources. I replaced this loop with a bunch of `Arc<Mutex<T>>` values that are shared between `Conn` and `Player`. As a result of removing `Synthesizer` I had to reorganize all of the exporter code. There are a lot of small changes that I'm not going to list here because let's be real, no one reads verbose changelogs, but the most noticeable change is that `Exporter` is a field in `Conn` and is no longer shared (there is no `Arc<Mutex<Exporter>>` anywhere in the code). This change affects a *lot* of the codebase, but it's mostly just refactoring with zero functional differences.
This update doesn't have any new features or bug fixes. In the future, I'm going to reserve major releases (0.x.0) for big new features, but I had to rewrite so much of Cacophony's code, and the results are such a big improvement, that I'm making this a major release anyway.

# 0.1.x

Expand Down
8 changes: 2 additions & 6 deletions input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,9 @@ impl Input {
self.pressed_chars.push(c);
}
// Get all pressed keys.
let pressed: Vec<KeyCode> = KEYS
.iter()
.filter(|&k| is_key_pressed(*k))
.copied()
.collect();
let pressed = get_keys_pressed();
// Get all held keys.
let down: Vec<KeyCode> = KEYS.iter().filter(|&k| is_key_down(*k)).copied().collect();
let down = get_keys_down();

// Update the qwerty key bindings.
self.qwerty_events
Expand Down
8 changes: 7 additions & 1 deletion input/src/qwerty_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{ALPHANUMERIC_INPUT_MODS, MODS};
use macroquad::input::KeyCode;
use serde::Deserialize;
use serde_json::{from_str, Error};
use std::collections::HashSet;

/// A list of qwerty keys plus mods that define a qwerty key binding.
#[derive(Clone)]
Expand Down Expand Up @@ -86,7 +87,12 @@ impl QwertyBinding {
/// - `pressed` The keys that were pressed on this frame.
/// - `down` The keys that were held down on this frame.
/// - `alphanumeric` If true, we're in alphanumeric input mode, which can affect whether we can listen for certain qwerty bindings.
pub(crate) fn update(&mut self, pressed: &[KeyCode], down: &[KeyCode], alphanumeric: bool) {
pub(crate) fn update(
&mut self,
pressed: &HashSet<KeyCode>,
down: &HashSet<KeyCode>,
alphanumeric: bool,
) {
self.pressed = false;
// Mods.
if self.mods.iter().all(|m| down.contains(m))
Expand Down

0 comments on commit 288dbc0

Please sign in to comment.