appcore-filemaker 0.1.0-beta.1

Deterministic declarative document, canvas, and dataset compiler for AppCore
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// =============================================================================
//        #######
//     ###       ###     F: runtime.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/31 12:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/09/02 18:00:00 by dnettoRaw
//      ###########      S: 1.0.2-rc
// =============================================================================

//! Measures focused compilation and bounded editable/hybrid A4 report pipelines.

use appcore_filemaker::{
    export, export_collision_mask, export_dataset_csv, preflight, BorrowedDataset, CollisionMask,
    Compiler, DataValue, DocumentFingerprint, DocumentIr, ElementId, ExportContext, ExportFormat,
    ExportRequest, Fidelity, FontAsset, FontManager, HtmlMode, LayoutEngine, LayoutOptions,
    MaskFormat, OperationControl, Patch, PatchOperation, PatchTransaction, PdfMode,
    PreflightOptions, Rect, ResolvedScene, ResourceLimits, Size, Unit,
};
use std::hint::black_box;
use std::io::{sink, Write};
use std::time::Instant;

const CANVAS_TEMPLATE: &[u8] = br"filemaker: '1.0'
model: canvas
id: bench-document
page: { width: 1920px, height: 1080px }
elements:
  - { id: title, type: text, x: 80px, y: 80px, width: 800px, height: 120px, text: 'Bench' }
  - { id: panel, type: rect, x: 80px, y: 240px, width: 1760px, height: 700px }
";
const A4_TEMPLATE: &[u8] = include_bytes!("../examples/intermediate.yml");
const A4_DATA: &[u8] = include_bytes!("../examples/intermediate-data.json");
const NOTO_SANS: &[u8] = include_bytes!("../examples/assets/NotoSans-Regular.ttf");
const COMPILE_CASE: &str = "compile_canvas_yaml";
const A4_CASE: &str = "a4_report_end_to_end";
const A4_HYBRID_CASE: &str = "a4_report_pdf_hybrid";
const A4_EXPORT_MATRIX_CASE: &str = "a4_report_export_matrix";
const FINGERPRINT_CASE: &str = "fingerprint_json_4m";
const COLLISION_MASK_CASE: &str = "collision_mask_json_4m";
const COLLISION_MASK_PDF_CASE: &str = "collision_mask_pdf_100k";

#[derive(Default)]
struct ByteCounter {
    bytes: usize,
}

