mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:40:11 +01:00
We already go to some effort to avoid moving these, but we end up still moving them twice: once when adding to the worklist and again when reversing a chunk of the worklist. * To avoid a move when constructing the worklist, add an `EmplaceResult` utility that allows the result of a function call to be emplaced into a container. * To avoid moves when reversing the list, stop reversing it. Instead of reversing the list and popping tasks as we run them, we accumulate a sequence of tasks for a deferred definition region, run them in the order they were enqueued, then pop them all at the end. This will in some cases increase the high-water-mark of the size of the worklist, but not asymptotically. The same high-water-mark could be reached with the old approach by reordering the declarations in the source file. In passing, we no longer create `LeaveDeferredDefinitionRegion` tasks for non-nested regions. We don't need them, because we can detect that condition by our reaching the end of the worklist. This means that the enter / leave region actions are now always in correspondence -- we only create them for *nested* regions. The tasks have been renamed to convey this. We still move the suspended function states around if the worklist grows to over 64 entries and gets reallocated. We could potentially address that issue too by switching to a chunked allocation strategy as is used by `ValueStore` and then make the tasks noncopyable, but I'm not attempting that in this PR. --------- Co-authored-by: Dana Jansens <danakj@orodu.net>
63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
// 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
|