cvm-rs: version 0.2.0

- switch to napi-rs. this mostly affects only the backend side of things, but IMO napi-rs is better (also, way less boilerplate is needed compared to neon).

- jpeg encoding no longer clones the input buffer internally (or wraps it in a Mutex as well), thanks to napi-rs not sucking in this regard. this is *probably* a micro-optimization, but will make it easier to later on do parallel encoding of all rectangles

- guac encoding is weird. This is kind of a painpoint of napi-rs but it's bearable
This commit is contained in:
modeco80
2024-08-20 06:14:08 -04:00
parent 55566fbd3a
commit 17191b0917
6 changed files with 257 additions and 158 deletions

View File

@@ -1,47 +1,20 @@
use crate::guac;
use neon::prelude::*;
fn guac_decode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsArray> {
let input = cx.argument::<JsString>(0)?.value(cx);
use napi_derive::napi;
#[napi(js_name = "guacDecode")]
#[allow(unused)]
pub fn guac_decode(input: String) -> napi::anyhow::Result<Vec<String>> {
match guac::decode_instruction(&input) {
Ok(data) => {
let array = JsArray::new(cx, data.len());
Ok(elements) => Ok(elements),
let conv = data
.iter()
.map(|v| cx.string(v))
.collect::<Vec<Handle<JsString>>>();
for (i, str) in conv.iter().enumerate() {
array.set(cx, i as u32, *str)?;
}
return Ok(array);
}
Err(e) => {
return cx.throw_error(format!("{}", e));
}
Err(err) => Err(anyhow::anyhow!("Error decoding Guacamole frame: {}", err)),
}
}
fn guac_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsString> {
let mut elements: Vec<String> = Vec::with_capacity(cx.len());
// Capture varadic arguments
for i in 0..cx.len() {
let input = cx.argument::<JsString>(i)?.value(cx);
elements.push(input);
}
Ok(cx.string(guac::encode_instruction(&elements)))
}
pub fn guac_decode(mut cx: FunctionContext) -> JsResult<JsArray> {
guac_decode_impl(&mut cx)
}
pub fn guac_encode(mut cx: FunctionContext) -> JsResult<JsString> {
guac_encode_impl(&mut cx)
// ... this is ugly, but works
#[napi(js_name = "guacEncodeImpl")]
#[allow(unused)]
pub fn guac_encode(items: Vec<String>) -> napi::anyhow::Result<String> {
Ok(guac::encode_instruction(&items))
}

View File

@@ -1,7 +1,5 @@
use std::sync::{Arc, Mutex};
use neon::prelude::*;
use neon::types::buffer::TypedArray;
use napi::Env;
use napi_derive::napi;
use once_cell::sync::OnceCell;
@@ -12,23 +10,22 @@ use rayon::{ThreadPool, ThreadPoolBuilder};
use crate::jpeg_compressor::*;
/// Gives a Rayon thread pool we use for parallelism
fn rayon_pool<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static ThreadPool> {
fn rayon_pool() -> &'static ThreadPool {
static RUNTIME: OnceCell<ThreadPool> = OnceCell::new();
RUNTIME
.get_or_try_init(|| {
// spawn at least 4 threads
let mut nr_threads = std::thread::available_parallelism().expect("??").get() / 8;
if nr_threads == 0 {
nr_threads = 4;
}
RUNTIME.get_or_init(|| {
// spawn at least 4 threads
let mut nr_threads = std::thread::available_parallelism().expect("??").get() / 8;
if nr_threads == 0 {
nr_threads = 4;
}
ThreadPoolBuilder::new()
.num_threads(nr_threads)
.thread_name(|index| format!("cvmrs_jpeg_{}", index + 1))
.build()
})
.or_else(|err| cx.throw_error(&err.to_string()))
ThreadPoolBuilder::new()
.num_threads(nr_threads)
.thread_name(|index| format!("cvmrs_jpeg_{}", index + 1))
.build()
.unwrap()
})
}
thread_local! {
@@ -38,43 +35,29 @@ thread_local! {
// TODO: We should probably allow passing an array of images to encode, which would
// increase parallelism heavily.
fn jpeg_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsPromise> {
let input = cx.argument::<JsObject>(0)?;
#[napi(object)]
pub struct JpegInputArgs {
pub width: u32,
pub height: u32,
pub stride: u32,
pub buffer: napi::JsBuffer,
}
// Get our input arguments here
let width: u64 = input.get::<JsNumber, _, _>(cx, "width")?.value(cx) as u64;
let height: u64 = input.get::<JsNumber, _, _>(cx, "height")?.value(cx) as u64;
let stride: u64 = input.get::<JsNumber, _, _>(cx, "stride")?.value(cx) as u64;
let buffer: Handle<JsBuffer> = input.get(cx, "buffer")?;
let (deferred, promise) = cx.promise();
let channel = cx.channel();
let pool = rayon_pool(cx)?;
let buf = buffer.as_slice(cx);
let copy: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(Vec::with_capacity(buf.len())));
// Copy from the node buffer to our temporary buffer
{
let mut locked = copy.lock().unwrap();
let cap = locked.capacity();
locked.resize(cap, 0);
locked.copy_from_slice(buf);
}
#[napi(js_name = "jpegEncode")]
#[allow(unused)]
pub fn jpeg_encode(env: Env, input: JpegInputArgs) -> napi::Result<napi::JsObject> {
let (deferred_resolver, promise) = env.create_deferred::<napi::JsUnknown, _>()?;
let buf = input.buffer.into_ref()?;
// Spawn a task on the rayon pool that encodes the JPEG and fufills the promise
// once it is done encoding.
pool.spawn_fifo(move || {
let clone = Arc::clone(&copy);
let locked = clone.lock().unwrap();
rayon_pool().spawn_fifo(move || {
let image: Image = Image {
buffer: locked.as_slice(),
width: width as u32,
height: height as u32,
buffer: &buf,
width: input.width as u32,
height: input.height as u32,
stride: (stride * 4u64) as u32, // I think?
stride: (input.stride as u64 * 4u64) as u32,
format: turbojpeg_sys::TJPF_TJPF_RGBA,
};
@@ -85,18 +68,13 @@ fn jpeg_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsPromise>
b.compress_buffer(&image)
});
// Fulfill the Javascript promise with our encoded buffer
deferred.settle_with(&channel, move |mut cx| {
let mut buf = cx.buffer(vec.len())?;
let slice = buf.as_mut_slice(&mut cx);
slice.copy_from_slice(vec.as_slice());
Ok(buf)
deferred_resolver.resolve(move |env| {
let buffer = env
.create_buffer_with_data(vec)
.expect("Couldn't create node Buffer, things are probably very broken by this point");
Ok(buffer.into_unknown())
});
});
Ok(promise)
}
pub fn jpeg_encode(mut cx: FunctionContext) -> JsResult<JsPromise> {
jpeg_encode_impl(&mut cx)
}

View File

@@ -3,15 +3,3 @@ mod guac_js;
mod jpeg_compressor;
mod jpeg_js;
use neon::prelude::*;
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
// Mostly transitionary, later on API should change
cx.export_function("jpegEncode", jpeg_js::jpeg_encode)?;
cx.export_function("guacDecode", guac_js::guac_decode)?;
cx.export_function("guacEncode", guac_js::guac_encode)?;
Ok(())
}