Prewarm LaTeX format and cache verified project preparation artifacts

This commit is contained in:
gloamxun committed 2026-12-02 18:01:20 +08:00
1 parent fdd327eb01
commit c1742e1c10
10 files changed
+668 -28

No files matched your search

+40 -9
View File
@@ -334,13 +334,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
root, &source, &format, &manifest, &run, &additional, input_profile,
)?;
let compiler = NativeCompiler::default().with_automatic_entry_checkpoints();
let editor = finish_prepare(staged, &run, &compiler, None)?;
let editor = finish_prepare(staged, &run, &compiler, None, None)?;
(port, Some(EditorSession::new(run, editor, compiler, None)))
};
let mut session = session.map(|session| Arc::new(Mutex::new(session)));
let mut opening: Option<OpeningEditor> = None;
let server = Server::http(("127.0.0.1", port)).map_err(|e| e.to_string())?;
println!("Native editor http://{}", server.server_addr());
if let Some(library) = &library {
library.prewarm_format();
}
for request in server.incoming_requests() {
let url = request.url().to_owned();
let path = url.as_str();
@@ -712,6 +715,7 @@ impl OpeningEditor {
run: PathBuf,
staged: StagedProject,
cache: Option<preview_cache::PreviewCache>,
compiler: NativeCompiler,
) -> Result<Self, Box<dyn std::error::Error>> {
let files = staged.editable.iter().filter_map(|(name, path)| {
staged.files.get(path).map(|bytes| (name.as_str(), bytes.as_ref()))
@@ -726,7 +730,7 @@ impl OpeningEditor {
cache,
sources,
ready: Arc::new(OnceLock::new()),
compiler: NativeCompiler::default().with_automatic_entry_checkpoints(),
compiler,
superseded: Arc::new(AtomicBool::new(false)),
};
let id = opening.id.clone();
@@ -735,7 +739,7 @@ impl OpeningEditor {
let superseded = opening.superseded.clone();
let cache = opening.cache.clone();
std::thread::Builder::new().name("texrra-open-project".into()).spawn(move || {
let result = finish_prepare(staged, &run, &compiler, Some(&superseded))
let result = finish_prepare(staged, &run, &compiler, Some(&superseded), Some(&id))
.map(|editor| {
let mut session = EditorSession::new(run, editor, compiler, cache);
session.id = Some(id);
@@ -1092,6 +1096,7 @@ fn finish_prepare(
run: &Path,
compiler: &NativeCompiler,
superseded: Option<&AtomicBool>,
project_id: Option<&str>,
) -> Result<EditorProject, Box<dyn std::error::Error>> {
let StagedProject {
world,
@@ -1119,9 +1124,10 @@ fn finish_prepare(
let date = dates.get(input).and_then(|time| time.to_system_time());
Some((converted, data.clone(), date))
}).collect::<Vec<_>>();
let figure_preparation = figure_conversion::Preparation::new(run);
for (path, source, date) in figures {
check_current()?;
match figure_conversion::convert(run, &source) {
match figure_preparation.convert(&source) {
Ok(Some(pdf)) => {
let modified = date.unwrap_or_else(SystemTime::now);
copy_with_date(&path, &pdf, Some(modified), &mut dates)?;
@@ -1133,21 +1139,46 @@ fn finish_prepare(
}
// Provision the finite, immutable resource bundle used by this trial.
// The seed uses the same native compiler and actual package bytes.
let compile_seed = || -> Result<_, Box<dyn std::error::Error>> {
let trace_preparation = std::env::var_os("TEXRRA_EDITOR_PREPARATION_TRACE")
.is_some_and(|value| value == "1");
let compile_seed = |pass| -> Result<_, Box<dyn std::error::Error>> {
let job = compiler.start();
check_current()?;
Ok(job.compile_detailed(request.clone()))
let started = Instant::now();
let result = job.compile_detailed(request.clone());
if trace_preparation {
eprintln!("Texrra editor preparation {}", serde_json::json!({
"stage": "seed",
"pass": pass,
"ms": started.elapsed().as_secs_f64() * 1000.0,
"format": result.as_ref().ok().and_then(|doc| doc.format_preparation.as_ref())
.map(|format| format!("{format:?}")),
"ok": result.is_ok(),
}));
}
Ok(result)
};
let mut seed = compile_seed()?;
let mut seed = compile_seed(1)?;
let mut bibliography = None;
if let Ok(initial) = &seed {
match bibliography::from_seed(&request, &initial.resources.generated_artifacts, &files) {
let cached_bbl = project_id
.filter(|id| {
!id.is_empty() && id.bytes().all(|byte| byte.is_ascii_alphanumeric()
|| matches!(byte, b'-' | b'_'))
})
.and_then(|id| {
run.parent().map(|root| root.join(".cached-bibliographies")
.join(format!("{id}.bbl-cache")))
});
match bibliography::from_seed(
&request, &initial.resources.generated_artifacts, &files, cached_bbl.as_deref(),
) {
Ok(Some((plan, contents))) => {
let path = plan.output().to_owned();
copy_with_date(&path, &contents, Some(SystemTime::now()), &mut dates)?;
files.insert(path, contents);
bibliography = Some(plan);
seed = compile_seed()?;
seed = compile_seed(2)?;
}
Ok(None) => {}
Err(error) => eprintln!("Editor bibliography: {error}"),