Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / pdf / src / main / rust / src / perf_tests_part2.rs
perf_tests_part2.rs400 lines · 14.7 KiBRaw
| 1 | fn ocg_doc(n_sections: usize, distinct: bool) -> (Document, ObjectId) { |
| 2 | let mut doc = Document::with_version("1.5"); |
| 3 | let n_ocgs = if distinct { n_sections } else { 1 }; |
| 4 | let mut ocg_ids = Vec::new(); |
| 5 | for i in 0..n_ocgs { |
| 6 | let id = doc.add_object(dictionary! { |
| 7 | "Type" => "OCG", |
| 8 | "Name" => Object::string_literal(format!("Layer{}", i)), |
| 9 | }); |
| 10 | ocg_ids.push(id); |
| 11 | } |
| 12 | // Every group listed in /ON, so the resolved config holds an n-element set — |
| 13 | // which used to be an n-element array cloned and scanned on each call the |
| 14 | // memo did not absorb. |
| 15 | let on: Vec<Object> = ocg_ids.iter().map(|id| Object::Reference(*id)).collect(); |
| 16 | let all: Vec<Object> = on.clone(); |
| 17 | let d = dictionary! { "ON" => on, "OFF" => Vec::<Object>::new(), "BaseState" => "ON" }; |
| 18 | let cat_extra = dictionary! { "OCGs" => all, "D" => d }; |
| 19 | |
| 20 | let mut props = Dictionary::new(); |
| 21 | let mut s = String::new(); |
| 22 | for i in 0..n_sections { |
| 23 | let which = if distinct { i } else { 0 }; |
| 24 | props.set(format!("P{}", i), ocg_ids[which]); |
| 25 | s.push_str(&format!( |
| 26 | "/OC /P{} BDC\n0.1 0.2 0.3 rg\n{} 10 5 5 re f\nEMC\n", |
| 27 | i, |
| 28 | (i % 300) * 2 |
| 29 | )); |
| 30 | } |
| 31 | let cid = doc.add_object(Stream::new(Dictionary::new(), s.into_bytes())); |
| 32 | let res = dictionary! { "Properties" => props }; |
| 33 | let pages_id = doc.new_object_id(); |
| 34 | let pid = doc.add_object(dictionary! { |
| 35 | "Type" => "Page", "Parent" => pages_id, |
| 36 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 37 | "Contents" => cid, "Resources" => res, |
| 38 | }); |
| 39 | doc.objects.insert( |
| 40 | pages_id, |
| 41 | Object::Dictionary(dictionary! { |
| 42 | "Type" => "Pages", "Kids" => vec![pid.into()], "Count" => 1, |
| 43 | }), |
| 44 | ); |
| 45 | let cat = doc.add_object(dictionary! { |
| 46 | "Type" => "Catalog", |
| 47 | "Pages" => pages_id, |
| 48 | "OCProperties" => cat_extra, |
| 49 | }); |
| 50 | doc.trailer.set("Root", cat); |
| 51 | (doc, pid) |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | #[ignore] |
| 56 | fn perf_optional_content_claim() { |
| 57 | println!("\n=== CLAIM: optional-content config hoisting (N BDC/EMC sections) ==="); |
| 58 | // Extended past 1600 to answer a specific question: after the per-call |
| 59 | // clones were removed, do the two remaining linear scans of /ON and /OFF |
| 60 | // still produce a measurable O(N^2) at realistic layer counts? |
| 61 | let ns = [100usize, 400, 1600, 3200, 6400]; |
| 62 | let mut same = Vec::new(); |
| 63 | let mut dist = Vec::new(); |
| 64 | for &n in &ns { |
| 65 | // Big N is slow if the quadratic is still there; keep the sample count |
| 66 | // low enough that the sweep terminates either way. |
| 67 | let iters = if n > 1600 { 5 } else { 9 }; |
| 68 | let (doc, pid) = ocg_doc(n, false); |
| 69 | let st = bench(iters, || { |
| 70 | black_box(interpret_page(&doc, pid).unwrap()); |
| 71 | }); |
| 72 | st.row("N sections, all ONE ocg (memo hits)", n, ""); |
| 73 | same.push(st.median_ms); |
| 74 | |
| 75 | let (doc2, pid2) = ocg_doc(n, true); |
| 76 | let st2 = bench(iters, || { |
| 77 | black_box(interpret_page(&doc2, pid2).unwrap()); |
| 78 | }); |
| 79 | st2.row("N sections, N DISTINCT ocgs", n, ""); |
| 80 | dist.push(st2.median_ms); |
| 81 | println!( |
| 82 | " per-section cost: one-ocg {:.4} ms, distinct {:.4} ms", |
| 83 | st.median_ms / n as f64, |
| 84 | st2.median_ms / n as f64 |
| 85 | ); |
| 86 | println!(); |
| 87 | } |
| 88 | println!(" all one OCG (memoized):"); |
| 89 | scaling(&ns, &same); |
| 90 | println!(" N distinct OCGs (memo cannot help):"); |
| 91 | scaling(&ns, &dist); |
| 92 | println!(" A flat 'per-section cost' for the distinct series means linear;"); |
| 93 | println!(" a per-section cost that grows with N means the scans still bite."); |
| 94 | } |
| 95 | |
| 96 | // --------------------------------------------------------------------------- |
| 97 | // 5. CLAIM: shading raster sizing (square vs long thin) |
| 98 | // --------------------------------------------------------------------------- |
| 99 | |
| 100 | fn axial_shading_dict() -> Dictionary { |
| 101 | dictionary! { |
| 102 | "ShadingType" => 2, |
| 103 | "ColorSpace" => "DeviceRGB", |
| 104 | "Coords" => vec![0.into(), 0.into(), 600.into(), 0.into()], |
| 105 | "Function" => dictionary! { |
| 106 | "FunctionType" => 2, |
| 107 | "Domain" => vec![0.into(), 1.into()], |
| 108 | "C0" => vec![1.into(), 0.into(), 0.into()], |
| 109 | "C1" => vec![0.into(), 0.into(), 1.into()], |
| 110 | "N" => 1, |
| 111 | }, |
| 112 | "Extend" => vec![true.into(), true.into()], |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn shading_doc(n: usize, clip: (f64, f64)) -> (Document, ObjectId) { |
| 117 | let mut doc = Document::with_version("1.5"); |
| 118 | let sh = doc.add_object(Object::Dictionary(axial_shading_dict())); |
| 119 | let mut s = String::new(); |
| 120 | for _ in 0..n { |
| 121 | s.push_str(&format!( |
| 122 | "q 0 0 {:.1} {:.1} re W n /Sh0 sh Q\n", |
| 123 | clip.0, clip.1 |
| 124 | )); |
| 125 | } |
| 126 | let cid = doc.add_object(Stream::new(Dictionary::new(), s.into_bytes())); |
| 127 | let res = dictionary! { "Shading" => dictionary! { "Sh0" => sh } }; |
| 128 | let pages_id = doc.new_object_id(); |
| 129 | let pid = doc.add_object(dictionary! { |
| 130 | "Type" => "Page", "Parent" => pages_id, |
| 131 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 132 | "Contents" => cid, "Resources" => res, |
| 133 | }); |
| 134 | doc.objects.insert( |
| 135 | pages_id, |
| 136 | Object::Dictionary(dictionary! { |
| 137 | "Type" => "Pages", "Kids" => vec![pid.into()], "Count" => 1, |
| 138 | }), |
| 139 | ); |
| 140 | let cat = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 141 | doc.trailer.set("Root", cat); |
| 142 | (doc, pid) |
| 143 | } |
| 144 | |
| 145 | fn raster_bytes(p: &PageData) -> usize { |
| 146 | p.prims |
| 147 | .iter() |
| 148 | .map(|pr| match pr { |
| 149 | Prim::Image { data, .. } => data.len(), |
| 150 | Prim::ImageTiled { data, .. } => data.len(), |
| 151 | _ => 0, |
| 152 | }) |
| 153 | .sum() |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | #[ignore] |
| 158 | fn perf_shading_claim() { |
| 159 | println!("\n=== CLAIM: shading raster sizing (square vs long thin) ==="); |
| 160 | let ns = [10usize, 40, 160]; |
| 161 | let mut meds = Vec::new(); |
| 162 | for &n in &ns { |
| 163 | let (doc, pid) = shading_doc(n, (600.0, 600.0)); |
| 164 | let st = bench(7, || { |
| 165 | black_box(interpret_page(&doc, pid).unwrap()); |
| 166 | }); |
| 167 | let p = interpret_page(&doc, pid).unwrap(); |
| 168 | st.row( |
| 169 | "N square shadings", |
| 170 | n, |
| 171 | &format!( |
| 172 | "prims={} raster={:.2} MiB", |
| 173 | p.prims.len(), |
| 174 | mib(raster_bytes(&p)) |
| 175 | ), |
| 176 | ); |
| 177 | meds.push(st.median_ms); |
| 178 | } |
| 179 | scaling(&ns, &meds); |
| 180 | |
| 181 | println!("\n one shading, square vs long-thin clip (raster allocation):"); |
| 182 | for (label, clip) in [ |
| 183 | ("square 600x600", (600.0, 600.0)), |
| 184 | ("thin 600x8", (600.0, 8.0)), |
| 185 | ("thin 600x2", (600.0, 2.0)), |
| 186 | ("thin 8x600", (8.0, 600.0)), |
| 187 | ] { |
| 188 | let (doc, pid) = shading_doc(1, clip); |
| 189 | let st = bench(15, || { |
| 190 | black_box(interpret_page(&doc, pid).unwrap()); |
| 191 | }); |
| 192 | let p = interpret_page(&doc, pid).unwrap(); |
| 193 | let mut dims = String::new(); |
| 194 | for pr in &p.prims { |
| 195 | if let Prim::Image { w, h, data, .. } = pr { |
| 196 | dims = format!("{}x{} ({} B)", w, h, data.len()); |
| 197 | } |
| 198 | } |
| 199 | st.row(label, 1, &format!("raster={}", dims)); |
| 200 | } |
| 201 | println!(" If the thin cases allocate ~the same bytes as the square one, the"); |
| 202 | println!(" raster is still effectively square-sized."); |
| 203 | } |
| 204 | |
| 205 | // --------------------------------------------------------------------------- |
| 206 | // 6. CLAIM: tiling pattern rasterization |
| 207 | // --------------------------------------------------------------------------- |
| 208 | |
| 209 | fn tiling_doc(pitch: f64) -> (Document, ObjectId) { |
| 210 | let mut doc = Document::with_version("1.5"); |
| 211 | let cell = format!( |
| 212 | "0 0 0 RG 0.4 w 0 0 m {:.2} {:.2} l S\n0 {:.2} m {:.2} 0 l S\n", |
| 213 | pitch, pitch, pitch, pitch |
| 214 | ); |
| 215 | let pat = doc.add_object(Stream::new( |
| 216 | dictionary! { |
| 217 | "Type" => "Pattern", |
| 218 | "PatternType" => 1, |
| 219 | "PaintType" => 1, |
| 220 | "TilingType" => 1, |
| 221 | "BBox" => vec![0.into(), 0.into(), pitch.into(), pitch.into()], |
| 222 | "XStep" => pitch, |
| 223 | "YStep" => pitch, |
| 224 | "Resources" => Dictionary::new(), |
| 225 | }, |
| 226 | cell.into_bytes(), |
| 227 | )); |
| 228 | let content = b"/Pattern cs /P0 scn\n0 0 612 792 re f\n".to_vec(); |
| 229 | let cid = doc.add_object(Stream::new(Dictionary::new(), content)); |
| 230 | let res = dictionary! { "Pattern" => dictionary! { "P0" => pat } }; |
| 231 | let pages_id = doc.new_object_id(); |
| 232 | let pid = doc.add_object(dictionary! { |
| 233 | "Type" => "Page", "Parent" => pages_id, |
| 234 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 235 | "Contents" => cid, "Resources" => res, |
| 236 | }); |
| 237 | doc.objects.insert( |
| 238 | pages_id, |
| 239 | Object::Dictionary(dictionary! { |
| 240 | "Type" => "Pages", "Kids" => vec![pid.into()], "Count" => 1, |
| 241 | }), |
| 242 | ); |
| 243 | let cat = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 244 | doc.trailer.set("Root", cat); |
| 245 | (doc, pid) |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | #[ignore] |
| 250 | fn perf_tiling_pattern_claim() { |
| 251 | println!("\n=== CLAIM: tiling pattern rasterization (full-page hatch) ==="); |
| 252 | // Smaller pitch = more tiles over the same page area, so tile count grows |
| 253 | // as 1/pitch^2. |
| 254 | let pitches = [32.0f64, 16.0, 8.0, 4.0, 2.0]; |
| 255 | let ns: Vec<usize> = pitches |
| 256 | .iter() |
| 257 | .map(|p| ((612.0 / p).ceil() * (792.0 / p).ceil()) as usize) |
| 258 | .collect(); |
| 259 | let mut meds = Vec::new(); |
| 260 | for (i, &pitch) in pitches.iter().enumerate() { |
| 261 | let (doc, pid) = tiling_doc(pitch); |
| 262 | let st = bench(7, || { |
| 263 | black_box(interpret_page(&doc, pid).unwrap()); |
| 264 | }); |
| 265 | let p = interpret_page(&doc, pid).unwrap(); |
| 266 | st.row( |
| 267 | &format!("hatch pitch {:.0} pt", pitch), |
| 268 | ns[i], |
| 269 | &format!( |
| 270 | "prims={} raster={:.3} MiB", |
| 271 | p.prims.len(), |
| 272 | mib(raster_bytes(&p)) |
| 273 | ), |
| 274 | ); |
| 275 | meds.push(st.median_ms); |
| 276 | } |
| 277 | scaling(&ns, &meds); |
| 278 | println!(" n here is the TILE COUNT covering the page. Flat time against a"); |
| 279 | println!(" growing tile count means the cell is rasterized once, not per tile."); |
| 280 | } |
| 281 | |
| 282 | // --------------------------------------------------------------------------- |
| 283 | // 7. Glyph outline extraction |
| 284 | // --------------------------------------------------------------------------- |
| 285 | |
| 286 | #[test] |
| 287 | #[ignore] |
| 288 | fn perf_glyph_outlines_scaling() { |
| 289 | println!("\n=== glyph outline extraction (embedded TrueType) ==="); |
| 290 | println!(" a) scaling in glyphs DRAWN (font fixed at 400 glyphs x 64 pts):"); |
| 291 | let ns = [200usize, 800, 3200]; |
| 292 | let mut meds = Vec::new(); |
| 293 | for &n in &ns { |
| 294 | let mut doc = Document::with_version("1.5"); |
| 295 | let res = embedded_font_res(&mut doc, 400, 64); |
| 296 | let pages_id = doc.new_object_id(); |
| 297 | let cid = doc.add_object(Stream::new(Dictionary::new(), text_content(n / 19 + 1))); |
| 298 | let pid = doc.add_object(dictionary! { |
| 299 | "Type" => "Page", "Parent" => pages_id, |
| 300 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 301 | "Contents" => cid, "Resources" => res, |
| 302 | }); |
| 303 | doc.objects.insert( |
| 304 | pages_id, |
| 305 | Object::Dictionary(dictionary! { |
| 306 | "Type" => "Pages", "Kids" => vec![pid.into()], "Count" => 1, |
| 307 | }), |
| 308 | ); |
| 309 | let cat = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 310 | doc.trailer.set("Root", cat); |
| 311 | let st = bench(9, || { |
| 312 | black_box(interpret_page(&doc, pid).unwrap()); |
| 313 | }); |
| 314 | let p = interpret_page(&doc, pid).unwrap(); |
| 315 | st.row("glyphs drawn (approx)", n, &format!("prims={}", p.prims.len())); |
| 316 | meds.push(st.median_ms); |
| 317 | } |
| 318 | scaling(&ns, &meds); |
| 319 | |
| 320 | println!(" b) scaling in FONT PROGRAM size (same tiny page, one text run):"); |
| 321 | let gs = [100u16, 400, 1600]; |
| 322 | let mut meds2 = Vec::new(); |
| 323 | let nsz: Vec<usize> = gs.iter().map(|g| *g as usize).collect(); |
| 324 | for &g in &gs { |
| 325 | let mut doc = Document::with_version("1.5"); |
| 326 | let res = embedded_font_res(&mut doc, g, 64); |
| 327 | let pages_id = doc.new_object_id(); |
| 328 | let cid = doc.add_object(Stream::new(Dictionary::new(), text_content(1))); |
| 329 | let pid = doc.add_object(dictionary! { |
| 330 | "Type" => "Page", "Parent" => pages_id, |
| 331 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 332 | "Contents" => cid, "Resources" => res, |
| 333 | }); |
| 334 | doc.objects.insert( |
| 335 | pages_id, |
| 336 | Object::Dictionary(dictionary! { |
| 337 | "Type" => "Pages", "Kids" => vec![pid.into()], "Count" => 1, |
| 338 | }), |
| 339 | ); |
| 340 | let cat = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 341 | doc.trailer.set("Root", cat); |
| 342 | let st = bench(11, || { |
| 343 | black_box(interpret_page(&doc, pid).unwrap()); |
| 344 | }); |
| 345 | st.row("font glyph count", g as usize, "one text run only"); |
| 346 | meds2.push(st.median_ms); |
| 347 | } |
| 348 | scaling(&nsz, &meds2); |
| 349 | println!(" (b) is the fixed cost a page pays just for HAVING a big embedded font."); |
| 350 | } |
| 351 | |
| 352 | // --------------------------------------------------------------------------- |
| 353 | // 8. Primitive count / memory: the "crasheshalfway" (OOM) hypothesis |
| 354 | // --------------------------------------------------------------------------- |
| 355 | |
| 356 | #[test] |
| 357 | #[ignore] |
| 358 | fn perf_primitive_cap_and_memory() { |
| 359 | println!("\n=== primitive count + PEAK HEAP (MAX_PRIMITIVES = {}) ===", MAX_PRIMITIVES); |
| 360 | println!(" Peak heap measured with a counting global allocator (net live bytes"); |
| 361 | println!(" since reset), not RSS. Timing rows are measured with tracking OFF."); |
| 362 | let ns = [10_000usize, 50_000, 200_000, 400_000]; |
| 363 | let mut meds = Vec::new(); |
| 364 | for &n in &ns { |
| 365 | let mut d0 = Document::with_version("1.5"); |
| 366 | let res = standard_font_res(&mut d0); |
| 367 | let (doc, pages) = build_doc(&[rect_content(n)], res); |
| 368 | |
| 369 | let st = bench(5, || { |
| 370 | black_box(interpret_page(&doc, pages[0]).unwrap()); |
| 371 | }); |
| 372 | |
| 373 | mem_reset(); |
| 374 | let p = interpret_page(&doc, pages[0]).unwrap(); |
| 375 | let prims = p.prims.len(); |
| 376 | let rb = raster_bytes(&p); |
| 377 | drop(p); |
| 378 | let peak = mem_peak(); |
| 379 | |
| 380 | st.row( |
| 381 | "rects requested", |
| 382 | n, |
| 383 | &format!( |
| 384 | "prims={} ({}) peak_heap={:.2} MiB prim_bytes~{:.2} MiB raster={:.2} MiB", |
| 385 | prims, |
| 386 | if prims >= MAX_PRIMITIVES { "CAPPED" } else { "under cap" }, |
| 387 | mib(peak), |
| 388 | mib(prims * std::mem::size_of::<Prim>()), |
| 389 | mib(rb), |
| 390 | ), |
| 391 | ); |
| 392 | meds.push(st.median_ms); |
| 393 | } |
| 394 | scaling(&ns, &meds); |
| 395 | println!(" size_of::<Prim>() = {} bytes", std::mem::size_of::<Prim>()); |
| 396 | println!( |
| 397 | " cap-implied ceiling on the primitive vector alone: {:.1} MiB", |
| 398 | mib(MAX_PRIMITIVES * std::mem::size_of::<Prim>()) |
| 399 | ); |
| 400 | } |