From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25985 invoked by alias); 13 Apr 2015 15:11:01 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 25593 invoked by uid 89); 13 Apr 2015 15:11:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 13 Apr 2015 15:11:00 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 00C788F2E3; Mon, 13 Apr 2015 15:10:58 +0000 (UTC) Received: from localhost (ovpn-116-126.ams2.redhat.com [10.36.116.126]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3DFAvkc016212; Mon, 13 Apr 2015 11:10:58 -0400 Date: Mon, 13 Apr 2015 15:11:00 -0000 From: Jonathan Wakely To: Marc Glisse Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [libstdc++/61347] std::distance(list.first(),list.end()) in O(1) Message-ID: <20150413151057.GK9755@redhat.com> References: <20150413134025.GI9755@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-04/txt/msg00597.txt.bz2 On 13/04/15 16:14 +0200, Marc Glisse wrote: >On Mon, 13 Apr 2015, Jonathan Wakely wrote: > >>On 13/04/15 13:42 +0200, Marc Glisse wrote: >>>this patch makes std::distance(list.first(),list.end()) a constant >>>time operation when optimizing, with no penalty for other calls. >>>We could do the test always (no __builtin_constant_p) but then it >>>will add a small runtime penalty for other calls, someone else can >>>take responsibility for that. >> >>I like the way you've done it. No penalty for other calls is great >>and IMHO it's OK that the optimisation only happens when optimising. >> >>(Does it work even at -Og?) > >No, the testcase currently passes with -O1 but not -Og. OK, we can live with that. >>Sadly, that node is going to look even stranger when I finish adding >>support for C++11 allocators, as the type of node becomes dependent on >>the allocator's pointer, which makes _List_node much more >>complicated :-( > >But then I assume _List_node_base is the part really getting more >complicated, so without looking too closely it seems almost >orthogonal. In order to avoid making any changes to std::list> I'm adding an entire parallel hierarchy of a new node base type (parameterized on the allocator's void_pointer type), a new node type and new iterators (parameterized on the pointer type), which will only be used when !is_pointer. So it will just mean adding two new overloads for the two new iterator types. Not a big deal, as long as I remember to do it :-) >>This should still be a qualified call to std::__distance, otherwise the >>compiler might end up instantiating things to figure out if there are >>any associated namespaces, e.g. for vector>::iterator we >>don't want to know T's base classes and rheir associated namespaces. > >Then the approach will not work. C++ overload resolution will not >consider the later __distance declarations in stl_list.h if the call >is qualified (I didn't change it gratuitously). Ahhh, yes, of course. >A forward declaration >of list iterators and those __distance overloads in >bits/stl_iterator_base_funcs.h is not very appealing but may work (or >it may cause issues, I don't know). Otherwise, I guess we are back to >creating a new file bits/list_iterator.h, which includes if > has already been included and vice versa, and which >provides overloads for distance directly. I don't have a preference, but I think the forward declarations should work without problems. includes bits/stl_iterator_base_funcs.h so if the forward declarations didn't match the definitions for some reason we'd know right away.