-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Avoid moving around large suspended function states in the deferred definition worklist. #5608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee71cff
Avoid moving around large suspended function states in the deferred
zygoloid 8b369b3
Trailing return types.
zygoloid 7d84ffa
Apply suggestions from code review
zygoloid e255e1d
Add some commentary explaining how EmplaceResult works and some caveats.
zygoloid 7030630
Rephrase comment
zygoloid ca6d18f
Ename EmplaceResult -> EmplaceByCalling.
zygoloid af1351a
Fix another case where we can avoid a copy.
zygoloid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef CARBON_COMMON_EMPLACE_BY_CALLING_H_ | ||
#define CARBON_COMMON_EMPLACE_BY_CALLING_H_ | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
namespace Carbon { | ||
|
||
// A utility to use when calling an `emplace` function to emplace the result of | ||
// a function call. Expected usage is: | ||
// | ||
// my_widget_vec.emplace_back(EmplaceByCalling([&] { | ||
// return ConstructAWidget(...); | ||
// })); | ||
// | ||
// In this example, the result of `ConstructAWidget` will be constructed | ||
// directly into the new element of `my_widget_vec`, without performing a copy | ||
// or move. | ||
// | ||
// Note that the type of the argument to `emplace_back` is an `EmplaceByCalling` | ||
// instance, not the type `DestT` stored in the container. When the `DestT` | ||
// instance is eventually initialized directly from the `EmplaceByCalling`, a | ||
// conversion function on `EmplaceByCalling` is used that converts to the type | ||
// `DestT` being emplaced. This `DestT` initialization does not call an | ||
// additional `DestT` copy or move constructor to initialize the result, and | ||
// instead initializes it in-place in the container's storage, per the C++17 | ||
// guaranteed copy elision rules. Similarly, within the conversion function, the | ||
// result is initialized directly by calling `make_fn`, again relying on | ||
// guaranteed copy elision. | ||
// | ||
// Because the make function is called from the conversion function, | ||
// `EmplaceByCalling` should only be used in contexts where it will be used to | ||
// initialize a `DestT` object exactly once. This is generally true of `emplace` | ||
// functions. Also, because the `make_fn` callback will be called after the | ||
// container has made space for the new element, it should not inspect or modify | ||
// the container that is being emplaced into. | ||
template <typename MakeFnT> | ||
class EmplaceByCalling { | ||
public: | ||
explicit(false) EmplaceByCalling(MakeFnT make_fn) | ||
: make_fn_(std::move(make_fn)) {} | ||
|
||
// Convert to the exact return type of the make function, by calling the make | ||
// function to construct the result. No implicit conversions are permitted | ||
// here, as that would mean we are not constructing the result in place. | ||
template <typename DestT> | ||
requires std::same_as<DestT, std::invoke_result_t<MakeFnT&&>> | ||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
explicit(false) operator DestT() && { | ||
return std::move(make_fn_)(); | ||
} | ||
|
||
private: | ||
MakeFnT make_fn_; | ||
}; | ||
|
||
template <typename MakeFnT> | ||
EmplaceByCalling(MakeFnT) -> EmplaceByCalling<MakeFnT>; | ||
|
||
} // namespace Carbon | ||
|
||
#endif // CARBON_COMMON_EMPLACE_BY_CALLING_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "common/emplace_by_calling.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <list> | ||
|
||
namespace Carbon { | ||
namespace { | ||
|
||
struct NoncopyableType { | ||
NoncopyableType() = default; | ||
NoncopyableType(const NoncopyableType&) = delete; | ||
auto operator=(const NoncopyableType&) -> NoncopyableType& = delete; | ||
}; | ||
|
||
auto Make() -> NoncopyableType { return NoncopyableType(); } | ||
|
||
TEST(EmplaceByCalling, Noncopyable) { | ||
std::list<NoncopyableType> list; | ||
// This should compile. | ||
list.emplace_back(EmplaceByCalling(Make)); | ||
} | ||
|
||
TEST(EmplaceByCalling, NoncopyableInAggregate) { | ||
struct Aggregate { | ||
int a, b, c; | ||
NoncopyableType noncopyable; | ||
}; | ||
|
||
std::list<Aggregate> list; | ||
// This should compile. | ||
list.emplace_back(EmplaceByCalling( | ||
[] { return Aggregate{.a = 1, .b = 2, .c = 3, .noncopyable = Make()}; })); | ||
} | ||
|
||
class CopyCounter { | ||
public: | ||
explicit CopyCounter(int* counter) : counter_(counter) {} | ||
CopyCounter(const CopyCounter& other) : counter_(other.counter_) { | ||
++*counter_; | ||
} | ||
|
||
private: | ||
int* counter_; | ||
}; | ||
|
||
TEST(EmplaceByCalling, NoCopies) { | ||
std::vector<CopyCounter> vec; | ||
vec.reserve(10); | ||
int copies = 0; | ||
for (int i = 0; i != 10; ++i) { | ||
vec.emplace_back(EmplaceByCalling([&] { return CopyCounter(&copies); })); | ||
} | ||
EXPECT_EQ(0, copies); | ||
} | ||
|
||
} // namespace | ||
} // namespace Carbon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we still want to push_back unless we're using the EmplaceResult tool, don't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When adding an element of the same type to a vector, yeah, we should be using
push_back
rather thanemplace_back
. But the element type of the worklist is a variant, notLeaveNestedDeferredDefinitionScope
. We want anemplace_back
not apush_back
here so that we pass in an (empty)LeaveNestedDeferredDefinitionScope
and the vector calls the variant converting constructor, rather than constructing a (large but almost entirely uninitialized) variant instance on the stack here and a variant copy in the vectorpush_back
logic.