From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 20A32396ECB4 for ; Tue, 17 Aug 2021 13:37:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 20A32396ECB4 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-368-98AkuEl9O7Sc1GqMUnPJLQ-1; Tue, 17 Aug 2021 09:37:11 -0400 X-MC-Unique: 98AkuEl9O7Sc1GqMUnPJLQ-1 Received: by mail-wr1-f70.google.com with SMTP id t15-20020a5d42cf000000b001565f9c9ee8so4433378wrr.2 for ; Tue, 17 Aug 2021 06:37:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uQsMEB07xCFtIA7VpsXSwQcfIHGLgb1hvjPALyn3bIg=; b=Hy48DBWKZ0Klr1Uow3cvPnr/cm3ZYbyOLQfaa8LV+Ht4Btnf6MFBA+0YuVMWA8Amht sRpNggYFnX8lxi3EpBkPdr1tzjNXCiMeFpMPgArbQ6aN7vA2u1QBMkgXDPXqvXJ4Wsro NcIymcHxFDAQdfDXCdM0/hJ6BEoYE+R2iBY5sehso2sI+FJKNW/1Czu7j6dwEgQUQnOj t31r9JtGOvFtQ1rTKhnP05rT/bFb6AQSOcfsXVlvuUvgFE3I4/g/ak0R5I4LJRqWdB7n GDJ7vYYmKDt2hvR3FCmio7M/J71vy4vLcxsAuPCNMBoHynZx0et17XAJR0iLhVPdb6k7 EfQg== X-Gm-Message-State: AOAM530y99RXvnMxftoeRxfhnKNMfvw7ChulfyEcRLI9aOLQTO2qNHdm TGq+OjWb0cJiHPaotVJGjhK7xFCVzu9Met5pYTYvfQf1WFzgbu9hrdiNRTh50PqlFXUPV/aawBD LzrO3LgRA+fiYzpsZT46bJE5QiZ41ZYY= X-Received: by 2002:a05:600c:3656:: with SMTP id y22mr3354251wmq.58.1629207430264; Tue, 17 Aug 2021 06:37:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3AVn/UYRj6rG+e8GM/8BeVmEM79XEdDQtSovkG1ANUx8/NbwXQ88q2En1fzniWbnNn9+WhJKmzuGEqVoHGbM= X-Received: by 2002:a05:600c:3656:: with SMTP id y22mr3354231wmq.58.1629207430107; Tue, 17 Aug 2021 06:37:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Tue, 17 Aug 2021 14:36:58 +0100 Message-ID: Subject: Re: [PATCH] Optimize seed_seq construction To: Antony Polukhin Cc: "libstdc++" , gcc-patches List X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Aug 2021 13:37:14 -0000 On Tue, 17 Aug 2021 at 09:42, Antony Polukhin wrote: > > When std::seed_seq is constructed from random access iterators we can > detect the internal vector size in O(1). Reserving memory for elements > in such cases may avoid multiple memory allocations. > > libstdc++-v3/ChangeLog: > > * include/bits/random.tcc: Optimize seed_seq construction. Thanks, this is a nice improvement. We can avoid tag dispatching to make it simpler though: @@ -3248,6 +3249,9 @@ namespace __detail template seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end) { + if _GLIBCXX17_CONSTEXPR (__is_random_access_iter<_InputIterator>::value) + _M_v.reserve(std::distance(__begin, __end)); + for (_InputIterator __iter = __begin; __iter != __end; ++__iter) _M_v.push_back(__detail::__mod::__value>(*__iter)); The call to std::distance is well-formed for input iterators, but we won't actually call it unless we have random access iterators. Unless you see a problem with this that I'm missing, I'll go with that version.