From patchwork Fri Nov 17 15:53:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 80139 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8605C3857346 for ; Fri, 17 Nov 2023 15:54:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5E45E3858C20 for ; Fri, 17 Nov 2023 15:54:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E45E3858C20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E45E3858C20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700236467; cv=none; b=g5bcrBoQNrOF6ZR5Q4NaqBZHa8fsIS3I/xb3O+ZDZLDvRJ3+4MH5Et8rzSc+j37NEfzcfwvpFwv2dgxve+6qs6GP/DcW0FW+7SM1hOR7JOcFbK9Os6khWE4EKLRnltp/EgrQCrreWBDnt28pcD69pokpc2LljXrGT3+dSrGzlT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700236467; c=relaxed/simple; bh=waoij+pf4EsXj6b9yYDk8gjJf8l8l5kg7EI+QERzqQ4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ojC7m+p5GpLjabNCLGOZcayrR/8FnMS2OA4/kkDtLALrghISviakEOzNpLQOPdt/fH0Zwx78AS8roIIwGuU2WFkqnW313YZuvJNsEsAhbEB6NLwf+jUDOmFEE4d2uxBWrlNGATvr0N5imdQcTvKEX9dryPo9AC2XBqT/B++MU9Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700236465; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FXgRx9snQabhy36AGQW3jpx/H+pgnsYrknqYYaSYir4=; b=OCZh3U401pe/lU2UWMaUM+7+sjid4zCS6BLPvo7R1372263rFbn8oIuuAT/Z5e6YiHfl0s k60w7H4/lCZRPhxo2T/TF1QMJ6QApAUZKUxXDPShw2/ME/f8Yd9uWjVlpCEsQ7sWVJjwX1 P8m97D4DferQSNyTY1l/8JN5iCrRef0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-530-C7ppQNY7NYyKj0Wr8KwpTQ-1; Fri, 17 Nov 2023 10:54:21 -0500 X-MC-Unique: C7ppQNY7NYyKj0Wr8KwpTQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9FB1685A5B5; Fri, 17 Nov 2023 15:54:21 +0000 (UTC) Received: from localhost (unknown [10.42.28.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68EEA1C060AE; Fri, 17 Nov 2023 15:54:21 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Add fast path for std::format("{}", x) [PR110801] Date: Fri, 17 Nov 2023 15:53:15 +0000 Message-ID: <20231117155420.1513704-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org I'll probably push this before stage 1 closes. I might move the new lambda out to a struct at namespace scope first though. -- >8 -- libstdc++-v3/ChangeLog: PR libstdc++/110801 * include/std/format (_Sink_iter::_M_get_pointer) (_Sink_iter::_M_end_pointer): New functions (_Sink::_M_get_pointer, _Sink::_M_end_pointer): New virtual functions. (_Seq_sink::_M_get_pointer, _Seq_sink::_M_end_pointer): New functions. (_Iter_sink::_M_get_pointer): Likewise. (__do_vformat_to): Use new functions to optimize "{}" case. --- libstdc++-v3/include/std/format | 155 +++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 8ec1c8a0b9a..3a9c64e4ab9 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -2442,6 +2442,10 @@ namespace __format iter_difference_t<_Out> size; }; +_GLIBCXX_BEGIN_NAMESPACE_CONTAINER +template class vector; +_GLIBCXX_END_NAMESPACE_CONTAINER + /// @cond undocumented namespace __format { @@ -2492,6 +2496,14 @@ namespace __format [[__gnu__::__always_inline__]] constexpr _Sink_iter operator++(int) { return *this; } + + _CharT* + _M_get_pointer(size_t __n) const + { return _M_sink->_M_get_pointer(__n); } + + void + _M_end_pointer(size_t __n) const + { _M_sink->_M_end_pointer(__n); } }; // Abstract base class for type-erased character sinks. @@ -2508,6 +2520,7 @@ namespace __format // Called when the span is full, to make more space available. // Precondition: _M_next != _M_span.begin() // Postcondition: _M_next != _M_span.end() + // TODO: remove the precondition? could make overflow handle it. virtual void _M_overflow() = 0; protected: @@ -2572,6 +2585,32 @@ namespace __format } } + // If this returns a non-null pointer it can be used to write directly + // up to N characters to the sink to avoid unwanted buffering. + // If anything is written to the buffer then there must be a call to + // _M_end_pointer(n2) before any call to another member function of + // this object, where N2 is the number of characters written. + // TODO: rewrite this direct access as an RAII type that exposes a span. + virtual _CharT* + _M_get_pointer(size_t __n) + { + auto __avail = _M_unused(); + if (__n <= __avail.size()) + return __avail.data(); + + if (__n > _M_span.size()) // Cannot meet the request. + return nullptr; + + _M_overflow(); // Make more space available. + __avail = _M_unused(); + return __n <= __avail.size() ? __avail.data() : nullptr; + } + + // pre: no calls to _M_write or _M_overflow since _M_get_pointer. + virtual void + _M_end_pointer(size_t __n) + { _M_next += __n; } + public: _Sink(const _Sink&) = delete; _Sink& operator=(const _Sink&) = delete; @@ -2596,6 +2635,8 @@ namespace __format { } }; + using _GLIBCXX_STD_C::vector; + // A sink that fills a sequence (e.g. std::string, std::vector, std::deque). // Writes to a buffer then appends that to the sequence when it fills up. template @@ -2619,6 +2660,46 @@ namespace __format this->_M_rewind(); } + _CharT* + _M_get_pointer(size_t __n) override + { + if constexpr (__is_specialization_of<_Seq, basic_string> + || __is_specialization_of<_Seq, vector>) + { + // Flush the buffer to _M_seq first: + if (this->_M_used().size()) + _M_overflow(); + // Expand _M_seq to make __n new characters available: + const auto __sz = _M_seq.size(); + if constexpr (is_same_v || is_same_v) + _M_seq.__resize_and_overwrite(__sz + __n, + [](auto, auto __n2) { + return __n2; + }); + else + _M_seq.resize(__sz + __n); + // Set _M_used() to be a span over the original part of _M_seq: + this->_M_reset(_M_seq, __sz); + // And return a pointer to the new portion: + return this->_M_unused().data(); + } + else // Try to use the base class' buffer. + return _Sink<_CharT>::_M_get_pointer(); + } + + void + _M_end_pointer(size_t __n) override + { + if constexpr (__is_specialization_of<_Seq, basic_string> + || __is_specialization_of<_Seq, vector>) + { + // Truncate the sequence to the part that was actually written to: + _M_seq.resize(this->_M_used().size() + __n); + // Switch back to using buffer: + this->_M_reset(this->_M_buf); + } + } + public: // TODO: for SSO string, use SSO buffer as initial span, then switch // to _M_buf if it overflows? Or even do that for all unused capacity? @@ -2744,6 +2825,21 @@ namespace __format } } + _CharT* + _M_get_pointer(size_t __n) final + { + auto __avail = this->_M_unused(); + if (__n > __avail.size()) + { + if (_M_max >= 0) + return nullptr; // cannot grow + + auto __s = this->_M_used(); + this->_M_reset({__s.data(), __s.size() + __n}, __s.size()); + } + return __avail.data(); + } + private: static span<_CharT> _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n, @@ -2773,7 +2869,7 @@ namespace __format uint64_t __off = reinterpret_cast(__ptr) % 1024; __n = (1024 - __off) / sizeof(_CharT); if (__n > 0) [[likely]] - return {__ptr, static_cast(__n)}; + return {__ptr, static_cast(__n)}; else // Misaligned/packed buffer of wchar_t? return {__ptr, 1}; } @@ -3835,6 +3931,63 @@ namespace __format else __sink_out = __sink.out(); + if constexpr (is_same_v<_CharT, char>) + if (__fmt.size() == 2 && __fmt[0] == '{' && __fmt[1] == '}') + { + bool __done = false; + std::visit_format_arg([&](auto& __arg) { + using _Tp = remove_cvref_t; + if constexpr (is_same_v<_Tp, bool>) + { + if (auto __ptr = __sink_out._M_get_pointer(4 + !__arg)) + { + if (__arg) + __builtin_memcpy(__ptr, "true", 4); + else + __builtin_memcpy(__ptr, "false", 5); + __sink_out._M_end_pointer(4 + !__arg); + __done = true; + } + } + else if constexpr (is_integral_v<_Tp>) + { + make_unsigned_t<_Tp> __uval; + const bool __neg = __arg < 0; + if (__neg) + __uval = make_unsigned_t<_Tp>(~__arg) + 1u; + else + __uval = __arg; + const auto __n = __detail::__to_chars_len(__uval) + __neg; + if (auto __ptr = __sink_out._M_get_pointer(__n)) + { + *__ptr = '-'; + __detail::__to_chars_10_impl(__ptr + (int)__neg, __n, + __uval); + __sink_out._M_end_pointer(__n); + __done = true; + } + } + else if constexpr (is_convertible_v<_Tp, string_view>) + { + string_view __sv = __arg; + if (auto __ptr = __sink_out._M_get_pointer(__sv.size())) + { + __builtin_memcpy(__ptr, __sv.data(), __sv.size()); + __sink_out._M_end_pointer(__sv.size()); + __done = true; + } + } + }, __args.get(0)); + + if (__done) + { + if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>) + return __sink_out; + else + return std::move(__sink)._M_finish().out; + } + } + auto __ctx = __loc == nullptr ? _Context(__args, __sink_out) : _Context(__args, __sink_out, *__loc);