Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid format! in the inner loop. #33

Closed
jonhoo opened this issue Feb 4, 2019 · 3 comments
Closed

Avoid format! in the inner loop. #33

jonhoo opened this issue Feb 4, 2019 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed info-needed Further information is requested

Comments

@jonhoo
Copy link
Owner

jonhoo commented Feb 4, 2019

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!

@jonhoo jonhoo added enhancement New feature or request help wanted Extra attention is needed info-needed Further information is requested labels Feb 4, 2019
@jonhoo
Copy link
Owner Author

jonhoo commented Feb 5, 2019

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 &strs 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.

@jonhoo
Copy link
Owner Author

jonhoo commented Feb 5, 2019

Just to double-check with @Stebalien that StrStack would actually work for this purpose.

@Stebalien
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed info-needed Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants