Skip to content

Add warm-engine mechanisms for autopdf #2

@BuddhiLW

Description

@BuddhiLW

✨ Feature Request: Warm-Engine (Persistent LaTeX Worker)

Summary

Add a mode to AutoPDF in which the LaTeX engine (pdflatex, xelatex, or lualatex) remains alive and pre-loaded across multiple document builds.
This persistent (“warm”) mode avoids re-initializing packages, fonts, and macros for every render — drastically improving throughput and latency for API or batch workloads.


Motivation

Currently, each AutoPDF run spawns a new LaTeX process that performs full initialization (format load, font cache, macro expansion, hyphenation setup).
This is acceptable for ad-hoc jobs but inefficient in backend scenarios that generate many similar PDFs.

A persistent engine can:

  • Eliminate redundant startup overhead.
  • Reduce per-document build time from seconds to milliseconds.
  • Improve scalability for services like cartas-backend, obito-backend, or other AutoPDF clients.

Goals

  • Introduce an optional warm mode: --warm, --daemon, or via config.
  • Support worker pools: one or more long-lived LaTeX subprocesses.
  • Allow .fmt pre-loading for faster engine startup.
  • Maintain backward compatibility (cold mode remains default).
  • Provide configurable knobs: pool size, idle timeout, jobs per worker.

Scope & Deliverables

1. Worker Manager

  • New module (e.g., internal/autopdf/worker/) managing LaTeX subprocesses.
  • Worker struct wraps a persistent LaTeX process with stdin/stdout handles.
  • Each worker:
    • Accepts .tex jobs via stdin or temp files.
    • Emits logs and output PDFs.
    • Can restart automatically on error or timeout.

2. Persistent Engine Invocation

  • Launch engine with -fmt=<custom> (if provided) for faster startup.
  • Keep process open between builds.
  • Clean job isolation (per-job workdir, *.aux cleanup).
  • Graceful restart on crash.

3. CLI / Config Additions

autopdf build --warm \
  --worker-pool-size=4 \
  --worker-idle-timeout=300s \
  --preload-fmt=/path/to/autopdf.fmt \
  template.tex config.yaml

YAML Config Example:

engine:
  warm:
    enabled: true
    poolSize: 2
    idleTimeout: 60   # seconds
    fmtFile: "autopdf.fmt"

4. Precompiled Format Support

  • Optional .fmt file created via:

    pdflatex -ini -jobname=autopdf "&pdflatex" \input{autopdf.ltx}
  • Allows LaTeX to skip package loading on every run.

5. Error Handling

  • Capture engine stdout/stderr and exit codes.
  • Restart failed workers automatically.
  • Clean shutdown of idle workers.
  • Optional job retry/back-off strategy.

6. Testing & Benchmarking

  • Benchmark warm vs cold mode latency.
  • Verify correctness (no cross-job artifacts).
  • Document measurable performance gains.

Benefits

  • 3–5× faster PDF generation under load.
  • Predictable latency for web/API workflows.
  • Lower CPU usage and I/O overhead.
  • Improved competitiveness for high-volume deployments.

Risks / Considerations

  • Persistent workers may accumulate memory; periodic restarts needed.
  • Job isolation (e.g., leftover .aux) must be ensured.
  • Added complexity — must remain modular and optional.

Success Criteria

  • Warm mode is opt-in and backward compatible.
  • Multiple consecutive builds show significant latency reduction.
  • Worker pool stable under parallel load.
  • Clear documentation and examples.

Suggested Next Steps

  1. Implement Worker and WorkerPool abstractions.
  2. Add CLI/config integration.
  3. Support .fmt pre-loading.
  4. Write integration tests and benchmarks.
  5. Update README and examples.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions