Skip to content

Commit

Permalink
Use scheduler blocking threadpool instead of tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Feb 16, 2024
1 parent 3e80e79 commit ede4682
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ urlencoding = "2.1"

### RUNTIME

blocking = "1.5"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio = { version = "1.24", features = ["full", "tracing"] }
Expand Down
19 changes: 9 additions & 10 deletions src/lune/builtins/roblox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use mlua::prelude::*;
use mlua_luau_scheduler::LuaSpawnExt;
use once_cell::sync::OnceCell;

use crate::{
Expand All @@ -11,8 +12,6 @@ use crate::{
},
};

use tokio::task;

static REFLECTION_DATABASE: OnceCell<ReflectionDatabase> = OnceCell::new();

pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
Expand Down Expand Up @@ -41,41 +40,41 @@ async fn deserialize_place<'lua>(
contents: LuaString<'lua>,
) -> LuaResult<LuaValue<'lua>> {
let bytes = contents.as_bytes().to_vec();
let fut = task::spawn_blocking(move || {
let fut = lua.spawn_blocking(move || {
let doc = Document::from_bytes(bytes, DocumentKind::Place)?;
let data_model = doc.into_data_model_instance()?;
Ok::<_, DocumentError>(data_model)
});
fut.await.into_lua_err()??.into_lua(lua)
fut.await.into_lua_err()?.into_lua(lua)
}

async fn deserialize_model<'lua>(
lua: &'lua Lua,
contents: LuaString<'lua>,
) -> LuaResult<LuaValue<'lua>> {
let bytes = contents.as_bytes().to_vec();
let fut = task::spawn_blocking(move || {
let fut = lua.spawn_blocking(move || {
let doc = Document::from_bytes(bytes, DocumentKind::Model)?;
let instance_array = doc.into_instance_array()?;
Ok::<_, DocumentError>(instance_array)
});
fut.await.into_lua_err()??.into_lua(lua)
fut.await.into_lua_err()?.into_lua(lua)
}

async fn serialize_place<'lua>(
lua: &'lua Lua,
(data_model, as_xml): (LuaUserDataRef<'lua, Instance>, Option<bool>),
) -> LuaResult<LuaString<'lua>> {
let data_model = (*data_model).clone();
let fut = task::spawn_blocking(move || {
let fut = lua.spawn_blocking(move || {
let doc = Document::from_data_model_instance(data_model)?;
let bytes = doc.to_bytes_with_format(match as_xml {
Some(true) => DocumentFormat::Xml,
_ => DocumentFormat::Binary,
})?;
Ok::<_, DocumentError>(bytes)
});
let bytes = fut.await.into_lua_err()??;
let bytes = fut.await.into_lua_err()?;
lua.create_string(bytes)
}

Expand All @@ -84,15 +83,15 @@ async fn serialize_model<'lua>(
(instances, as_xml): (Vec<LuaUserDataRef<'lua, Instance>>, Option<bool>),
) -> LuaResult<LuaString<'lua>> {
let instances = instances.iter().map(|i| (*i).clone()).collect();
let fut = task::spawn_blocking(move || {
let fut = lua.spawn_blocking(move || {
let doc = Document::from_instance_array(instances)?;
let bytes = doc.to_bytes_with_format(match as_xml {
Some(true) => DocumentFormat::Xml,
_ => DocumentFormat::Binary,
})?;
Ok::<_, DocumentError>(bytes)
});
let bytes = fut.await.into_lua_err()??;
let bytes = fut.await.into_lua_err()?;
lua.create_string(bytes)
}

Expand Down
15 changes: 5 additions & 10 deletions src/lune/builtins/serde/compress_decompress.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use lz4_flex::{compress_prepend_size, decompress_size_prepended};
use mlua::prelude::*;
use tokio::{
io::{copy, BufReader},
task,
};

use lz4_flex::{compress_prepend_size, decompress_size_prepended};
use tokio::io::{copy, BufReader};

use async_compression::{
tokio::bufread::{
Expand Down Expand Up @@ -100,9 +98,7 @@ pub async fn compress<'lua>(
) -> LuaResult<Vec<u8>> {
if let CompressDecompressFormat::LZ4 = format {
let source = source.as_ref().to_vec();
return task::spawn_blocking(move || compress_prepend_size(&source))
.await
.into_lua_err();
return Ok(blocking::unblock(move || compress_prepend_size(&source)).await);
}

let mut bytes = Vec::new();
Expand Down Expand Up @@ -133,9 +129,8 @@ pub async fn decompress<'lua>(
) -> LuaResult<Vec<u8>> {
if let CompressDecompressFormat::LZ4 = format {
let source = source.as_ref().to_vec();
return task::spawn_blocking(move || decompress_size_prepended(&source))
return blocking::unblock(move || decompress_size_prepended(&source))
.await
.into_lua_err()?
.into_lua_err();
}

Expand Down
12 changes: 5 additions & 7 deletions src/lune/builtins/stdio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use mlua::prelude::*;

use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
use tokio::{
io::{self, AsyncWriteExt},
task,
};
use mlua_luau_scheduler::LuaSpawnExt;
use tokio::io::{self, AsyncWriteExt};

use crate::lune::util::{
formatting::{
Expand Down Expand Up @@ -55,10 +53,10 @@ async fn stdio_ewrite(_: &Lua, s: LuaString<'_>) -> LuaResult<()> {
Ok(())
}

async fn stdio_prompt(_: &Lua, options: PromptOptions) -> LuaResult<PromptResult> {
task::spawn_blocking(move || prompt(options))
async fn stdio_prompt(lua: &Lua, options: PromptOptions) -> LuaResult<PromptResult> {
lua.spawn_blocking(move || prompt(options))
.await
.into_lua_err()?
.into_lua_err()
}

fn prompt(options: PromptOptions) -> LuaResult<PromptResult> {
Expand Down

0 comments on commit ede4682

Please sign in to comment.