From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id 1CC143858D37; Mon, 20 Nov 2023 15:44:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1CC143858D37 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=ucw.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kam.mff.cuni.cz ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1CC143858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.113.20.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700495081; cv=none; b=wHE5O3Fhdm0qIeL3c6/UcVgLWoENOdXa9Dy+zoFZKnCn/adz4lawqtA7QazCsbMrCxuWjhJIskoEpvLAC/i76C8WUenu6XZSizTA7SxosODj/WgffP8ee/bvfvw8Q1rHd0CBFKVu145MusMlAiQg9ixjixcgVGTfeNDaMHCCrZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700495081; c=relaxed/simple; bh=sDO1ffvPuv19n2sRfZQFsB72kTNPQfLne0JQk3nm2V0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=K398E81B/gR4UB/Ag1KMvLIEpm2E2ZmQn/nsbcOE0/04z4TXciQ+GRWo4Si6YGjd2awVjPOVBKno7cnE0pq5XhplufMSR/Uj2J0JUxX/zuoSzUvz9jlTNJ+I2kL9zdA28V1iomB7gO8eEU9K5NPEZnuu3lJ9HwDyDm0DS/7yfMA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id 41B8028B857; Mon, 20 Nov 2023 16:44:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ucw.cz; s=gen1; t=1700495079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TFgnYxVn6xg3yrvYO8pmBLDTmCSdXunp+QqCWu+OOx8=; b=C9+AzEUD/Sxa21rbTWS5VJZzWAsqj/XjBuDID9T5/OERmsqf+6kC1WCtxOlFC8YPJ+fGxf VGpeJ4OD559QUCygHLSza9DRBNsRL8nBiaqICQlAauJPVWXd9SGNYxKTg+T53mxJ7iIUq8 DIq2LJFhhv9bzxkSeFT+gufmV5dKCuo= Date: Mon, 20 Nov 2023 16:44:39 +0100 From: Jan Hubicka To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: libstdc++: Speed up push_back Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,JMQ_SPF_NEUTRAL,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > > + // RAII type to destroy initialized elements. > > There's only one initialized element, not "elements". > > > + struct _Guard_elts > > + { > > + pointer _M_first, _M_last; // Elements to destroy > > We only need to store one pointer here, call it _M_p. > > > + _Tp_alloc_type& _M_alloc; > > + > > + _GLIBCXX20_CONSTEXPR > > + _Guard_elts(pointer __elt, _Tp_alloc_type& __a) > > + : _M_first(__elt), _M_last(__elt + 1), _M_alloc(__a) > > + { } > > + > > + _GLIBCXX20_CONSTEXPR > > + ~_Guard_elts() > > + { std::_Destroy(_M_first, _M_last, _M_alloc); } > > This should be either: > > std::_Destroy(_M_p, _M_p+1, _M_alloc); > > or avoid the loop that happens in that _Destroy function: > > _Alloc_traits::destroy(_M_alloc, _M_p); > > > + > > + private: > > + _Guard_elts(const _Guard_elts&); > > + }; > > + > > + // Guard the new element so it will be destroyed if anything throws. > > + _Guard_elts __guard_elts(__new_start + __elems, _M_impl); > > + > > + __new_finish = std::__uninitialized_move_if_noexcept_a( > > + __old_start, __old_finish, > > + __new_start, _M_get_Tp_allocator()); > > + > > + ++__new_finish; > > + // Guard everything before the new element too. > > + __guard_elts._M_first = __new_start; > > This seems redundant, we're not doing any more insertions now, and so > this store is dead. I removed this one. > > > + > > + // New storage has been fully initialized, destroy the old elements. > > + __guard_elts._M_first = __old_start; > > + __guard_elts._M_last = __old_finish; However here I think I need __guard_elts supporting destruction of many elements in case the vector has moved to new location.... So I do not quite see how to simplify the code as suggested above except that the constructor can be simplified to not require first and last argument since we always initialize it for 1 destruction but later we may update it. Thanks, Honza