Files
carbon-lang/toolchain/sem_ir/impl.h
T
Chandler CarruthandJon Ross-Perkins 8992d22ab3 Port the toolchain to use the new Carbon hashtable (#4097)
This works to leverage the capabilities of the hashtable as much as
possible, for example using the key context in the value stores.
However, there may still be opportunities to refactor more deeply and
use the functionality even better. Hopefully this is at least
a reasonable start and gets us a clean baseline.

On an Arm M1, this is a 15% improvement on my large lexing stress test,
but ends up a wash on my x86-64 server. This is a smaller benefit than
I expected, and it's because we're using a set-of-IDs and looking up
values with a key context for things like identifiers. This pattern has
a surprising tradeoff. The new hashtable uses significantly less memory,
a 10% peak RSS reduction just from the hashtable change. But indirecting
through the vector of values makes growing the hashtable dramatically
less cache-friendly: it causes growth to randomly access every key when
rehashing. On x86, everything gained by the faster hashtable is lost in
even slower growth. And even on Arm, this eats into the benefits.

But I have a plan to tweak how identifiers specifically work to avoid
most of the growth, and so I suspect this is the right tradeoff on the
whole. It gives us significant working set size reduction and we can
likely avoid the regressed operation (growth with rehash) in most cases
by clever reserving and if necessary by adding a hash caching layer to
the table infrastructure.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-07-03 01:10:44 +00:00

88 lines
2.9 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
#ifndef CARBON_TOOLCHAIN_SEM_IR_IMPL_H_
#define CARBON_TOOLCHAIN_SEM_IR_IMPL_H_
#include "common/map.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// An implementation of a constraint.
struct Impl : public Printable<Impl> {
auto Print(llvm::raw_ostream& out) const -> void {
out << "{self: " << self_id << ", constraint: " << constraint_id << "}";
}
// Determines whether this impl has been fully defined. This is false until we
// reach the `}` of the impl definition.
auto is_defined() const -> bool { return witness_id.is_valid(); }
// Determines whether this impl's definition has begun but not yet ended.
auto is_being_defined() const -> bool {
return definition_id.is_valid() && !is_defined();
}
// The following members always have values, and do not change throughout the
// lifetime of the interface.
// TODO: Track the generic parameters for `impl forall`.
// The type for which the impl is implementing a constraint.
TypeId self_id;
// The constraint that the impl implements.
TypeId constraint_id;
// The following members are set at the `{` of the impl definition.
// The definition of the impl. This is an ImplDecl.
InstId definition_id = InstId::Invalid;
// The impl scope.
NameScopeId scope_id = NameScopeId::Invalid;
// The first block of the impl body.
// TODO: Handle control flow in the impl body, such as if-expressions.
InstBlockId body_block_id = InstBlockId::Invalid;
// The following members are set at the `}` of the impl definition.
// The witness for the impl. This can be `BuiltinError`.
InstId witness_id = InstId::Invalid;
};
// A collection of `Impl`s, which can be accessed by the self type and
// constraint implemented.
class ImplStore {
public:
// Looks up the impl with this self type and constraint, or creates a new
// `Impl` if none exists.
// TODO: Handle parameters.
auto LookupOrAdd(TypeId self_id, TypeId constraint_id) -> ImplId {
auto result = lookup_.Insert(std::pair{self_id, constraint_id}, [&]() {
return values_.Add({.self_id = self_id, .constraint_id = constraint_id});
});
return result.value();
}
// Returns a mutable value for an ID.
auto Get(ImplId id) -> Impl& { return values_.Get(id); }
// Returns the value for an ID.
auto Get(ImplId id) const -> const Impl& { return values_.Get(id); }
auto OutputYaml() const -> Yaml::OutputMapping {
return values_.OutputYaml();
}
auto array_ref() const -> llvm::ArrayRef<Impl> { return values_.array_ref(); }
auto size() const -> size_t { return values_.size(); }
private:
ValueStore<ImplId> values_;
Map<std::pair<TypeId, TypeId>, ImplId> lookup_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_IMPL_H_