Skip to content

perf: avoid buffering stream when computing placeholder PNG C2PA chunk#2154

Merged
ok-nick merged 2 commits into
mainfrom
ok-nick/png-mem-opt
May 21, 2026
Merged

perf: avoid buffering stream when computing placeholder PNG C2PA chunk#2154
ok-nick merged 2 commits into
mainfrom
ok-nick/png-mem-opt

Conversation

@ok-nick
Copy link
Copy Markdown
Contributor

@ok-nick ok-nick commented May 18, 2026

Reduces memory usage when reading/signing a PNG that doesn't contain a C2PA manifest by optimizing get_object_locations_from_stream to quickly find where the C2PA chunk goes rather than computing it via write_cai. When signing a 100MB PNG, the total memory usage decreases by 29.4%.

Before:
Screenshot 2026-05-18 at 5 25 14 PM
After:
Screenshot 2026-05-18 at 5 25 30 PM

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 18, 2026

Merging this PR will improve performance by 11.96%

⚡ 1 improved benchmark
✅ 29 untouched benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation small 25.2 ms 22.5 ms +11.96%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ok-nick/png-mem-opt (223f661) with main (a48f1cc)

Open in CodSpeed

Footnotes

  1. 64 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 18, 2026

Merging this PR will improve performance by 12.06%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 29 untouched benchmarks
⏩ 64 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation small 25.2 ms 22.5 ms +12.06%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ok-nick/png-mem-opt (48f1ae2) with main (350e138)

Open in CodSpeed

Footnotes

  1. 64 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown
Member

@gpeacock gpeacock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there are side effects caused by modifying the input stream. We probably don't expect that. It is mut so that we can seek, not modify.

@ok-nick
Copy link
Copy Markdown
Contributor Author

ok-nick commented May 19, 2026

@gpeacock I'm not sure what you mean. The input stream is read-only and this change only seeks?

@ok-nick ok-nick merged commit 82ea133 into main May 21, 2026
44 checks passed
@ok-nick ok-nick deleted the ok-nick/png-mem-opt branch May 21, 2026 14:19
@ok-nick
Copy link
Copy Markdown
Contributor Author

ok-nick commented May 21, 2026

Discussed offline with Gavin.

@caiopensrc caiopensrc mentioned this pull request May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants