Skip to main content

Template Engine Architecture

The template engine provides {{variable}} interpolation for workflows. It uses the pongo2 template library (Django-compatible) with custom optimizations for high-concurrency scenarios.

Template Syntax

Standard Variables

Standard template variables use {{variable}} syntax:

Secondary Variables (Foreach)

Foreach loops use [[variable]] syntax to avoid conflicts:

Generator Functions

Generator functions provide dynamic values:

Engine Types

The template system provides two engine implementations:
EngineDescriptionUse Case
EngineStandard single-threaded engineSimple workflows
ShardedEngineHigh-performance sharded cacheHigh concurrency

TemplateEngine Interface

BatchRenderer Interface

For high-throughput scenarios, the BatchRenderer interface extends TemplateEngine:

ShardedEngine Architecture

The ShardedEngine distributes templates across multiple shards to reduce lock contention:

Configuration

Performance Features

  1. Sharded Caching: Templates are distributed across shards using FNV-1a hash, reducing lock contention.
  2. RWMutex per Shard: Read-heavy workloads benefit from reader-writer locks.
  3. Quick Path: Templates without {{ are returned immediately without processing.
  4. Lock-Free Execution: Compiled pongo2 templates are thread-safe; execution happens outside locks.
  5. Context Pooling: Reusable context maps reduce GC pressure.

Render Flow

Precompiled Templates

The PrecompiledRegistry stores pre-compiled templates for workflows:

Precompiler Interface

Usage

Context Pooling

Context maps are pooled to reduce memory allocations:

Normalized Boolean Handling

Boolean values are normalized for template compatibility:

Generator Functions

Generator functions produce dynamic values:

Usage

Batch Rendering

For high-throughput scenarios, batch rendering reduces lock acquisitions:

Secondary Template Rendering

Foreach loops use [[variable]] to avoid conflicts with pre-rendered {{variables}}:

Performance Benchmarks

Typical performance characteristics:
OperationSingle-threaded10 Concurrent100 Concurrent
Simple render500ns600ns800ns
Cached render200ns250ns400ns
Batch render (10)1.5µs2µs3µs
Pre-compiled150ns200ns350ns

Best Practices

  1. Use pre-compilation for frequently executed workflows
  2. Enable pooling for high-concurrency scenarios
  3. Use batch rendering when rendering multiple templates with the same context
  4. Avoid unnecessary templates - only use {{}} when variable substitution is needed
  5. Use secondary syntax [[]] for foreach loop variables to avoid double-rendering issues