Skip to content

[C++] Clean up antlr4::misc::Interval and antlr4::dfa - #3409

Merged
parrt merged 1 commit into
antlr:masterfrom
jcking:cpp-optimize
Dec 18, 2021
Merged

parrt merged 1 commit into
antlr:masterfrom
jcking:cpp-optimize

Conversation

@jcking

@jcking jcking commented Dec 14, 2021

Copy link
Copy Markdown
Collaborator

Fix const correctness, remove virtual where it makes sense (class is instantiated directly by antlr4 and there is no way for users to override), and make things constexpr where it is feasible.

@jcking

jcking commented Dec 14, 2021

Copy link
Copy Markdown
Collaborator Author

@mike-lischke Normal cleanup.

Comment thread runtime/Cpp/runtime/src/misc/Interval.h Resolved
Comment on lines +29 to +38
constexpr Interval() : Interval(static_cast<ssize_t>(-1), static_cast<ssize_t>(-2)) {}

constexpr explicit Interval(size_t a_, size_t b_) : Interval(symbolToNumeric(a_), symbolToNumeric(b_)) {}

constexpr Interval(ssize_t a_, ssize_t b_) : a(a_), b(b_) {}

/// return number of elements between a and b inclusively. x..x is length 1.
/// if b < a, then length is 0. 9..10 has length 2.
size_t length() const;
constexpr size_t length() const { return b >= a ? static_cast<size_t>(b - a + 1) : 0; }

bool operator == (const Interval &other) const;
constexpr bool operator==(const Interval &other) const { return a == other.a && b == other.b; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so fond of having implementation code in header files. One single change (and be it for testing) and you might have to recompile everything that includes that header (in the worst case the entire application, which can take a while in C++ as you know).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting the implementation in the header allows the compiler to perform optimizations without requiring link time optimization. Without it in the header file, the compiler is forced to emit a function call. With it in the header file, the compiler can boil down the Interval constructor to two simple move instructions.

I am not saying we should put everything in the header, but rather code that is called frequently and is relatively simple. Mostly accessors, logical operators, and etc. Things that boil down to a few instructions or simply forward to another function.

@mike-lischke mike-lischke Dec 18, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I follow your argumentation.

@mike-lischke

Copy link
Copy Markdown
Member

@parrt Another C++ patch to merge.

@parrt parrt added this to the 4.9.4 milestone Dec 18, 2021
@parrt
parrt merged commit dd623ec into antlr:master Dec 18, 2021
@mike-lischke

mike-lischke commented Dec 18, 2021 •

Copy link
Copy Markdown
Member

@jcking It just appeared to me that we may have something wrong here, so I need your input. A constexpr can be evaluated at compile time, but you introduced that for a number of methods and even constructors (didn't know this is possible). However, the limit members a and b can be altered at runtime, so length() or operator == are not constant. Do I miss something here?

@jcking

jcking commented Dec 20, 2021

Copy link
Copy Markdown
Collaborator Author

So constexpr is a little bit non-intuitive, as everybody has probably made that assumption. constexpr does not mean it must be evaluated at compile time, it simply restricts what can appear in the definition so that it could, if all inputs are known at compile time, be evaluated at compile time. It doesn't mean it actually is, as the compiler could choose not to technically AFAIK.

I believe a newer standard introduces something like consteval which enforces compile time evaluation, but constexpr doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants