From de67e58dd5b17e2755956f4ad2ef102f1cf6ca20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 5 Nov 2024 11:44:11 +0400 Subject: [PATCH] perf(server): make tiles encoding parallel with rayon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This can help a lot wall-clock time, but depends on CPU. rfx_enc time: [9.7885 ms 10.123 ms 10.439 ms] change: [-80.484% -79.847% -79.208%] (p = 0.00 < 0.05) Performance has improved. Signed-off-by: Marc-André Lureau --- Cargo.lock | 1 + crates/ironrdp-server/Cargo.toml | 3 +++ crates/ironrdp-server/src/encoder/rfx.rs | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 76192a3f..c390068d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2484,6 +2484,7 @@ dependencies = [ "ironrdp-rdpsnd", "ironrdp-svc", "ironrdp-tokio", + "rayon", "rustls-pemfile", "tokio", "tokio-rustls", diff --git a/crates/ironrdp-server/Cargo.toml b/crates/ironrdp-server/Cargo.toml index 0750f926..2bc3fb23 100644 --- a/crates/ironrdp-server/Cargo.toml +++ b/crates/ironrdp-server/Cargo.toml @@ -16,7 +16,9 @@ doctest = true test = false [features] +default = ["rayon"] helper = ["dep:x509-cert", "dep:rustls-pemfile"] +rayon = ["dep:rayon"] # Internal (PRIVATE!) features used to aid testing. # Don't rely on these whatsoever. They may disappear at any time. @@ -42,6 +44,7 @@ ironrdp-rdpsnd.workspace = true tracing.workspace = true x509-cert = { version = "0.2.5", optional = true } rustls-pemfile = { version = "2.2.0", optional = true } +rayon = { version = "1.10.0", optional = true } [dev-dependencies] tokio = { version = "1", features = ["sync"] } diff --git a/crates/ironrdp-server/src/encoder/rfx.rs b/crates/ironrdp-server/src/encoder/rfx.rs index 2292b2cf..6324f9e9 100644 --- a/crates/ironrdp-server/src/encoder/rfx.rs +++ b/crates/ironrdp-server/src/encoder/rfx.rs @@ -144,9 +144,16 @@ impl<'a> UpdateEncoder<'a> { } fn encode(&self, data: &'a mut UpdateEncoderData) -> EncodeResult>> { + #[cfg(feature = "rayon")] + use rayon::prelude::*; + let (tiles_x, tiles_y) = self.tiles_xy(); + #[cfg(not(feature = "rayon"))] let chunks = data.0.chunks_mut(64 * 64 * 3); + #[cfg(feature = "rayon")] + let chunks = data.0.par_chunks_mut(64 * 64 * 3); + let tiles: Vec<_> = (0..tiles_y).flat_map(|y| (0..tiles_x).map(move |x| (x, y))).collect(); chunks