From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id WnPBHZWylmEZVQAAWB0awg (envelope-from ) for ; Thu, 18 Nov 2021 15:07:49 -0500 Received: by simark.ca (Postfix, from userid 112) id 658C91F0BF; Thu, 18 Nov 2021 15:07:49 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, NICE_REPLY_A,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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 simark.ca (Postfix) with ESMTPS id E5FB41E813 for ; Thu, 18 Nov 2021 15:07:48 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 664CD385480B for ; Thu, 18 Nov 2021 20:07:48 +0000 (GMT) Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 1534C3858422 for ; Thu, 18 Nov 2021 20:07:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1534C3858422 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [172.16.0.95] (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 8270A1E813; Thu, 18 Nov 2021 15:07:36 -0500 (EST) Subject: Re: [PATCH 2/4] gdbsupport: add array_view copy function To: Tom Tromey , Simon Marchi References: <20211108210609.353208-1-simon.marchi@efficios.com> <20211108210609.353208-2-simon.marchi@efficios.com> <87h7cbesar.fsf@tromey.com> <90ddca61-ea64-b354-6fb4-99ab607a43d6@efficios.com> <87tugbzl7y.fsf@tromey.com> From: Simon Marchi Message-ID: Date: Thu, 18 Nov 2021 15:07:35 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <87tugbzl7y.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Language: tl Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Simon Marchi via Gdb-patches Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" On 2021-11-16 7:20 p.m., Tom Tromey wrote: >>> In C++20 I think we could use std::copy by having the array view >>> iterators implement the contiguous_iterator concept. So, maybe a >>> comment to this effect would be good. > > Simon> Do you mean we could use std::copy to implement this copy function, > Simon> instead of using memcpy, and that would deal with the details of copying > Simon> in the most efficient manner? > > Yeah, but IIUC it will only use memcpy or whatever if it sees the > contiguous_iterator concept (and if the content type is trivially > copyable). I'm not sure if there's a way to get this behavior before > C++20 though. > > Tom > I did some tests (with g++ 11, if that matters), compiled GDB with -std=c++11, and it seems like std::copy does the right thing. When copying a trivial type (array_view), it ends up using this __builtin_memmove: https://gitlab.com/gnutools/gcc/-/blob/6f4ac4f81f89caac7e74127ed2e6db6bbb3d7426/libstdc++-v3/include/bits/stl_algobase.h#L414-434 When copying a type that has a user-defined copy assignment operator, it ends up using this manual loop: https://gitlab.com/gnutools/gcc/-/blob/6f4ac4f81f89caac7e74127ed2e6db6bbb3d7426/libstdc++-v3/include/bits/stl_algobase.h#L394-412 That seems to be exactly what we want. Also, reading about std::copy made me realize that we would have to make sure we handle overlapping ranges correctly. Using std::copy or std::copy_backwards (depending on whether the destination comes before the source) handles that. I will post an updated series. Simon