Skip to content

Commit 09366cf

Browse files
committed
perf: optimize render pipeline with rayon parallelism and mimalloc
1 parent b6b46a2 commit 09366cf

File tree

8 files changed

+230
-176
lines changed

8 files changed

+230
-176
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
rustflags = ["-C", "target-cpu=native"]

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ serde-wasm-bindgen = "0.6"
2727
[[bin]]
2828
name = "maple"
2929
path = "src/main.rs"
30+
31+
[profile.release]
32+
lto = "fat"
33+
codegen-units = 1
34+
panic = "abort"
35+
36+
[dependencies.mimalloc]
37+
version = "0.1"
38+
default-features = false
39+
40+
[dependencies.rayon]
41+
version = "1.10"

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use std::{
33
path::{Path, PathBuf},
44
};
55

6+
#[global_allocator]
7+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
8+
69
use clap::{ArgGroup, Parser};
710
use color_eyre::eyre::{Result, WrapErr, eyre};
811
use image::Rgba;

src/pixer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,29 @@ pub struct Pixer {
99
}
1010

1111
impl Pixer {
12+
#[inline(always)]
1213
pub fn new() -> Self {
1314
Pixer::default()
1415
}
1516

17+
#[inline(always)]
1618
pub fn from_rgba(pixel: &Rgba<u8>) -> Self {
1719
Pixer { r: pixel[0] as f64, g: pixel[1] as f64, b: pixel[2] as f64, a: pixel[3] as f64 }
1820
}
1921

22+
#[inline(always)]
2023
pub fn to_rgba(&self) -> Rgba<u8> {
2124
Rgba([clamp_u8(self.r), clamp_u8(self.g), clamp_u8(self.b), clamp_u8(self.a)])
2225
}
2326

27+
#[inline(always)]
2428
pub fn preblend(&mut self) {
2529
self.r *= self.a;
2630
self.g *= self.a;
2731
self.b *= self.a;
2832
}
2933

34+
#[inline(always)]
3035
pub fn postblend(&mut self, scale: f64) {
3136
if scale > 0.0001 {
3237
self.r /= scale;
@@ -118,7 +123,7 @@ fn clamp_u8(v: f64) -> u8 {
118123
}
119124
}
120125

121-
#[inline]
126+
#[inline(always)]
122127
pub fn safe_pixel(img: &RgbaImage, x: i32, y: i32) -> Rgba<u8> {
123128
let w = img.width() as i32;
124129
let h = img.height() as i32;
@@ -131,6 +136,7 @@ pub fn safe_pixel(img: &RgbaImage, x: i32, y: i32) -> Rgba<u8> {
131136
}
132137

133138
/// Bilinear interpolation with alpha-weighted averaging
139+
#[inline(always)]
134140
pub fn sample_linear(img: &RgbaImage, x: f64, y: f64) -> Pixer {
135141
let xx = x.floor() as i32;
136142
let yy = y.floor() as i32;
@@ -179,7 +185,7 @@ pub fn sample_weakly(img: &RgbaImage, x: f64, y: f64) -> Pixer {
179185
Pixer::from_rgba(&safe_pixel(img, x as i32, y as i32))
180186
}
181187

182-
#[inline]
188+
#[inline(always)]
183189
pub fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
184190
((x1 - x2).powi(2) + (y1 - y2).powi(2)).sqrt()
185191
}

0 commit comments

Comments
 (0)