Repository navigation
Reduce PDF rendering allocations with a shared content builder - #712
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Invariant writer formatting QRCoder/Extensions/StreamWriterInterpolatedStringHandler.cs, QRCoder/Extensions/StreamWriterExtensions.cs, QRCoder/Attributes/InterpolatedStringHandlerAttributes.cs, QRCoderTests/StreamWriterExtensionsTests.cs |
The new interpolated-string handler writes literals and formats values with invariant culture. Tests cover numeric formats, Unicode text, null interpolation, and UTF-8 and UTF-16 output. The warning-disable directive moved above the RefStructs documentation. |
PDF generation QRCoder/PdfByteQRCode.cs, QRCoderTests/PdfByteQRCodeRendererTests.cs |
PDF commands now use invariant formatting and a StringBuilder. RGB arrays must contain three bytes. The content-stream length uses the builder’s character count. A test compares generated bytes under invariant and de-DE cultures. |
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Possibly related PRs
- Shane32/QRCoder#696: Adds
AppendInvariantand interpolated-string handler attributes, which share formatting infrastructure with this change.
Suggested reviewers: kamilla11, krisvandermotten
Merge Risk: 
Minimal · up to b16d4
-
No concrete merge-blocking issue is identified; the change appears mergeable after normal checks.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passedFull details: Docstring Coverage
-
Explanation
Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 6 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
Generate docstrings
-
- Commit to this branch
- Create a new PR
Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
(cherry picked from commit ee1b102)
Preserve the shared helpers from master and correct compatibility-attribute pragma placement.
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the PDF rendering allocation-reduction changes.
No open issues 🎉
What changed in this PR
Reduces PDF rendering allocations in QRCoder by sharing one content builder and writing it directly on newer targets, while preserving numeric precision and output compatibility.
Changes:
- Combines drawing commands and module paths in one
StringBuilder, with aToString()fallback for older targets. - Adds invariant-culture writer interpolation with stack-buffer numeric formatting on .NET 6+.
- Adds culture and encoding regression tests and adjusts a compatibility attribute’s warning pragma placement.
| File | Description |
|---|---|
QRCoderTests/StreamWriterExtensionsTests.cs |
Tests invariant numeric formatting, nullable text, and encoding preservation. |
QRCoderTests/PdfByteQRCodeRendererTests.cs |
Checks identical PDF bytes across cultures. |
QRCoder/PdfByteQRCode.cs |
Uses a shared content builder and invariant writer formatting. |
QRCoder/Extensions/StreamWriterInterpolatedStringHandler.cs |
Implements direct invariant interpolation with numeric stack buffers. |
QRCoder/Extensions/StreamWriterExtensions.cs |
Adds the internal WriteInvariant extension. |
QRCoder/Attributes/InterpolatedStringHandlerAttributes.cs |
Moves a warning pragma above XML documentation. |
Review effort: Balanced
gfoidl
left a comment
There was a problem hiding this comment.
Just one suggestion, otherwise LGTM.
Note: that suggestion can be handle as separate PR too.
|
|
||
| // Parse colors | ||
| var darkColor = darkColorHtmlHex.HexColorToByteArray(); | ||
| var lightColor = lightColorHtmlHex.HexColorToByteArray(); |
There was a problem hiding this comment.
Can we avoid that byte[] allocations?
E.g. by using a InlineArray(3) (for .NET 8+).
Or a Span<byte> color = stackalloc[3].
Summary
PDF rendering creates separate path and content strings before writing them to the output stream. Build the drawing commands and module paths in one
StringBuilder, then write its chunks directly on .NET 5+. Older targets retain aToString()fallback.Add an internal
StreamWriter.WriteInvariantinterpolation extension for PDF object numbers, dimensions, and cross-reference entries. It formats numeric values using invariant culture; .NET 6+ uses a stack buffer for supported numeric types. Preserve the existing PDF precision and byte output, with aligned comments explaining each PDF command.Also move a compatibility attribute's warning pragma above its XML documentation to fix the existing formatting diagnostic on older targets.
Performance
Compared with current master (
17e81d30c8c1718e00fa3acbddf4c2e0e297fe62), using the final merged commit (b16d43f) and its Release .NET 6 library:Local rendering microbenchmark on Windows/.NET 6.0.36 with tiered compilation disabled. Generate
"test"with ECC M and fixed QR versions 1/10/40 once, reuse the QR data and renderer, then measureGetGraphic(5)with default black/white colors and 150 DPI. QR generation is excluded. Each renderer receives 1,000 warmup calls; three measured rounds alternate baseline/candidate order with 100,000/20,000/3,000 renders per batch, respectively. Collect GC before each batch, measure allocations withGC.GetAllocatedBytesForCurrentThread, and report median elapsed time.Allocation counts were identical across all three rounds. Observed time ranges (master → new), in µs/render: version 1, 5.141–5.531 → 4.798–5.995; version 10, 37.013–38.870 → 33.537–34.365; version 40, 458.275–462.465 → 370.431–375.894. The smallest case has overlapping timing ranges and an outlier, so its speed is treated as comparable. These are local rendering measurements rather than end-to-end throughput guarantees.
Test plan
de-DE, numeric formats/custom culture signs, oversized formats, nullable strings, large Unicode strings, and UTF-8/UTF-16 writer encodings.git diff --checkpasses.Summary by CodeRabbit