Files
carbon-lang/toolchain/sem_ir/bundle.cpp
T
Chandler Carruth 3625e74a7d Out-of-line cold YAML and debug-printing paths from widely-included headers (#7320)
These were defined inline in headers reached by most of the toolchain,
and as non-template functions their bodies -- including some very
expensive template instantiations -- are compiled in every including TU
even though they are only cold debug/dump paths:

- BundleStore's YAML output entry points instantiate IdAndKind::Dispatch
  over all ~50 ID kinds (two ~365ms instantiations per TU) plus
  Yaml::OutputScalar/std::function machinery; moved to a new bundle.cpp.
- TypeStore, ConstantValueStore, NameStoreWrapper, and SharedValueStores
  OutputYaml bodies wrap capturing lambdas in std::function via
  Yaml::OutputMapping; moved to their existing .cpp files.

This change appears to be worth another 10% compile time reduction.

Assisted-by: Claude
2026-06-08 02:42:23 +00:00

75 lines
2.6 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 "toolchain/sem_ir/bundle.h"
#include "toolchain/base/block_value_store_impl.h"
#include "toolchain/base/value_store_impl.h"
namespace Carbon::SemIR {
auto BundleStore::OutputYaml() const -> Yaml::OutputMapping {
return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
for (auto bundle_id : store_.ids()) {
AddYamlMapEntry(map, bundle_id);
}
});
}
auto BundleStore::OutputBundleYaml(RawBundleId bundle_id) const
-> Yaml::OutputMapping {
return Yaml::OutputMapping([this, bundle_id](Yaml::OutputMapping::Map map) {
AddYamlMapEntry(map, bundle_id);
});
}
auto BundleStore::CollectMemUsage(MemUsage& mem_usage,
llvm::StringRef label) const -> void {
store_.CollectMemUsage(mem_usage, label);
bundle_kind_cache_.CollectMemUsage(mem_usage, label);
}
auto BundleStore::BundleString(RawBundleId bundle_id) const -> std::string {
RawStringOstream out;
llvm::ListSeparator sep;
out << "{";
for (auto [i, raw_id] : llvm::enumerate(store_.Get(bundle_id))) {
PrintBundleField(out, sep, i, raw_id);
}
out << "}";
return out.TakeStr();
}
auto BundleStore::AddYamlMapEntry(Yaml::OutputMapping::Map& map,
RawBundleId bundle_id) const -> void {
auto kind_set = bundle_kind_cache_.GetWithDefault(bundle_id, IdKindSet{});
if (kind_set.empty()) {
map.Add(PrintToString(bundle_id),
Yaml::OutputScalar(BundleString(bundle_id)));
} else if (kind_set.size() == 1) {
IdAndKind typed_bundle_id(*kind_set.begin(), bundle_id.index);
auto bundle_string = typed_bundle_id.Dispatch<std::string>(
[this](auto id) { return BundleString(id); });
map.Add(PrintToString(bundle_id), Yaml::OutputScalar(bundle_string));
} else {
map.Add(PrintToString(bundle_id), YamlBundleMap(bundle_id, kind_set));
}
}
auto BundleStore::YamlBundleMap(RawBundleId bundle_id,
const IdKindSet& kinds) const
-> Yaml::OutputMapping {
return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
for (auto [i, id_kind] : llvm::enumerate(kinds)) {
IdAndKind typed_bundle_id(id_kind, bundle_id.index);
// TODO: make this a YAML sequence instead of a map.
map.Add(llvm::itostr(i),
typed_bundle_id.Dispatch<std::string>(
[this](auto id) { return BundleString(id); }));
}
});
}
} // namespace Carbon::SemIR