Repository navigation
[C++] Clean up antlr4::misc::Interval and antlr4::dfa - #3409
Conversation
|
@mike-lischke Normal cleanup. |
| 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; } |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK, I follow your argumentation.
|
@parrt Another C++ patch to merge. |
|
@jcking It just appeared to me that we may have something wrong here, so I need your input. A |
|
So I believe a newer standard introduces something like |
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.