September 17, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to Szymon Gatner | On 9/17/14, 2:01 AM, Szymon Gatner wrote:
> std::string returned from get_url() is a temporary and hence a "rvalue".
> In fact it's address cannot be taken.
It can in C++, after bound (implicitly!) to a const &. "That was the joke." That's why I'm opposed to adding the same conversion to D without restriction. -- Andrei
|
September 17, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | On 9/17/14, 3:03 AM, IgorStepanov wrote: > BTW. About r-values: > > void fun(S s) > { > fun2(s); //pass s by value. > } > > Now, compiler inserts postblit call before func2 call and dtor before > end of fun. Is the call to dtor part of fun(), or part of fun()'s call sequence? I've always meant to look at that. LMK if you know for sure. > However, it is able to doesn't it, because after fun2 call, > s isn't used. > Thus, we can implement last postblit optimization: > If compiler want to insert postblit, and object is't used after this > postblit, compiler is able to doesn't generate postblit and last dtor > calls. > Is there limitation of this optimization? > It may 90 percent to solve a r-value ref task. I think rvalues are already not postblitted into functions. Indeed more postblits can be optimized away for variables are are last used in a function call. Andrei |
September 17, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Wednesday, 17 September 2014 at 15:03:12 UTC, Andrei Alexandrescu wrote: > On 9/17/14, 3:03 AM, IgorStepanov wrote: >> BTW. About r-values: >> >> void fun(S s) >> { >> fun2(s); //pass s by value. >> } >> >> Now, compiler inserts postblit call before func2 call and dtor before >> end of fun. > > Is the call to dtor part of fun(), or part of fun()'s call sequence? I've always meant to look at that. LMK if you know for sure. Dtor call is a part of fun(). I've written test code... struct Foo { this(int i) { } this(this) { } ~this() { } } void calledFunc(Foo probe) { } void main() { calledFunc(Foo(42)); } ... add a trace output into Statement_toIR::visit(ExpStatement *s) ... printf("ExpStatement::toIR(), exp = %s in %s\n", s->exp ? s->exp->toChars() : "", irs->symbol ? irs->symbol->toChars() : "NULL"); ... and got: ExpStatement::toIR(), exp = probe.~this() in calledFunc >> However, it is able to doesn't it, because after fun2 call, >> s isn't used. >> Thus, we can implement last postblit optimization: >> If compiler want to insert postblit, and object is't used after this >> postblit, compiler is able to doesn't generate postblit and last dtor >> calls. >> Is there limitation of this optimization? >> It may 90 percent to solve a r-value ref task. > > I think rvalues are already not postblitted into functions. Indeed more postblits can be optimized away for variables are are last used in a function call. > > > Andrei Yes, rvalues aren't posblitted. However I want and I can't deliver without postblits rvalue from first call (of constructor or factory function) through intermediate calls to the final place of stay. struct AA(Key, Value) { this(T...)(T args) { buckets.length = T.length; foreach (i; Step2Tuple!(T.length)) { alias key = args[i]; alias value = args[i+1]; size_t key_hash = hashOf(key); size_t idx = key_hash % T.length; auto e = new Entry(key_hash, key, value, impl.buckets[idx]); buckets[idx] = e; } } ... private static struct Entry { size_t hash; Key key; Value value; Entry* next; } Entry*[] buckets; } AA!(Key, Value) aaLiteral(Key, Value, T...)(auto ref T args) { return AA!(Key, Value)(args); } .... //somewhere in user code: auto aa = aaLiteral!(Foo, int)(Foo(1), 1, Foo(2), 2); //postblits aren't called. I want to place Foo(1) to buckets[nn].key without postblit call. Compiler can't help me now, however, I think, It can do it without language change. |
September 18, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to IgorStepanov | On 9/17/14, 12:28 PM, IgorStepanov wrote:
> I want to place Foo(1) to buckets[nn].key without postblit call.
> Compiler can't help me now, however, I think, It can do it without
> language change.
File an enhancement request with explanations and sample code, the works. This will be good. Thanks! -- Andrei
|
September 18, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Thursday, 18 September 2014 at 00:53:40 UTC, Andrei Alexandrescu wrote: > On 9/17/14, 12:28 PM, IgorStepanov wrote: >> I want to place Foo(1) to buckets[nn].key without postblit call. >> Compiler can't help me now, however, I think, It can do it without >> language change. > > File an enhancement request with explanations and sample code, the works. This will be good. Thanks! -- Andrei I think it's this one: https://issues.dlang.org/show_bug.cgi?id=12684 Kind of required when you requested emplace from rvalues: https://issues.dlang.org/show_bug.cgi?id=12628 |
September 18, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Thursday, 18 September 2014 at 00:53:40 UTC, Andrei Alexandrescu wrote: > On 9/17/14, 12:28 PM, IgorStepanov wrote: >> I want to place Foo(1) to buckets[nn].key without postblit call. >> Compiler can't help me now, however, I think, It can do it without >> language change. > > File an enhancement request with explanations and sample code, the works. This will be good. Thanks! -- Andrei Done: https://issues.dlang.org/show_bug.cgi?id=13492 |
September 18, 2014 Re: Example of the perils of binding rvalues to const ref | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Thursday, 18 September 2014 at 10:50:31 UTC, monarch_dodra wrote:
> On Thursday, 18 September 2014 at 00:53:40 UTC, Andrei Alexandrescu wrote:
>> On 9/17/14, 12:28 PM, IgorStepanov wrote:
>>> I want to place Foo(1) to buckets[nn].key without postblit call.
>>> Compiler can't help me now, however, I think, It can do it without
>>> language change.
>>
>> File an enhancement request with explanations and sample code, the works. This will be good. Thanks! -- Andrei
>
> I think it's this one:
> https://issues.dlang.org/show_bug.cgi?id=12684
>
> Kind of required when you requested emplace from rvalues:
> https://issues.dlang.org/show_bug.cgi?id=12628
Yes, your 12684 issue is a case of new 13492.
I've tried to classify the problem and describe a danger cases.
|
Copyright © 1999-2021 by the D Language Foundation