220 34975 <d41189b5-cf47-4ddc-bf3c-0274394015f2@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Mingxin Wang <wmx16835vv@163.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Idea about "std::pmr::memory_resource"
Date: Mon, 16 Oct 2017 20:35:55 -0700 (PDT)
Lines: 254
Approved: news@gmane.org
Message-ID: <d41189b5-cf47-4ddc-bf3c-0274394015f2@isocpp.org>
References: <0a8293b4-3246-47a5-9881-bc1ca4b91772@isocpp.org>
 <07ddb1f6-d3af-43df-bf96-8975c1ab4313@isocpp.org>
 <2e191a96-1952-479b-b78b-22e31f8e03de@isocpp.org>
 <0e37b5ae-64f6-4a8a-86bd-f44b119ed40a@isocpp.org>
 <15f8c5ba-3a6a-4441-9620-92e157c66094@isocpp.org>
 <26fe4cb4-98ca-41cc-ac96-5c51851799d1@isocpp.org>
 <fee0a310-5dc5-4421-bf49-bc8fa164fe29@isocpp.org>
 <784ffce8-ace7-449d-a13d-a99d7336117a@isocpp.org>
 <d029c18a-12ee-4a7d-828c-ab1bd07b58c6@isocpp.org>
 <60f71623-0a56-478e-8dcc-e76685fc8706@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_19046_446693016.1508211355794"
