Skip to content

Speed up iterator_buffer#4679

Open
user202729 wants to merge 1 commit intofmtlib:masterfrom
user202729:speed-up-iterator-buffer
Open

Speed up iterator_buffer#4679
user202729 wants to merge 1 commit 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 2 times, most recently from 9778e35 to 73b240b Compare February 23, 2026 07:12
@user202729 user202729 force-pushed the speed-up-iterator-buffer branch 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
void flush() {
auto size = this->size();
this->clear();
const T* begin = data_;
const T* end = begin + this->limit(size);
while (begin != end) *out_++ = *begin++;
#if defined(__cpp_if_constexpr)
if constexpr (std::is_move_assignable<OutputIt>::value)
out_ = copy<T>(begin, end, out_);
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 03ed496 to 2e1c57c Compare March 23, 2026 05:13
@user202729
Copy link
Contributor Author

user202729 commented Mar 23, 2026

I followed the suggestion, and sure enough, base-test can't link.

What then? Move the tests in base-test.cc that requires this method to format-test?

@vitaut
Copy link
Contributor

vitaut commented Mar 24, 2026

I see, the problems is that the definition of flush won't get instantiated with the output iterator type. I guess moving copy to base.h is indeed the simplest option, sorry for going back and forth on it.

@user202729 user202729 force-pushed the speed-up-iterator-buffer branch from 2e1c57c to 1a282f8 Compare March 24, 2026 03:50
@user202729
Copy link
Contributor Author

in retrospect, with #4716 , this wouldn't be too useful. (maybe std::deque, but for that one the append method is named append_range.) but anyway...?

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