# Avoiding unnecessary copies from movable futures

**URL:** <https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076>\
**Category:** Development\
**Created:** [April 17, 2020, 4:47pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076 "2020-04-17T16:47:45Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![markus.pilman](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/markus.pilman/32/379_2.png) [@markus.pilman](https://forums.foundationdb.org/u/markus.pilman)\
**Post date:** [April 24, 2020, 4:35pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/21 "2020-04-24T16:35:16Z")

</div>

I agree. If we would start from scratch we probably should have the following semantic:

```auto
T t = wait(f); // moves result of f into t
const T& t = wait(f); // takes a reference into the future

```

The problem is, that currently the syntax for the first one has the semantics of the second one. So changing this (if even possible) will require us to refactor quite some code…

---

<div class="post-metadata">

**Author:** ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)\
**Post date:** [April 24, 2020, 5:31pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/22 "2020-04-24T17:31:08Z")

</div>

> [@markus.pilman](#):
>
> T t = wait(f); // moves result of f into t

That looks like it should copy the result of f into t to me

---

<div class="post-metadata">

**Author:** ![Daniel-B-Smith](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/daniel-b-smith/32/743_2.png) [@Daniel-B-Smith](https://forums.foundationdb.org/u/Daniel-B-Smith)\
**Post date:** [April 24, 2020, 5:33pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/23 "2020-04-24T17:33:42Z")

</div>

If this were standard C++, `wait()` would be a function that would return a prvalue that would always select the `T(&&)` constructor overload when present.

Assuming that there wasn’t copy elision going on.

---

<div class="post-metadata">

**Author:** ![markus.pilman](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/markus.pilman/32/379_2.png) [@markus.pilman](https://forums.foundationdb.org/u/markus.pilman)\
**Post date:** [April 24, 2020, 5:35pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/24 "2020-04-24T17:35:34Z")

</div>

> [@andrew.noyes](#):
>
> That looks like it should copy the result of f into t to me

Really? How so? If `wait` would be a normal function wouldn’t you expect the result of wait to be moved into the result? Say the following:

```auto
std::string foo(...);

// later in a function
std::string str = foo(/* some params */);

```

Would you expect a copy above? Or do you really think this syntax looks natural:

```auto
std::string&& str = foo(/* some params */);

```

---

<div class="post-metadata">

**Author:** ![ajbeamon](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/ajbeamon/32/13_2.png) [@ajbeamon](https://forums.foundationdb.org/u/ajbeamon)\
**Post date:** [April 24, 2020, 5:41pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/25 "2020-04-24T17:41:21Z")

</div>

When you say “move the result”, is that removing it from `f` such that `f.get()` would no longer work? If so, that’s maybe a little different than the function `foo` example, which isn’t obviously modifying a parameter to the function.

---

<div class="post-metadata">

**Author:** ![markus.pilman](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/markus.pilman/32/379_2.png) [@markus.pilman](https://forums.foundationdb.org/u/markus.pilman)\
**Post date:** [April 24, 2020, 5:56pm UTC](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076/26 "2020-04-24T17:56:15Z")

</div>

> [@ajbeamon](#):
>
> When you say “move the result”, is that removing it from `f` such that `f.get()` would no longer work?

This is the thing we need to figure out. I would say the following would be the best behavior (as in closest to what other C++ code would probably work):

```auto
T t = wait(someActor()); // moves result of someActor into t after completion
T t = wait(std::move(f)); // moves result of f into t after completion
T const& t = wait(f); // gets a reference into the future
T const& t = wait(someActor()); // compilation error
T t = wait(f); // copies the result of the future f when it becomes available

```

Now there are a few problems:

1. The actor compiler can’t do type deduction. So implementing this would be difficult at best.
2. `Future<T>` doesn’t own the value - so above is probably still wrong.

Instead having a `consume` method (as Steve suggested) is probably cleaner. IMHO the semantics should then be: only allow to call `consume` if `SAV<T>::futures == 1 && SAV<T>::promised == 0` for the underlying sav.

[Previous page](https://forums.foundationdb.org/t/avoiding-unnecessary-copies-from-movable-futures/2076.md?page=1)
