Skip to content

Speed up iterator_buffer#4679

Open
user202729 wants to merge 2 commits intofmtlib:masterfrom
user202729:speed-up-iterator-buffer
Open

Speed up iterator_buffer#4679
user202729 wants to merge 2 commits intofmtlib:masterfrom
user202729:speed-up-iterator-buffer

Conversation

@user202729
Copy link
Contributor

@user202729 user202729 commented Feb 23, 2026

Just a 1-line change to reuse the fmt::detail::copy() method instead of manually loop over each character. For that, some methods need to be moved around.

Demonstration of the speedup in fmtlib/format-benchmark#35 . Arguably vector<char> is a rare type, but it might be useful for other things (custom string types...?)

To make tests pass, I need to check whether the iterator type is move-assignable (throwing_iterator is not assignable), although I'd argue that in practice iterators ought to be move-assignable and that test is actually invalid.

@user202729 user202729 force-pushed the speed-up-iterator-buffer branch 3 times, most recently from 73b240b to 1a282f8 Compare February 23, 2026 07:14
Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

Comment on lines 1930 to 1941
else
#endif
while (begin != end) *out_++ = *begin++;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's move the definition to fmt/format.h instead of bringing all the copy machinery here.

@vitaut
Copy link
Contributor

vitaut commented Mar 14, 2026

@user202729 do you still plan to update this PR?

@user202729 user202729 force-pushed the speed-up-iterator-buffer branch from 1a282f8 to 15252c8 Compare March 15, 2026 02:41
@user202729
Copy link
Contributor Author

user202729 commented Mar 15, 2026

Updated.

That said, it leads to a bunch of other things such as format_to being moved to format.h, which is technically a breaking change since, for example, previously a program that only use base.h would be able to use iterator_buffer, now they can't. (See the failing tests changed in the latest commit. In particular, deleting the error base-test includes format.h is extraordinarily wrong. One option is to move them to format-test.cc.)

I can think of a few possibilities such as providing a faster overload in format.h, but that would probably be a ODR violation.

What do you think?

Side note, if it makes you feel better, you could just close the PR and comment like "if you want to work on this later you can reopen it", or make a [stale] label and set your pull requests page to by default filter them out. I appreciate that you are very active and keep the number of pending issues and pull requests extraordinarily low.

@user202729 user202729 force-pushed the speed-up-iterator-buffer branch from c3ef19e to 03ed496 Compare March 15, 2026 05:27

// A buffer that writes to an output iterator when flushed.
template <typename OutputIt, typename T, typename Traits = buffer_traits>
class iterator_buffer : public Traits, public buffer<T> {
Copy link
Contributor

@vitaut vitaut Mar 15, 2026

Choose a reason for hiding this comment

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

I was not suggesting to move iterator_buffer, just the implementation of the function you were modifying. Sorry if it wasn't clear enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you move only the implementation, but not the interface, won't anything that include base.h but not format.h, get linker error? Besides, there is a header-only compilation option.

You can't just put it in the cpp, since the function is templated (depends on the template parameter OutputIt).

Copy link
Contributor

Choose a reason for hiding this comment

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

No, only the consumers who need this will have to include format.h which is OK.

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