From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 73547 invoked by alias); 9 Aug 2016 17:34:35 -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 73526 invoked by uid 89); 9 Aug 2016 17:34:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=funny, sk:basic_s, flaw, sso-23 X-HELO: paperclip.tbsaunde.org Received: from tbsaunde.org (HELO paperclip.tbsaunde.org) (66.228.47.254) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 09 Aug 2016 17:34:22 +0000 Received: from ball (unknown [IPv6:2607:f0c8:8000:80e0:56ee:75ff:fe52:afb9]) by paperclip.tbsaunde.org (Postfix) with ESMTPSA id 12A25C11F; Tue, 9 Aug 2016 17:34:21 +0000 (UTC) Date: Tue, 09 Aug 2016 17:34:00 -0000 From: Trevor Saunders To: Oleg Endo Cc: Jeff Law , Richard Biener , Aldy Hernandez , Martin Sebor , gcc-patches Subject: Re: protected alloca class for malloc fallback Message-ID: <20160809174139.GA18239@ball> References: <57A3F57F.3050509@gmail.com> <57A4A5E8.90205@redhat.com> <1470420954.639.64.camel@t-online.de> <44EE0FB0-A8B9-43F9-BF58-C4D7D27DA944@gmail.com> <57A5B8BE.2000004@redhat.com> <941D179C-146F-4004-BECB-9FB066DDCC8D@gmail.com> <21bcbebe-28a8-58a7-68e8-af9abcb03dce@redhat.com> <20160808173939.GA13790@ball> <1470742429.639.196.camel@t-online.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1470742429.639.196.camel@t-online.de> User-Agent: Mutt/1.6.0 (2016-04-01) X-SW-Source: 2016-08/txt/msg00748.txt.bz2 On Tue, Aug 09, 2016 at 08:33:49PM +0900, Oleg Endo wrote: > On Mon, 2016-08-08 at 13:39 -0400, Trevor Saunders wrote: > > > > First sizeof std::string is 32 on x86_64, a char *, a size_t for the > > length, and a 16 byte union of a size_t for allocated size and a 16 > > byte buffer for short strings. I suppose some of this is required by > > the C++ standard, > > I recommend checking what others have figured regarding that matter > https://github.com/elliotgoodrich/SSO-23 I think one of the big mistakes in the C++ stl strings is that they try to shove stack allocated strings where you want an internal buffer for SSO and a class to put in structs into the same type. imho it would be better to have std::string for the second use case, and a separate std::stack_string or something for the first case. > > but I doubt we really need to care about strings longer than 2^32 in > > gcc. If we put the length and allocated size in the buffer, > > and have a separate class for stack allocated buffers I think we can > > have a string that is just sizeof void *. > > This idea has one flaw in that it does not allow having objects by > value. It essentially *requires* one to *always* allocate them on the > heap via new/delete. Being able to store objects by value is useful in > many situations. So if you do the above, the next logical step that > will follow is a smart-pointer-like wrapper that allows value > semantics. Because eventually somebody will want that operator == or > operator < e.g. for associative containers. If what you want is the ability to put the buffer on the stack instead of the heap then I think a stack_string class that interoperates with your string class is the thing you want. I don't really see anything wrong with a string class being a really fancy smart pointer that has a bunch of useful string stuff on it. As for operator == I'd be fairly ok with that, other than it hiding a O(N) operation in ==. > > > > Second it would be useful performance wise to have a std::string_view > > type class, but that is c++14 or 17? only so we'd need to import it > > into gcc or something. > > http://en.cppreference.com/w/cpp/experimental/basic_string_view > http://en.cppreference.com/w/cpp/string/basic_string_view > > It's "funny" that GCC contains a C++ stdlib implementation and so > little is actually used by GCC itself. Regretably necessary sure, but I'm not sure its funny. The first big problem with using the stl is that the subset available in C++98 isn't that great, you could maybe hack up libstdc++ so that you can use newer types just without the bits that use newer language features, but that would take some work. The other big problem is that the stl is often too general, and tries to be too simple. std::string is actually a good example of both of those problems. There's really no reason it should use size_t instead of uint32_t for the string length / capacity. It would also be a lot better if it had separate types for strings where you want an internal buffer or don't. Trev