Skip to content

Commit

Permalink
feat: allow configuring snippet rendering threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Jul 7, 2024
1 parent 041a83f commit fd487b9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
21 changes: 21 additions & 0 deletions config-file-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@
"$ref": "#/definitions/SnippetExecConfig"
}
]
},
"render": {
"description": "The properties for snippet auto rendering.",
"allOf": [
{
"$ref": "#/definitions/SnippetRenderConfig"
}
]
}
},
"additionalProperties": false
Expand All @@ -292,6 +300,19 @@
},
"additionalProperties": false
},
"SnippetRenderConfig": {
"type": "object",
"properties": {
"threads": {
"description": "The number of threads to use when rendering.",
"default": 2,
"type": "integer",
"format": "uint",
"minimum": 0.0
}
},
"additionalProperties": false
},
"TypstConfig": {
"type": "object",
"properties": {
Expand Down
22 changes: 22 additions & 0 deletions src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ pub struct SnippetConfig {
/// The properties for snippet execution.
#[serde(default)]
pub exec: SnippetExecConfig,

/// The properties for snippet auto rendering.
#[serde(default)]
pub render: SnippetRenderConfig,
}

#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
Expand All @@ -132,6 +136,24 @@ pub struct SnippetExecConfig {
pub enable: bool,
}

#[derive(Clone, Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SnippetRenderConfig {
/// The number of threads to use when rendering.
#[serde(default = "default_snippet_render_threads")]
pub threads: usize,
}

impl Default for SnippetRenderConfig {
fn default() -> Self {
Self { threads: default_snippet_render_threads() }
}
}

pub(crate) fn default_snippet_render_threads() -> usize {
2
}

#[derive(Clone, Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TypstConfig {
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ fn run(mut cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
let printer = Arc::new(ImagePrinter::new(graphics_mode.clone())?);
let registry = ImageRegistry(printer.clone());
let resources = Resources::new(resources_path, registry.clone());
let third_party_config =
ThirdPartyConfigs { typst_ppi: config.typst.ppi.to_string(), mermaid_scale: config.mermaid.scale.to_string() };
let third_party_config = ThirdPartyConfigs {
typst_ppi: config.typst.ppi.to_string(),
mermaid_scale: config.mermaid.scale.to_string(),
threads: config.snippet.render.threads,
};
let third_party = ThirdPartyRender::new(third_party_config, registry, resources_path);
let code_executor = Rc::new(code_executor);
if cli.export_pdf || cli.generate_pdf_metadata {
Expand Down
9 changes: 5 additions & 4 deletions src/third_party.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
custom::{default_mermaid_scale, default_typst_ppi},
custom::{default_mermaid_scale, default_snippet_render_threads, default_typst_ppi},
markdown::elements::{Text, TextBlock},
media::{image::Image, printer::RegisterImageError},
presentation::{
Expand All @@ -23,13 +23,13 @@ use std::{
};
use tempfile::tempdir_in;

const MAX_WORKERS: usize = 2;
const DEFAULT_HORIZONTAL_MARGIN: u16 = 5;
const DEFAULT_VERTICAL_MARGIN: u16 = 7;

pub struct ThirdPartyConfigs {
pub typst_ppi: String,
pub mermaid_scale: String,
pub threads: usize,
}

pub struct ThirdPartyRender {
Expand Down Expand Up @@ -74,6 +74,7 @@ impl Default for ThirdPartyRender {
let config = ThirdPartyConfigs {
typst_ppi: default_typst_ppi().to_string(),
mermaid_scale: default_mermaid_scale().to_string(),
threads: default_snippet_render_threads(),
};
Self::new(config, Default::default(), Path::new("."))
}
Expand Down Expand Up @@ -113,12 +114,12 @@ struct RenderPool {

impl RenderPool {
fn new(config: ThirdPartyConfigs, root_dir: String, image_registry: ImageRegistry) -> Self {
let threads = config.threads;
let shared = Shared { config, root_dir, signal: Default::default() };
let state = RenderPoolState { requests: Default::default(), image_registry, cache: Default::default() };

let max_workers = MAX_WORKERS;
let this = Self { state: Arc::new(Mutex::new(state)), shared: Arc::new(shared) };
for _ in 0..max_workers {
for _ in 0..threads {
let worker = Worker { state: this.state.clone(), shared: this.shared.clone() };
thread::spawn(move || worker.run());
}
Expand Down

0 comments on commit fd487b9

Please sign in to comment.