Skip to content

Commit

Permalink
Improve make_returnable_ref efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Dec 30, 2020
1 parent 619d0af commit 23d9635
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/alia/flow/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ namespace alia {
template<class T>
struct returnable_ref_node : data_node
{
// TODO: Use optional instead.
std::unique_ptr<T> value;
T value;

void
clear_cache()
returnable_ref_node(T& x) : value(std::move(x))
{
value.reset();
}
};

Expand All @@ -28,13 +25,14 @@ T&
make_returnable_ref(context ctx, T x)
{
returnable_ref_node<T>* node;
if (get_data_node(ctx, &node))
node->value.reset(new T(x));
else
*node->value = std::move(x);
return *node->value;
if (!get_data_node(
ctx, &node, [&] { return new returnable_ref_node<T>(x); }))
{
node->value = std::move(x);
}
return node->value;
}

} // namespace alia

#endif
#endif

0 comments on commit 23d9635

Please sign in to comment.