Avoid `format!` in the inner loop.
jonhoo opened this issue · comments
Currently, we use format!
in a number of places in order to pass appropriate values to BytesStart::with_attributes
. That's really sad, because it means we're allocating tons of little String
items. It'd be far better if we instead just had a single buffer that use write!()
to write into, and then pulled out slices of to get the attribute substrings. I don't know if such a crate exists, but if it doesn't, that seems like a useful thing to write!
Specifically, I think the trick will be to have a StrStack
allocated outside the main loop, and then each time before calling with_attributes
, write!
the attributes onto a StrStack::writer
, and pass the resulting &str
s to with_attributes
, before then clearing the StrStack
again. This should mean that, in general, we do not allocate anything for these attributes in the inner loop.
Just to double-check with @Stebalien that StrStack
would actually work for this purpose.
That sounds like it should work. StrStack implements IntoIterator<Item=&str>
(an iterator over the current strings on the stack) so you should be able to pass it into with_attributes
directly.
This is how I'm currently using it: https://github.com/Stebalien/gazetta/blob/b9bb26b8ebc633ab18445dd3f3cf0b4a9330f90f/core/src/render.rs#L177-L186