Skip to content

Reuse UTF-8 buffers when rendering templates - #3727

Merged
itowlson merged 3 commits into
spinframework:mainfrom
ChihweiLHBird:zhiwei/templates-reuse-utf8-buffers
Sep 28, 2026
Merged

itowlson merged 3 commits into
spinframework:mainfrom
ChihweiLHBird:zhiwei/templates-reuse-utf8-buffers

Conversation

@ChihweiLHBird

@ChihweiLHBird ChihweiLHBird commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Use the consuming String::from_utf8 and String::into_bytes conversions in the template renderer, so template text and rendered output reuse their buffers instead of being copied. Also inlines the single-use string_from_bytes helper.

Template files were validated with `str::from_utf8` and then copied into a new `String`, and rendered output was copied back into a `Vec<u8>` byte by byte. Use the consuming `String::from_utf8` and `String::into_bytes` conversions so both steps reuse the existing allocation. Binary files and text that fails to parse as a Liquid template still pass through byte-for-byte, since `FromUtf8Error::into_bytes` returns the original buffer and UTF-8 validation never alters it.

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
None => Ok(TemplateContent::Binary(raw)),
Some(s) => {
match String::from_utf8(raw) {
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // TODO: try other encodings!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This would benefit from a comment: my first reading was "wait is this rendering the error" and I had to look at the docs to understand "oh the error has methods to retrieve the blob that got consumed."

Suggested change
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // TODO: try other encodings!
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // retrieves the original blob // TODO: try other encodings!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @itowlson, thanks for the feedback, and this is indeed very confusing. I rephrased it a little bit and pushed in 57b22da
Is that better? Let me know if you would suggest a further change.

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird
ChihweiLHBird marked this pull request as ready for review September 28, 2026 05:09

@itowlson itowlson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good - thanks!

As a minor nit, it may be better to do small fixups as amendments, rather than whole new commits. The second and third commit don't really have useful historical value - we could squash this into one commit and for me it would be clearer that way. (Unfortunately, our policies around verified commits mean we can't do that via a GitHub squash-merge, which is what I'd usually do.) But that's not a blocker, just something to consider for future PRs.

@itowlson
itowlson merged commit e95d4a6 into spinframework:main Sep 28, 2026
17 checks passed
@ChihweiLHBird
ChihweiLHBird deleted the zhiwei/templates-reuse-utf8-buffers branch September 28, 2026 18:48
@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

Looks good - thanks!

As a minor nit, it may be better to do small fixups as amendments, rather than whole new commits. The second and third commit don't really have useful historical value - we could squash this into one commit and for me it would be clearer that way. (Unfortunately, our policies around verified commits mean we can't do that via a GitHub squash-merge, which is what I'd usually do.) But that's not a blocker, just something to consider for future PRs.

Ah got it, sorry about that. Will do them in a single commit next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants