mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Move subtree sizes over to TreeAndSubtrees, using the different structure to represent the additional parse work that occurs, as well as making it clear which functions require the extra information. My intent is to make it hard to use this by accident. The subtree size is still tracked during Parse::Tree construction. I think a lot of that can be cleaned up, although we use it during placeholder assignment so it may take some work. I wanted to see what people thought about this before taking action on such a change. I'm using a 1m line source file generated by #4124 for testing. Command is `time bazel-bin/toolchain/install/prefix_root/bin/carbon compile --phase=check --dump-mem-usage ~/tmp/data.carbon` At head, what I'm seeing is: ``` ... parse_tree_.node_impls_: used_bytes: 61516116 reserved_bytes: 61516116 ... Total: used_bytes: 447814230 reserved_bytes: 551663894 ... 1.43s user 0.14s system 99% cpu 1.565 total ``` With `Tree::Verify` disabled completely, it looks like: ``` parse_tree_.node_impls_: used_bytes: 41010744 reserved_bytes: 41010744 ... Total: used_bytes: 427308858 reserved_bytes: 531158522 ... 1.20s user 0.13s system 99% cpu 1.332 total ``` Re-enabling just the basic verification (what is now `Tree::Verify`), I'm seeing maybe 0.05s slower, but that's within noise for my system. I do see variability in my timing results, and overall I think this is a 0.2s +/- 0.1s improvement versus the earlier (always testing `Extract` code) implementation. That's opt; debug builds will be unaffected, because the same checking occurs as before. Note, the subtree size is a third of the node representation, which is why I'm showing the decrease in memory usage here.
119 lines
4.0 KiB
C++
119 lines
4.0 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/parse/context.h"
|
|
#include "toolchain/parse/handle.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
auto HandleBindingPattern(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
// Parameters may have keywords prefixing the pattern. They become the parent
|
|
// for the full BindingPattern.
|
|
if (auto token = context.ConsumeIf(Lex::TokenKind::Template)) {
|
|
context.PushState({.state = State::BindingPatternTemplate,
|
|
.token = *token,
|
|
.subtree_start = state.subtree_start});
|
|
}
|
|
|
|
if (auto token = context.ConsumeIf(Lex::TokenKind::Addr)) {
|
|
context.PushState({.state = State::BindingPatternAddr,
|
|
.token = *token,
|
|
.subtree_start = state.subtree_start});
|
|
}
|
|
|
|
// Handle an invalid pattern introducer for parameters and variables.
|
|
auto on_error = [&]() {
|
|
CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
|
|
"Expected binding pattern.");
|
|
context.emitter().Emit(*context.position(), ExpectedBindingPattern);
|
|
// Add a placeholder for the type.
|
|
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
|
/*has_error=*/true);
|
|
state.has_error = true;
|
|
context.PushState(state, State::BindingPatternFinishAsRegular);
|
|
};
|
|
|
|
// The first item should be an identifier or `self`.
|
|
bool has_name = false;
|
|
if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
|
|
context.AddLeafNode(NodeKind::IdentifierName, *identifier);
|
|
has_name = true;
|
|
} else if (auto self =
|
|
context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
|
|
// Checking will validate the `self` is only declared in the implicit
|
|
// parameter list of a function.
|
|
context.AddLeafNode(NodeKind::SelfValueName, *self);
|
|
has_name = true;
|
|
}
|
|
if (!has_name) {
|
|
// Add a placeholder for the name.
|
|
context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
|
|
/*has_error=*/true);
|
|
on_error();
|
|
return;
|
|
}
|
|
|
|
if (auto kind = context.PositionKind();
|
|
kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
|
|
state.state = kind == Lex::TokenKind::Colon
|
|
? State::BindingPatternFinishAsRegular
|
|
: State::BindingPatternFinishAsGeneric;
|
|
// Use the `:` or `:!` for the root node.
|
|
state.token = context.Consume();
|
|
context.PushState(state);
|
|
context.PushStateForExpr(PrecedenceGroup::ForType());
|
|
} else {
|
|
on_error();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Handles BindingPatternFinishAs(Generic|Regular).
|
|
static auto HandleBindingPatternFinish(Context& context, NodeKind node_kind)
|
|
-> void {
|
|
auto state = context.PopState();
|
|
|
|
context.AddNode(node_kind, state.token, state.has_error);
|
|
|
|
// Propagate errors to the parent state so that they can take different
|
|
// actions on invalid patterns.
|
|
if (state.has_error) {
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
|
|
HandleBindingPatternFinish(context, NodeKind::CompileTimeBindingPattern);
|
|
}
|
|
|
|
auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
|
|
HandleBindingPatternFinish(context, NodeKind::BindingPattern);
|
|
}
|
|
|
|
auto HandleBindingPatternAddr(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
context.AddNode(NodeKind::Addr, state.token, state.has_error);
|
|
|
|
// If an error was encountered, propagate it while adding a node.
|
|
if (state.has_error) {
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
auto HandleBindingPatternTemplate(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
context.AddNode(NodeKind::Template, state.token, state.has_error);
|
|
|
|
// If an error was encountered, propagate it while adding a node.
|
|
if (state.has_error) {
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|