Skip to content

Commit

Permalink
dev(compiler): prefer Arc<Document>
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Sep 26, 2023
1 parent f4a3dab commit 221dd16
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cli/src/query_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
20 changes: 8 additions & 12 deletions compiler/src/service/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -55,13 +53,11 @@ impl<C: Compiler> WrappedCompiler for CompileExporter<C> {
&mut self.compiler
}

fn wrap_compile(&mut self) -> SourceResult<typst::doc::Document> {
let doc = Arc::new(self.inner_mut().compile()?);
fn wrap_compile(&mut self) -> SourceResult<Arc<typst::doc::Document>> {
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)
}
}

Expand Down Expand Up @@ -168,7 +164,7 @@ impl<C: Compiler + ShadowApi> WorldExporter for DynamicLayoutCompiler<C> {

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 {:?}, {}",
Expand Down Expand Up @@ -205,14 +201,14 @@ impl<C: Compiler + ShadowApi> WrappedCompiler for DynamicLayoutCompiler<C> {
&mut self.compiler
}

fn wrap_compile(&mut self) -> SourceResult<typst::doc::Document> {
fn wrap_compile(&mut self) -> SourceResult<Arc<TypstDocument>> {
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)
}
}
12 changes: 6 additions & 6 deletions compiler/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ pub trait Compiler {
fn reset(&mut self) -> SourceResult<()>;

/// Compile once from scratch.
fn pure_compile(&mut self) -> SourceResult<Document> {
fn pure_compile(&mut self) -> SourceResult<Arc<Document>> {
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.
Expand All @@ -76,7 +76,7 @@ pub trait Compiler {
}

/// Compile once from scratch.
fn compile(&mut self) -> SourceResult<Document> {
fn compile(&mut self) -> SourceResult<Arc<Document>> {
self.pure_compile()
}

Expand Down Expand Up @@ -177,7 +177,7 @@ pub trait WrappedCompiler {
}

/// Hooked compile once from scratch.
fn wrap_compile(&mut self) -> SourceResult<Document> {
fn wrap_compile(&mut self) -> SourceResult<Arc<Document>> {
self.inner_mut().compile()
}

Expand Down Expand Up @@ -215,7 +215,7 @@ impl<T: WrappedCompiler> Compiler for T {
}

#[inline]
fn pure_compile(&mut self) -> SourceResult<Document> {
fn pure_compile(&mut self) -> SourceResult<Arc<Document>> {
self.inner_mut().pure_compile()
}

Expand All @@ -225,7 +225,7 @@ impl<T: WrappedCompiler> Compiler for T {
}

#[inline]
fn compile(&mut self) -> SourceResult<Document> {
fn compile(&mut self) -> SourceResult<Arc<Document>> {
self.wrap_compile()
}

Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::Path, sync::Arc};
use std::path::Path;

use base64::Engine;
use js_sys::{JsString, Uint8Array};
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions tests/incremental/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::Path, sync::Arc};
use std::path::Path;

use typst::doc::Document;
use typst_ts_compiler::{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 221dd16

Please sign in to comment.