Skip to content

derive: make sure the size_hint is collected#700

Merged
Kijewski merged 3 commits intoaskama-rs:mainfrom
Kijewski:pr-size_hint
Feb 10, 2026
Merged

derive: make sure the size_hint is collected#700
Kijewski merged 3 commits intoaskama-rs:mainfrom
Kijewski:pr-size_hint

Conversation

@Kijewski
Copy link
Copy Markdown
Member

Make it a #[must_use] newtype to ensure that the every size_hint contributes to the total, to get a better estimate for the rendered size. By making the type #[must_use], a few missing size hint additions were found, e.g. text before {% continue %}.

@Kijewski Kijewski added the derive Related to the code generating label Feb 10, 2026
Make it a `#[must_use]` newtype to ensure that the every `size_hint`
contributes to the total, to get a better estimate for the rendered
size. By making the type `#[must_use]`, a few missing size hint
additions were found, e.g. text before `{% continue %}`.
}
"#,
&[],
12,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added the last commit that explains how size_hint in a loop is calculated, because I was confused how 8 characters in a loop could equal an estimate of 12:

((8 * 3) + 0) / 2 == 12.
  ^ then   ^ else

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

I have to admit I tend to skip the size hint a lot. ^^'

Well done!

@Kijewski
Copy link
Copy Markdown
Member Author

That's why #[must_use] is great! :) There's no way that I would have found all the cases where the size hint was ignored. Or at least I would not have bothered to read the entire code base of the generator.

But I'm not very confident that our size_hint is even remotely accurate, regardless if it is accumulated or not. :D

@Kijewski Kijewski merged commit ba23980 into askama-rs:main Feb 10, 2026
50 checks passed
@Kijewski Kijewski deleted the pr-size_hint branch February 10, 2026 15:19
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

I was actually wondering if it's actually that useful...

@Kijewski
Copy link
Copy Markdown
Member Author

I never tried to measure it myself, or read too deeply into the matter, but it seems rust's vector growth implementation (new capacity = old capacity * 2) is the worst implementation of the good implementations (linear growth is much worse). See e.g. facebook/folly/FBVector.h.

That why one shouldn't start with an empty String in rust, but that's also why an estimation that is just shy of the actual number is very bad in rust (overallocation, fragmentation). Some alternative string implementations are quite a bit faster than the built-in String, e.g. compact_str, that in addition has a small string optimization.

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

Labels

derive Related to the code generating

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants