Hi Johannes,

joinPromises() actually takes ownership of the array (the parameter is
taken by-move), so it doesn't have to allocate a copy internally.
joinPromises() has to store the list of promises somewhere, so I don't
think you can actually do much better than this, unless we wanted to
special-case for certain fixed sizes (2, maybe), but I think that's
probably over-engineering.

Also note that promises in general involve a lot of heap allocation, so the
overhead of allocating this particular array is probably negligible.

I don't like that promises are allocation-heavy, but it's hard to see what
we can do about that without making them harder to use. I think in the
longer run, the introduction of async/await in C++ will help reduce the
number of allocations needed considerably, but we need to wait for the
compilers to support it.

-Kenton

On Wed, Sep 13, 2017 at 3:08 AM, Johannes Zeppenfeld <[email protected]>
wrote:

> Hi Kenton,
>
> thanks for the clarification.
>
> Are there any plans to allow joining of Promise<void>s without the need
> for allocating a separate Array? I'd think this is a fairly common
> operation, so avoiding a heap-allocated array would be a welcome
> optimization.
>
> -Johannes
>
> On Friday, September 8, 2017 at 6:34:30 PM UTC+2, Kenton Varda wrote:
>>
>> Hi Johannes,
>>
>> Actually I don't think using braced initialization format ever worked,
>> due to the C++ standard's unfortunate decision that elements of an
>> std::initializer_list should be const. My example code there is erroneous.
>>
>> What you actually need to do is something like:
>>
>>     auto builder = kj::heapArrayBuilder<kj::Promise<void>>(2);
>>     builder.add(kj::mv(promise1));
>>     builder.add(kj::mv(promise2));
>>     auto joined = kj::joinPromises(builder.finish());
>>
>> -Kenton
>>
>> On Fri, Sep 8, 2017 at 2:20 AM, Johannes Zeppenfeld <[email protected]>
>> wrote:
>>
>>> Hi Kenton,
>>>
>>> in [1] you give an example of joining two Promise<void>s to produce a
>>> Promise<void> that is fulfilled when both other promises have fulfilled (to
>>> chain a list of related promises, avoiding a TaskSet and allowing to wait
>>> on the result).
>>>
>>> In Capnp 6.0.1 using g++ 5.4.0 this gives me the following error:
>>>
>>> error: no matching function for call to ‘joinPromises(<brace-enclosed
>>> initializer list>)’
>>>    tasks = kj::joinPromises({kj::mv(tasks), kj::mv(newTask)});
>>>
>>> /usr/local/include/kj/async.h:312:24: note: candidate:
>>> kj::Promise<void> kj::joinPromises(kj::Array<kj::Promise<void> >&&)
>>>    friend Promise<void> joinPromises(Array<Promise<void>>&& promises);
>>>                         ^
>>> /usr/local/include/kj/async.h:312:24: note:   no known conversion for
>>> argument 1 from ‘<brace-enclosed initializer list>’ to
>>> ‘kj::Array<kj::Promise<void> >&&’
>>>
>>> Has something changed here to make this no longer possible? Do I have to
>>> use an ArrayBuilder? Is there some other way to join Promise<void>s without
>>> needing to allocate an Array?
>>>
>>> Thanks!
>>> Johannes
>>>
>>>
>>> [1] https://github.com/capnproto/capnproto/issues/286#issuecomme
>>> nt-185975985
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Cap'n Proto" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> Visit this group at https://groups.google.com/group/capnproto.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at https://groups.google.com/group/capnproto.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to