mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Getting the basic constants is actually quite hot in the parser, and is spending all of its time in memory stalls due to touching the call stack just to return a constant when these are out-of-line. This alone is worth another 8% improvement in parsing, and 2% in syntax checking: ``` name old cpu/op new cpu/op delta BM_CompileAPIFileDenseDecls<Phase::Lex>/256 37.7µs ± 1% 37.5µs ± 2% -0.58% (p=0.019 n=19+18) BM_CompileAPIFileDenseDecls<Phase::Lex>/1024 181µs ± 1% 179µs ± 2% -0.95% (p=0.001 n=20+20) BM_CompileAPIFileDenseDecls<Phase::Lex>/4096 743µs ± 1% 736µs ± 2% -0.85% (p=0.001 n=20+19) BM_CompileAPIFileDenseDecls<Phase::Lex>/16384 3.30ms ± 1% 3.25ms ± 2% -1.48% (p=0.000 n=17+19) BM_CompileAPIFileDenseDecls<Phase::Lex>/65536 14.2ms ± 1% 13.9ms ± 2% -1.95% (p=0.000 n=17+19) BM_CompileAPIFileDenseDecls<Phase::Lex>/262144 64.7ms ± 2% 63.8ms ± 2% -1.30% (p=0.000 n=18+18) BM_CompileAPIFileDenseDecls<Phase::Parse>/256 71.0µs ± 1% 65.2µs ± 2% -8.20% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/1024 351µs ± 1% 320µs ± 1% -9.02% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/4096 1.43ms ± 2% 1.31ms ± 2% -8.41% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/16384 6.08ms ± 2% 5.57ms ± 1% -8.49% (p=0.000 n=19+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/65536 25.2ms ± 1% 23.2ms ± 1% -8.08% (p=0.000 n=19+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/262144 109ms ± 1% 101ms ± 1% -6.93% (p=0.000 n=19+19) BM_CompileAPIFileDenseDecls<Phase::Check>/256 752µs ± 1% 744µs ± 1% -1.07% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/1024 1.62ms ± 1% 1.59ms ± 1% -1.88% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/4096 4.97ms ± 2% 4.85ms ± 1% -2.43% (p=0.000 n=20+20) BM_CompileAPIFileDenseDecls<Phase::Check>/16384 19.0ms ± 2% 18.5ms ± 2% -2.32% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/65536 78.8ms ± 2% 76.9ms ± 2% -2.50% (p=0.000 n=20+20) BM_CompileAPIFileDenseDecls<Phase::Check>/262144 334ms ± 1% 327ms ± 2% -2.29% (p=0.000 n=20+20) ```
184 lines
5.3 KiB
C++
184 lines
5.3 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_PARSE_PRECEDENCE_H_
|
|
#define CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_
|
|
|
|
#include <optional>
|
|
|
|
#include "toolchain/lex/token_kind.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
// Given two operators `$` and `@`, and an expression `a $ b @ c`, how should
|
|
// the expression be parsed?
|
|
enum class OperatorPriority : int8_t {
|
|
// The left operator has higher precedence: `(a $ b) @ c`.
|
|
LeftFirst = -1,
|
|
// The expression is ambiguous.
|
|
Ambiguous = 0,
|
|
// The right operator has higher precedence: `a $ (b @ c)`.
|
|
RightFirst = 1,
|
|
};
|
|
|
|
enum class Associativity : int8_t {
|
|
LeftToRight = -1,
|
|
None = 0,
|
|
RightToLeft = 1
|
|
};
|
|
|
|
// A precedence group associated with an operator or expression.
|
|
class PrecedenceGroup {
|
|
public:
|
|
struct Trailing;
|
|
|
|
// Objects of this type should only be constructed using the static factory
|
|
// functions below.
|
|
PrecedenceGroup() = delete;
|
|
|
|
// Get the sentinel precedence level for a postfix expression. All operators
|
|
// have lower precedence than this.
|
|
static auto ForPostfixExpr() -> PrecedenceGroup;
|
|
|
|
// Get the precedence level for a top-level or parenthesized expression. All
|
|
// expression operators have higher precedence than this.
|
|
static auto ForTopLevelExpr() -> PrecedenceGroup;
|
|
|
|
// Get the sentinel precedence level for a statement context. All operators,
|
|
// including statement operators like `=` and `++`, have higher precedence
|
|
// than this.
|
|
static auto ForExprStatement() -> PrecedenceGroup;
|
|
|
|
// Get the precedence level at which to parse a type expression. All type
|
|
// operators have higher precedence than this.
|
|
static auto ForType() -> PrecedenceGroup;
|
|
|
|
// Get the precedence level at which to parse the type expression between
|
|
// `impl` and `as`.
|
|
static auto ForImplAs() -> PrecedenceGroup;
|
|
|
|
// Get the precedence level at which to parse expressions in requirements
|
|
// after `where` or `require`.
|
|
static auto ForRequirements() -> PrecedenceGroup;
|
|
|
|
// Look up the operator information of the given prefix operator token, or
|
|
// return std::nullopt if the given token is not a prefix operator.
|
|
static auto ForLeading(Lex::TokenKind kind) -> std::optional<PrecedenceGroup>;
|
|
|
|
// Look up the operator information of the given infix or postfix operator
|
|
// token, or return std::nullopt if the given token is not an infix or postfix
|
|
// operator. `infix` indicates whether this is a valid infix operator, but is
|
|
// only considered if the same operator symbol is available as both infix and
|
|
// postfix.
|
|
static auto ForTrailing(Lex::TokenKind kind, bool infix)
|
|
-> std::optional<Trailing>;
|
|
|
|
friend auto operator==(PrecedenceGroup lhs, PrecedenceGroup rhs) -> bool {
|
|
return lhs.level_ == rhs.level_;
|
|
}
|
|
|
|
// Compare the precedence levels for two adjacent operators.
|
|
static auto GetPriority(PrecedenceGroup left, PrecedenceGroup right)
|
|
-> OperatorPriority;
|
|
|
|
// Get the associativity of this precedence group.
|
|
auto GetAssociativity() const -> Associativity {
|
|
return static_cast<Associativity>(GetPriority(*this, *this));
|
|
}
|
|
|
|
private:
|
|
enum PrecedenceLevel : int8_t;
|
|
struct OperatorPriorityTable;
|
|
|
|
static const int8_t NumPrecedenceLevels;
|
|
|
|
// We rely on implicit conversions via `int8_t` for enumerators defined in the
|
|
// implementation.
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
PrecedenceGroup(int8_t level) : level_(level) {}
|
|
|
|
// The precedence level.
|
|
int8_t level_;
|
|
};
|
|
|
|
// Precedence information for a trailing operator.
|
|
struct PrecedenceGroup::Trailing {
|
|
// The precedence level.
|
|
PrecedenceGroup level;
|
|
// `true` if this is an infix binary operator, `false` if this is a postfix
|
|
// unary operator.
|
|
bool is_binary;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Only implementation details below this point.
|
|
//
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
enum PrecedenceGroup::PrecedenceLevel : int8_t {
|
|
// Sentinel representing the absence of any operator.
|
|
Highest,
|
|
// Terms.
|
|
TermPrefix,
|
|
// Numeric.
|
|
IncrementDecrement,
|
|
NumericPrefix,
|
|
Modulo,
|
|
Multiplicative,
|
|
Additive,
|
|
// Bitwise.
|
|
BitwisePrefix,
|
|
BitwiseAnd,
|
|
BitwiseOr,
|
|
BitwiseXor,
|
|
BitShift,
|
|
// Type formation.
|
|
TypePrefix,
|
|
TypePostfix,
|
|
// `where` keyword.
|
|
Where,
|
|
// Casts.
|
|
As,
|
|
// Logical.
|
|
LogicalPrefix,
|
|
Relational,
|
|
LogicalAnd,
|
|
LogicalOr,
|
|
// Conditional.
|
|
If,
|
|
// Assignment.
|
|
Assignment,
|
|
// Sentinel representing a context in which any operator can appear.
|
|
Lowest,
|
|
};
|
|
|
|
inline auto PrecedenceGroup::ForPostfixExpr() -> PrecedenceGroup {
|
|
return PrecedenceGroup(Highest);
|
|
}
|
|
|
|
inline auto PrecedenceGroup::ForTopLevelExpr() -> PrecedenceGroup {
|
|
return PrecedenceGroup(If);
|
|
}
|
|
|
|
inline auto PrecedenceGroup::ForExprStatement() -> PrecedenceGroup {
|
|
return PrecedenceGroup(Lowest);
|
|
}
|
|
|
|
inline auto PrecedenceGroup::ForType() -> PrecedenceGroup {
|
|
return ForTopLevelExpr();
|
|
}
|
|
|
|
inline auto PrecedenceGroup::ForImplAs() -> PrecedenceGroup {
|
|
return PrecedenceGroup(As);
|
|
}
|
|
|
|
inline auto PrecedenceGroup::ForRequirements() -> PrecedenceGroup {
|
|
return PrecedenceGroup(Where);
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|
|
|
|
#endif // CARBON_TOOLCHAIN_PARSE_PRECEDENCE_H_
|