X-Trace: blaine.gmane.org 1508211363 31293 195.159.176.226 (17 Oct 2017 03:36:03 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 17 Oct 2017 03:36:03 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDNMBNHJWIGBBHHVSXHQKGQE574YUNY@isocpp.org Tue Oct 17 05:35:58 2017
Return-path: <std-proposals+bncBDNMBNHJWIGBBHHVSXHQKGQE574YUNY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ua0-f199.google.com ([209.85.217.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDNMBNHJWIGBBHHVSXHQKGQE574YUNY@isocpp.org>)
	id 1e4Ifb-0006W8-T1
	for gclcip-std-proposals@m.gmane.org; Tue, 17 Oct 2017 05:35:56 +0200
Original-Received: by mail-ua0-f199.google.com with SMTP id h34sf236475uaa.8
        for <gclcip-std-proposals@m.gmane.org>; Mon, 16 Oct 2017 20:36:03 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=+JY7cGy/GmiHrnyEzW/sXBwmFM8G0Rqd5QvpXz3wd8w=;
        b=cv3hTxVRwuj5IDnJHq9Vpu7HCtpXf6Ygn77RUekELBGbwmEXAhWpzsRua5D13+8Cl2
         fv20QpFyeHpd4MiV3ix2pIXZwN6RBCmPi3xBPLV21bGiyaFsIKY1MtA3z/6IYPWCR3ew
         nZtT6dimOruRzqKMvJNiTFKZfBY0NRigiHMjWjQgWr6NsBWu7R8tkfdWfiW8AWJAeUge
         37RoT2OGqiIx7IbslQCuaac7V9GzShkOGFdt6UAM3yjoXK1J79De6du+t9jSOd4i3XMy
         iwaPyt9g04I2O5rC/O3y6kSvGLIFwA73TpUCE1Ti6azezWYsp0DtUFWLt/SanyFlUoMX
         GKgQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=+JY7cGy/GmiHrnyEzW/sXBwmFM8G0Rqd5QvpXz3wd8w=;
        b=sn2ghKHGE03E7U3Jky7KruLw0wJwYKHlYSCEHr04TYYUWiIr86F1Qad4qQ7IOH5vtl
         LpuyovivXHECV9n+X4YqQRCFGBhRwyRWjgcn87kAbwIeF6AoPJGJoCzf+HRiFPEklY45
         kjkpj98Z2FHM7/HF14wTUvFFa+3I4RCrIo0RZ7k7iobncV+kqmWOo4bhp9AjYRt7xAbt
         qTVkj7UlLiCGBlCKtS0b4VzEjo5I0HmQooTyX7GtdNj5E4FPNPP5owtZ/eclDp3vHCzj
         cIFZuFKo3Y87+nTDi9XkPihkvgnR2/XCWJBQV2zTJV6EyfHpz9MLXdArCf3gyhzare4f
         wBoA==
X-Gm-Message-State: AMCzsaXaOk8gCmiMF3ZqUIjpQxEq+MEVabHzwiK03BMNJzSChnHDqjO0
	RvOaPTHxmkNuy+PS6cCuqPb3Eg==
X-Google-Smtp-Source: AOwi7QAeZGLaCbrZVnX9SN/DxJDgvcPP949ZQd3LZPW2agnQCNV6qyHJQnDaYbZlmqvUQe1vKDtIhA==
X-Received: by 10.159.59.10 with SMTP id i10mr6254467uah.61.1508211358018;
        Mon, 16 Oct 2017 20:35:58 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.31.56.21 with SMTP id f21ls31610vka.1.gmail; Mon, 16 Oct 2017
 20:35:56 -0700 (PDT)
X-Received: by 10.31.137.138 with SMTP id l132mr542482vkd.14.1508211356341;
        Mon, 16 Oct 2017 20:35:56 -0700 (PDT)
In-Reply-To: <60f71623-0a56-478e-8dcc-e76685fc8706@isocpp.org>
X-Original-Sender: wmx16835vv@163.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:34975
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/34975>

------=_Part_19046_446693016.1508211355794
Content-Type: multipart/alternative; 
	boundary="----=_Part_19047_379779897.1508211355794"

------=_Part_19047_379779897.1508211355794
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Tuesday, October 17, 2017 at 4:44:16 AM UTC+8, Nicol Bolas wrote:
>
> On Monday, October 16, 2017 at 6:26:09 AM UTC-4, Mingxin Wang wrote:
>>
>> Adding a "MemoryResource" type to the template of a polymorphic facility=
=20
>> is positive for performance in large-scale programming.
>>
>
> Where is the "large-scale programming" that needs "large-scale" quantitie=
s=20
> of `any` and/or `function` or similar type-erased types? Generally=20
> speaking, such tools are not used in high-performance code, not so much=
=20
> because of allocation behavior, but because they require type-erasure and=
=20
> therefore are more expensive than static polymorphism.
>

I disagree with that. I think polymorphic requirements are more likely to=
=20
appear in large-scale program, because independent modules are difficult to=
=20
be implemented with templates, and therefore they tend to accept=20
polymorphic input. For example, the "threadpool" is a common facility in=20
large-scale programming, and it seems that no one could implement such=20
facility without polymorphism (more specifically, function pointers).

Actually, allocation is much more expensive than polymorphism in various=20
implementations for `std::any`. In order to prove that, I have run a couple=
=20
of performance tests for the "template constructor" (that is, construct=20
from an arbitrary type) and "copy constructor" of `std::any` with empty=20
trivial types whose size range from 1 to 64.

Here is the environment information:

CPU: Intel=C2=AE core(TM) i7-4700HQ @ 2.40GHz
Memory: 12GB
OS: CentOS 7.2
Compiler: gcc 7.1.0 x64 posix
Compiler Flags: -Wall -fexceptions -march=3Dcorei7-avx =E2=80=93O3 -m64 -fc=
oncepts=20
-std=3Dc++17

And the experimental results are as follows:

<https://lh3.googleusercontent.com/-iu6cGoFY18M/WeVy6znX6SI/AAAAAAAAAGE/FGM=
x0SBzoecoVCV4mtsoD5XVYHzdpJRLgCLcBGAs/s1600/cc.png>

<https://lh3.googleusercontent.com/-CfcykQv412s/WeVzBFnxI0I/AAAAAAAAAGI/VSZ=
N9vp9iJ4pugbEUSxs0f1wVP_5QJcxQCLcBGAs/s1600/tc.png>

It is apparent that the execution time has increased significantly when the=
=20
size of the test class changes from 8 to 9. It is because that the size for=
=20
"Small Object Optimization" is 8 (defined by the implementation of=20
`std::any`). When the size becomes larger, there are memory allocations,=20
and allocations consume much more time than polymorphism.

We could use it in allocators and polymorphic facilities, and we are able=
=20
>> to configure whether the memory resource should be polymorphic.
>>
>
> Which we can already do. `pmr::memory_resource` is a polymorphic type, so=
 *by=20
> definition* it can be used in "polymorphic facilities". And you can very=
=20
> much build allocators out of them; that's what a=20
> `pmr::polymorphic_allocator<T>` *is*.
>
> If you wanted, you could make a `pmr::static_allocator<T, MemoryResource>=
`=20
> template that takes its `pmr::memory_resource`-derived class by type. As=
=20
> such, it can de-virtualize any calls to the allocation/deallocation=20
> functions. Thus, it would be essentially zero overhead.
>

However, this compiler optimization does not hold under all circumstances,=
=20
especially when there are classes derived from a=20
"`pmr::memory_resource`-derived class", unless it is explicitly declared=20
"final".

Besides, a "`pmr::memory_resource`-derived class" is certainly not empty,=
=20
even if it is stateless; so that `pmr::static_allocator<T,=20
`pmr::memory_resource`-derived>` could not be empty either. Even if the=20
compiler could deduce that a "`pmr::memory_resource`-derived class" is=20
accessible without any dispatcher, there is no chance to optimize the size.
=20

> So what *exactly* can you not do currently that you want to do?
>
> If there is no particular demand in "std::pmr::memory_resource", I have n=
o=20
>> objection to removing it, because it is easily implementable with "the=
=20
>> proxy=20
>> <https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/lfAr=
1ef22YM>"=20
>> (I am still working on that proposal).
>>
>
> But we do have a "particular demand" for it; that's why it was=20
> standardized. If there was no demand, we wouldn't have standardized it.
>

OK, that's no big deal, because it is easily implementable with "the proxy=
=20
<https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/lfAr1ef=
22YM>".=20
I will add it into that proposal as a use case.

Mingxin Wang

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/d41189b5-cf47-4ddc-bf3c-0274394015f2%40isocpp.or=
g.

------=_Part_19047_379779897.1508211355794
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, October 17, 2017 at 4:44:16 AM UTC+8, Nicol Bo=
las wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:=
 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">On =
Monday, October 16, 2017 at 6:26:09 AM UTC-4, Mingxin Wang wrote:<blockquot=
e class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px=
 #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Adding a &quot;MemoryRe=
source&quot; type to the template of a polymorphic facility is positive for=
 performance in large-scale programming.</div></div></blockquote><div><br><=
/div><div>Where is the &quot;large-scale programming&quot; that needs &quot=
;large-scale&quot; quantities of `any` and/or `function` or similar type-er=
ased types? Generally speaking, such tools are not used in high-performance=
 code, not so much because of allocation behavior, but because they require=
 type-erasure and therefore are more expensive than static polymorphism.</d=
iv></div></blockquote><div><br></div><div>I disagree with that. I think pol=
ymorphic requirements are more likely to appear in large-scale program, bec=
ause independent modules are difficult to be implemented with templates, an=
d therefore they tend to accept polymorphic input. For example, the &quot;t=
hreadpool&quot; is a common facility in large-scale programming, and it see=
ms that no one could implement such facility without polymorphism (more spe=
cifically, function pointers).</div><div><br></div><div>Actually, allocatio=
n is much more expensive than polymorphism in various implementations for `=
std::any`. In order to prove that, I have run a couple of performance tests=
 for the &quot;template constructor&quot; (that is, construct from an arbit=
rary type) and &quot;copy constructor&quot; of `std::any` with empty trivia=
l types whose size=C2=A0range from 1 to 64.</div><div><br></div><div>Here i=
s the=C2=A0environment information:</div><div><br></div><div><div>CPU: Inte=
l=C2=AE core(TM) i7-4700HQ @ 2.40GHz</div><div>Memory: 12GB</div><div>OS: C=
entOS 7.2</div><div>Compiler: gcc 7.1.0 x64 posix</div><div>Compiler Flags:=
 -Wall -fexceptions -march=3Dcorei7-avx =E2=80=93O3 -m64 -fconcepts -std=3D=
c++17</div></div><div><br></div><div>And the=C2=A0experimental results are =
as follows:</div><div><br></div><p class=3D"separator" style=3D"text-align:=
 center; clear: both;"><a imageanchor=3D"1" href=3D"https://lh3.googleuserc=
ontent.com/-iu6cGoFY18M/WeVy6znX6SI/AAAAAAAAAGE/FGMx0SBzoecoVCV4mtsoD5XVYHz=
dpJRLgCLcBGAs/s1600/cc.png" style=3D"margin-left: 1em; margin-right: 1em;">=
<img src=3D"https://lh3.googleusercontent.com/-iu6cGoFY18M/WeVy6znX6SI/AAAA=
AAAAAGE/FGMx0SBzoecoVCV4mtsoD5XVYHzdpJRLgCLcBGAs/s320/cc.png" border=3D"0" =
width=3D"320" height=3D"213"></a></p><div><br></div><p class=3D"separator" =
style=3D"text-align: center; clear: both;"><a imageanchor=3D"1" href=3D"htt=
ps://lh3.googleusercontent.com/-CfcykQv412s/WeVzBFnxI0I/AAAAAAAAAGI/VSZN9vp=
9iJ4pugbEUSxs0f1wVP_5QJcxQCLcBGAs/s1600/tc.png" style=3D"margin-left: 1em; =
margin-right: 1em;"><img src=3D"https://lh3.googleusercontent.com/-CfcykQv4=
12s/WeVzBFnxI0I/AAAAAAAAAGI/VSZN9vp9iJ4pugbEUSxs0f1wVP_5QJcxQCLcBGAs/s320/t=
c.png" border=3D"0" width=3D"320" height=3D"213"></a></p><div><br></div><di=
v>It is apparent that the execution time has increased significantly when t=
he size of the test class changes from 8 to 9. It is because that the size =
for &quot;Small Object Optimization&quot; is 8 (defined by the implementati=
on of `std::any`). When the size becomes larger, there are memory allocatio=
ns, and allocations consume much more time than polymorphism.</div><div><br=
></div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.=
8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><block=
quote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left=
:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>We could use it in =
allocators and polymorphic facilities, and we are able to configure whether=
 the memory resource should be polymorphic.<br></div></div></blockquote><di=
v><br></div><div>Which we can already do. `pmr::memory_resource` is a polym=
orphic type, so <i>by definition</i> it can be used in &quot;polymorphic fa=
cilities&quot;. And you can very much build allocators out of them; that&#3=
9;s what a `pmr::polymorphic_allocator&lt;T&gt;<wbr>` <b>is</b>.</div><div>=
<br></div><div>If you wanted, you could make a `pmr::static_allocator&lt;T,=
 MemoryResource&gt;` template that takes its `pmr::memory_resource`-derived=
 class by type. As such, it can de-virtualize any calls to the allocation/d=
eallocation functions. Thus, it would be essentially zero overhead.</div></=
div></blockquote><div><br></div><div>However, this compiler optimization do=
es not hold under all circumstances, especially when there are classes deri=
ved from a &quot;`pmr::memory_resource`-derived class&quot;, unless it is e=
xplicitly declared &quot;final&quot;.</div><div><br></div><div>Besides, a &=
quot;`pmr::memory_resource`-derived class&quot; is certainly not empty, eve=
n if it is stateless; so that `pmr::static_allocator&lt;T, `pmr::memory_res=
ource`-derived&gt;` could not be empty either. Even if the compiler could d=
educe that a &quot;`pmr::memory_resource`-derived class&quot; is accessible=
 without any dispatcher, there is no chance to optimize the size.</div><div=
>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">=
<div>So what <i>exactly</i> can you not do currently that you want to do?<b=
r></div><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0;=
margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"=
ltr"><div></div><div></div><div>If there is no particular demand in &quot;s=
td::pmr::memory_resource&quot;, I have no objection to removing it, because=
 it is easily implementable with &quot;<a href=3D"https://groups.google.com=
/a/isocpp.org/forum/#!topic/std-proposals/lfAr1ef22YM" rel=3D"nofollow" tar=
get=3D"_blank" onmousedown=3D"this.href=3D&#39;https://groups.google.com/a/=
isocpp.org/forum/#!topic/std-proposals/lfAr1ef22YM&#39;;return true;" oncli=
ck=3D"this.href=3D&#39;https://groups.google.com/a/isocpp.org/forum/#!topic=
/std-proposals/lfAr1ef22YM&#39;;return true;">the proxy</a>&quot; (I am sti=
ll working on that proposal).</div></div></blockquote><div><br></div><div>B=
ut we do have a &quot;particular demand&quot; for it; that&#39;s why it was=
 standardized. If there was no demand, we wouldn&#39;t have standardized it=
..<br></div></div></blockquote><div><br></div><div>OK, that&#39;s no big dea=
l, because it is easily implementable with &quot;<a href=3D"https://groups.=
google.com/a/isocpp.org/forum/#!topic/std-proposals/lfAr1ef22YM" rel=3D"nof=
ollow" target=3D"_blank">the proxy</a>&quot;. I will add it into that propo=
sal as a use case.</div><div><br></div><div>Mingxin Wang</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/d41189b5-cf47-4ddc-bf3c-0274394015f2%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/d41189b5-cf47-4ddc-bf3c-0274394015f2=
%40isocpp.org</a>.<br />

------=_Part_19047_379779897.1508211355794--

------=_Part_19046_446693016.1508211355794--

.
