Skip to content

Commit

Permalink
WPT: Use thread_local crate for thread state
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 6, 2024
1 parent e82361c commit 2452967
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/wpt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ tower = {version = "0.5.1"}
url = "2.5.3"
reqwest = "0.12.9"
rayon = "1.10.0"
thread_local = "1.1.8"
47 changes: 34 additions & 13 deletions apps/wpt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blitz_renderer_vello::VelloImageRenderer;
use blitz_traits::net::SharedProvider;
use parley::FontContext;
use reqwest::Url;
use thread_local::ThreadLocal;
use tower_http::services::ServeDir;

use regex::Regex;
Expand All @@ -18,6 +19,7 @@ use axum::Router;
use image::{ImageBuffer, ImageFormat};
use log::{error, info};
use owo_colors::OwoColorize;
use std::cell::RefCell;
use std::fs::File;
use std::io::Write;
use std::net::SocketAddr;
Expand Down Expand Up @@ -106,6 +108,13 @@ fn clone_font_ctx(ctx: &FontContext) -> FontContext {
}
}

struct ThreadState {
renderer: VelloImageRenderer,
font_ctx: FontContext,
test_buffer: Vec<u8>,
ref_buffer: Vec<u8>,
}

fn main() {
env_logger::init();
let rt = tokio::runtime::Builder::new_current_thread()
Expand Down Expand Up @@ -151,16 +160,28 @@ fn main() {

let base_font_context = parley::FontContext::default();

let thread_state: ThreadLocal<RefCell<ThreadState>> = ThreadLocal::new();

test_paths.into_par_iter().for_each_init(
|| {
let renderer = rt.block_on(VelloImageRenderer::new(WIDTH, HEIGHT, SCALE));
let font_ctx = clone_font_ctx(&base_font_context);
let test_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
let ref_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
let guard = rt.enter();
(renderer, font_ctx, test_buffer, ref_buffer, guard)
},
|state, path| {
|| rt.enter(),
|_guard, path| {
let mut state = thread_state
.get_or(|| {
let renderer = rt.block_on(VelloImageRenderer::new(WIDTH, HEIGHT, SCALE));
let font_ctx = clone_font_ctx(&base_font_context);
let test_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);
let ref_buffer = Vec::with_capacity((WIDTH * HEIGHT * 4) as usize);

RefCell::new(ThreadState {
renderer,
font_ctx,
test_buffer,
ref_buffer,
})
})
.borrow_mut();
let state = &mut *state;

let num = num.fetch_add(1, Ordering::SeqCst) + 1;

let relative_path = path
Expand All @@ -172,10 +193,10 @@ fn main() {

let url = base_url.join(relative_path.as_str()).unwrap();

let renderer = &mut state.0;
let font_ctx = &state.1;
let test_buffer = &mut state.2;
let ref_buffer = &mut state.3;
let renderer = &mut state.renderer;
let font_ctx = &state.font_ctx;
let test_buffer = &mut state.test_buffer;
let ref_buffer = &mut state.ref_buffer;

let result = catch_unwind(AssertUnwindSafe(|| {
let mut blitz_context = setup_blitz();
Expand Down

0 comments on commit 2452967

Please sign in to comment.