impl Write for ByteCounter {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        self.bytes = self
            .bytes
            .checked_add(bytes.len())
            .ok_or_else(|| std::io::Error::other("benchmark byte count overflow"))?;
        Ok(bytes.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    memory_checkpoint("idle", true);
    let selected = std::env::var("APPCORE_BENCH_CASE").ok();
    if selected
        .as_deref()
        .is_none_or(|value| value == COMPILE_CASE)
    {
        benchmark_compile_canvas()?;
    }
    if selected.as_deref().is_none_or(|value| value == A4_CASE) {
        benchmark_a4_report(A4_CASE, PdfMode::Editable)?;
    }
    if selected
        .as_deref()
        .is_none_or(|value| value == A4_HYBRID_CASE)
    {
        benchmark_a4_report(A4_HYBRID_CASE, PdfMode::Hybrid)?;
    }
    if selected
        .as_deref()
        .is_none_or(|value| value == A4_EXPORT_MATRIX_CASE)
    {
        benchmark_a4_export_matrix()?;
    }
    if selected
        .as_deref()
        .is_none_or(|value| value == FINGERPRINT_CASE)
    {
        benchmark_fingerprint()?;
    }
    if selected
        .as_deref()
        .is_none_or(|value| value == COLLISION_MASK_CASE)
    {
        benchmark_collision_mask_json()?;
    }
    if selected
        .as_deref()
        .is_none_or(|value| value == COLLISION_MASK_PDF_CASE)
    {
        benchmark_collision_mask_pdf()?;
    }
    if let Some(value) = selected.as_deref() {
        if value != COMPILE_CASE
            && value != A4_CASE
            && value != A4_HYBRID_CASE
            && value != A4_EXPORT_MATRIX_CASE
            && value != FINGERPRINT_CASE
            && value != COLLISION_MASK_CASE
            && value != COLLISION_MASK_PDF_CASE
        {
            return Err(format!("unknown FileMaker benchmark case: {value}").into());
        }
    }
    memory_checkpoint("retained", true);
    Ok(())
}

fn benchmark_collision_mask_json() -> Result<(), Box<dyn std::error::Error>> {
    let limits = ResourceLimits::default();
    let mask = collision_mask(20_736)?;
    let mut counter = ByteCounter::default();
    export_collision_mask(&mask, MaskFormat::Json, 72, &limits, &mut counter)?;
    println!(
        "appcore-filemaker::{COLLISION_MASK_CASE} fixture_bytes={}",
        counter.bytes
    );
    measure(COLLISION_MASK_CASE, 10, || {
        black_box(export_collision_mask(
            &mask,
            MaskFormat::Json,
            72,
            &limits,
            &mut sink(),
        )?);
        Ok(())
    })
}

fn benchmark_collision_mask_pdf() -> Result<(), Box<dyn std::error::Error>> {
    let limits = ResourceLimits::default();
    let mask = collision_mask(100_000)?;
    let mut counter = ByteCounter::default();
    export_collision_mask(&mask, MaskFormat::Pdf, 72, &limits, &mut counter)?;
    println!(
        "appcore-filemaker::{COLLISION_MASK_PDF_CASE} fixture_bytes={}",
        counter.bytes
    );
    measure(COLLISION_MASK_PDF_CASE, 5, || {
        black_box(export_collision_mask(
            &mask,
            MaskFormat::Pdf,
            72,
            &limits,
            &mut sink(),
        )?);
        Ok(())
    })
}

fn collision_mask(entries: usize) -> Result<CollisionMask, appcore_filemaker::FileMakerError> {
    let bounds = Rect::new(Unit::ZERO, Unit::ZERO, Unit::points(10)?, Unit::points(10)?)?;
    let occupied = (0..entries)
        .map(|index| Ok((ElementId::new(format!("bench-{index:05}"))?, bounds)))
        .collect::<Result<Vec<_>, appcore_filemaker::FileMakerError>>()?;
    Ok(CollisionMask {
        page: 0,
        size: Size::new(Unit::points(1920)?, Unit::points(1080)?)?,
        occupied,
        free: Vec::new(),
        collisions: Vec::new(),
        overflow: Vec::new(),
    })
}

fn benchmark_fingerprint() -> Result<(), Box<dyn std::error::Error>> {
    let limits = ResourceLimits::default();
    let compiler = Compiler::builder().limits(limits.clone()).build()?;
    let template = compiler.compile_template_yaml(CANVAS_TEMPLATE)?;
    let data = DataValue::String("d".repeat(4 * 1024 * 1024));
    let fonts = FontManager::default();
    measure(FINGERPRINT_CASE, 10, || {
        black_box(DocumentFingerprint::compute(
            &template, &data, None, &fonts, &limits,
        )?);
        Ok(())
    })
}

fn benchmark_compile_canvas() -> Result<(), Box<dyn std::error::Error>> {
    let compiler = Compiler::builder().build()?;
    measure(COMPILE_CASE, 2_000, || {
        black_box(compiler.compile_template_yaml(black_box(CANVAS_TEMPLATE)))?;
        Ok(())
    })
}

fn benchmark_a4_report(
    case_name: &str,
    pdf_mode: PdfMode,
) -> Result<(), Box<dyn std::error::Error>> {
    let limits = ResourceLimits::default();
    let compiler = Compiler::builder().limits(limits.clone()).build()?;
    let fonts = fonts()?;
    let request = ExportRequest {
        format: ExportFormat::Pdf,
        pdf_mode,
        ..ExportRequest::default()
    };
    let control = OperationControl::default();
    let patch = a4_patch()?;
    measure(case_name, 5, || {
        let (_, scene) = resolve_a4(&compiler, &limits, &fonts, &patch)?;
        let context = ExportContext {
            limits: &limits,
            fonts: &fonts,
            assets: None,
        };
        let report = preflight(
            &scene,
            &request,
            &context,
            &PreflightOptions {
                strict: true,
                ..PreflightOptions::default()
            },
            &control,
        )?;
        report.enforce(true)?;
        let outcome = export(&scene, &request, &context, &mut sink())?;
        black_box((scene.pages.len(), outcome.bytes_written));
        Ok(())
    })
}

fn benchmark_a4_export_matrix() -> Result<(), Box<dyn std::error::Error>> {
    let limits = ResourceLimits::default();
    let compiler = Compiler::builder().limits(limits.clone()).build()?;
    let fonts = fonts()?;
    let requests = export_matrix_requests();
    let patch = a4_patch()?;
    let control = OperationControl::default();
    measure(A4_EXPORT_MATRIX_CASE, 1, || {
        let (document, scene) = resolve_a4(&compiler, &limits, &fonts, &patch)?;
        let context = ExportContext {
            limits: &limits,
            fonts: &fonts,
            assets: None,
        };
        let mut bytes_written = 0usize;
        for request in &requests {
            let strict = request.fidelity == Fidelity::Strict;
            let report = preflight(
                &scene,
                request,
                &context,
                &PreflightOptions {
                    strict,
                    ..PreflightOptions::default()
                },
                &control,
            )?;
            report.enforce(strict)?;
            let outcome = export(&scene, request, &context, &mut sink())?;
            bytes_written = bytes_written
                .checked_add(outcome.bytes_written)
                .ok_or_else(|| std::io::Error::other("export byte count overflow"))?;
        }
        let table = document
            .elements
            .iter()
            .find_map(|element| element.table.as_ref())
            .ok_or_else(|| std::io::Error::other("A4 fixture has no table"))?;
        let outcome = export_dataset_csv(
            &table.spec,
            &BorrowedDataset::new(&table.rows),
            &limits,
            &mut sink(),
        )?;
        bytes_written = bytes_written
            .checked_add(outcome.bytes_written)
            .ok_or_else(|| std::io::Error::other("export byte count overflow"))?;
        black_box((scene.pages.len(), bytes_written));
        Ok(())
    })
}

fn resolve_a4(
    compiler: &Compiler,
    limits: &ResourceLimits,
    fonts: &FontManager,
    patch: &Patch,
) -> Result<(DocumentIr, ResolvedScene), Box<dyn std::error::Error>> {
    let template = compiler.compile_template_yaml(black_box(A4_TEMPLATE))?;
    let data: DataValue = serde_json::from_slice(black_box(A4_DATA))?;
    let mut document = compiler.bind(&template, &data, &[])?;
    PatchTransaction::new(&mut document, limits.max_patch_operations).apply(patch)?;
    let scene = LayoutEngine::new(limits, fonts, LayoutOptions::default())?.resolve(&document)?;
    Ok((document, scene))
}

fn export_matrix_requests() -> [ExportRequest; 8] {
    [
        pdf_request(PdfMode::Editable),
        pdf_request(PdfMode::Flattened),
        pdf_request(PdfMode::Hybrid),
        ExportRequest {
            format: ExportFormat::Svg,
            ..ExportRequest::default()
        },
        ExportRequest {
            format: ExportFormat::Html,
            ..ExportRequest::default()
        },
        ExportRequest {
            format: ExportFormat::Html,
            html_mode: HtmlMode::Fixed,
            ..ExportRequest::default()
        },
        ExportRequest {
            format: ExportFormat::Png,
            page: Some(0),
            dpi: 36,
            ..ExportRequest::default()
        },
        ExportRequest {
            format: ExportFormat::Jpeg,
            fidelity: Fidelity::BestEffort,
            page: Some(0),
            dpi: 36,
            ..ExportRequest::default()
        },
    ]
}

fn pdf_request(pdf_mode: PdfMode) -> ExportRequest {
    ExportRequest {
        format: ExportFormat::Pdf,
        pdf_mode,
        ..ExportRequest::default()
    }
}

fn a4_patch() -> Result<Patch, appcore_filemaker::FileMakerError> {
    Ok(Patch {
        sequence: 1,
        operations: vec![PatchOperation::Move {
            id: ElementId::new("review-marker")?,
            x: "185mm".parse()?,
            y: "112mm".parse()?,
        }],
    })
}

fn fonts() -> Result<FontManager, Box<dyn std::error::Error>> {
    let mut fonts = FontManager::default();
    fonts.register(FontAsset::new("NotoSans", NOTO_SANS.to_vec(), 0)?)?;
    Ok(fonts)
}

fn measure(
    case_name: &str,
    fallback_iterations: u64,
    mut operation: impl FnMut() -> Result<(), Box<dyn std::error::Error>>,
) -> Result<(), Box<dyn std::error::Error>> {
    let iterations = iterations(fallback_iterations);
    memory_checkpoint("workload", true);
    let started = Instant::now();
    for _ in 0..iterations {
        operation()?;
    }
    let total_ns = started.elapsed().as_nanos();
    println!(
        "appcore-filemaker::{case_name} iterations={iterations} total_ns={total_ns} ns_per_iter={:.2}",
        total_ns as f64 / iterations as f64
    );
    Ok(())
}

fn memory_checkpoint(phase: &str, settle: bool) {
    let Some(milliseconds) = std::env::var("APPCORE_BENCH_MEMORY_CHECKPOINT_MS")
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| (1..=1_000).contains(value))
    else {
        return;
    };
    println!(
        "appcore-bench-memory phase={phase} pid={}",
        std::process::id()
    );
    let _ = std::io::stdout().flush();
    if settle {
        std::thread::sleep(std::time::Duration::from_millis(milliseconds));
    }
}

fn iterations(fallback: u64) -> u64 {
    std::env::var("APPCORE_BENCH_ITERATIONS")
        .ok()
        .and_then(|value| value.parse().ok())
        .filter(|value| *value > 0)
        .unwrap_or(fallback)
}