From 221dd16edf2fc701c8175552ff787dadafacdf0d Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Tue, 26 Sep 2023 12:05:05 +0800 Subject: [PATCH] dev(compiler): prefer `Arc` --- cli/src/query_repl.rs | 3 ++- compiler/src/service/export.rs | 20 ++++++++------------ compiler/src/service/mod.rs | 12 ++++++------ packages/compiler/src/lib.rs | 6 +++--- tests/incremental/src/main.rs | 3 +-- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/cli/src/query_repl.rs b/cli/src/query_repl.rs index 3abf88c7..20021280 100644 --- a/cli/src/query_repl.rs +++ b/cli/src/query_repl.rs @@ -10,6 +10,7 @@ use rustyline::hint::{Hinter, HistoryHinter}; use rustyline::validate::MatchingBracketValidator; use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyEvent}; use rustyline::{Helper, Validator}; +use typst_ts_core::TakeAs; use crate::query::serialize; use crate::CompileOnceArgs; @@ -123,7 +124,7 @@ impl Completer for ReplContext { driver.world.reset(); let typst_completions = driver .with_shadow_file_by_id(main_id, dyn_content.as_bytes().into(), |driver| { - let frames = driver.compile().map(|d| d.pages); + let frames = driver.compile().map(|d| d.take().pages); let frames = frames.as_ref().map(|v| v.as_slice()).unwrap_or_default(); let source = driver.world.main(); Ok(autocomplete(&driver.world, frames, &source, cursor, true)) diff --git a/compiler/src/service/export.rs b/compiler/src/service/export.rs index 42cc08d5..f8d9ebdf 100644 --- a/compiler/src/service/export.rs +++ b/compiler/src/service/export.rs @@ -2,9 +2,7 @@ use std::{path::PathBuf, sync::Arc}; use crate::ShadowApi; use typst::{diag::SourceResult, syntax::VirtualPath}; -use typst_ts_core::{ - exporter_builtins::GroupExporter, DynExporter, TakeAs, TypstDocument, TypstFileId, -}; +use typst_ts_core::{exporter_builtins::GroupExporter, DynExporter, TypstDocument, TypstFileId}; use super::{Compiler, WrappedCompiler}; @@ -55,13 +53,11 @@ impl WrappedCompiler for CompileExporter { &mut self.compiler } - fn wrap_compile(&mut self) -> SourceResult { - let doc = Arc::new(self.inner_mut().compile()?); + fn wrap_compile(&mut self) -> SourceResult> { + let doc = self.inner_mut().compile()?; self.export(doc.clone())?; - // Note: when doc is not retained by the exporters, no clone happens, - // because of the `Arc` type detecting a single owner at runtime. - Ok(doc.take()) + Ok(doc) } } @@ -168,7 +164,7 @@ impl WorldExporter for DynamicLayoutCompiler { self.with_shadow_file_by_id(variable_file, variables.as_bytes().into(), |this| { // compile and export document - let output = Arc::new(this.inner_mut().compile()?); + let output = this.inner_mut().compile()?; svg_exporter.render(current_width, output); log::trace!( "rerendered {} at {:?}, {}", @@ -205,14 +201,14 @@ impl WrappedCompiler for DynamicLayoutCompiler { &mut self.compiler } - fn wrap_compile(&mut self) -> SourceResult { + fn wrap_compile(&mut self) -> SourceResult> { if !self.enable_dynamic_layout { return self.inner_mut().compile(); } - let pure_doc = Arc::new(self.inner_mut().compile()?); + let pure_doc = self.inner_mut().compile()?; self.export(pure_doc.clone())?; - Ok(pure_doc.take()) + Ok(pure_doc) } } diff --git a/compiler/src/service/mod.rs b/compiler/src/service/mod.rs index cf668fd3..fd0bdf71 100644 --- a/compiler/src/service/mod.rs +++ b/compiler/src/service/mod.rs @@ -62,12 +62,12 @@ pub trait Compiler { fn reset(&mut self) -> SourceResult<()>; /// Compile once from scratch. - fn pure_compile(&mut self) -> SourceResult { + fn pure_compile(&mut self) -> SourceResult> { self.reset()?; let mut tracer = Tracer::default(); // compile and export document - typst::compile(self.world(), &mut tracer) + typst::compile(self.world(), &mut tracer).map(Arc::new) } /// With **the compilation state**, query the matches for the selector. @@ -76,7 +76,7 @@ pub trait Compiler { } /// Compile once from scratch. - fn compile(&mut self) -> SourceResult { + fn compile(&mut self) -> SourceResult> { self.pure_compile() } @@ -177,7 +177,7 @@ pub trait WrappedCompiler { } /// Hooked compile once from scratch. - fn wrap_compile(&mut self) -> SourceResult { + fn wrap_compile(&mut self) -> SourceResult> { self.inner_mut().compile() } @@ -215,7 +215,7 @@ impl Compiler for T { } #[inline] - fn pure_compile(&mut self) -> SourceResult { + fn pure_compile(&mut self) -> SourceResult> { self.inner_mut().pure_compile() } @@ -225,7 +225,7 @@ impl Compiler for T { } #[inline] - fn compile(&mut self) -> SourceResult { + fn compile(&mut self) -> SourceResult> { self.wrap_compile() } diff --git a/packages/compiler/src/lib.rs b/packages/compiler/src/lib.rs index 466f6289..00bee64f 100644 --- a/packages/compiler/src/lib.rs +++ b/packages/compiler/src/lib.rs @@ -1,4 +1,4 @@ -use std::{path::Path, sync::Arc}; +use std::path::Path; use base64::Engine; use js_sys::{JsString, Uint8Array}; @@ -181,7 +181,7 @@ impl TypstCompiler { // compile and export document let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; let data = ast_exporter - .export(self.compiler.world(), Arc::new(doc)) + .export(self.compiler.world(), doc) .map_err(|e| format!("{e:?}"))?; let converted = ansi_to_html::convert_escaped( @@ -204,7 +204,7 @@ impl TypstCompiler { let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?; let artifact_bytes = ir_exporter - .export(self.compiler.world(), Arc::new(doc)) + .export(self.compiler.world(), doc) .map_err(|e| format!("{e:?}"))?; Ok(artifact_bytes) } diff --git a/tests/incremental/src/main.rs b/tests/incremental/src/main.rs index 9885596d..85536f7d 100644 --- a/tests/incremental/src/main.rs +++ b/tests/incremental/src/main.rs @@ -1,4 +1,4 @@ -use std::{path::Path, sync::Arc}; +use std::path::Path; use typst::doc::Document; use typst_ts_compiler::{ @@ -98,7 +98,6 @@ pub fn test_compiler( }) .unwrap(); - let doc = Arc::new(doc); let delta = incr_server.pack_delta(doc); let delta = BytesModuleStream::from_slice(&delta).checkout_owned(); incr_client.merge_delta(delta);