Skip to content

Conversation

@stevenschlansker
Copy link

@stevenschlansker stevenschlansker commented Nov 19, 2025

The class docs already state that Interval is immutable, but this is not enforced.
Followup to #4901

I tried to fix up existing sites where this supposedly immutable class is mutated, to instead replace with a new value.

The class docs already state that Interval is immutable, but this is not enforced.
Followup to antlr#4901

Signed-off-by: Steven Schlansker <[email protected]>
}
// if in middle a..x..b, split interval
if ( el>a && el<b ) { // found in this interval
int oldb = I.b;
Copy link
Author

Choose a reason for hiding this comment

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

I think this variable oldb is redundant since we already saved I.b above at line 656. Let me know if you'd like me to remove this. (I didn't, in the spirit of keeping the change minimal)

Copy link
Contributor

Choose a reason for hiding this comment

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

Only test coverage would tell us that. Let's not touch this.

}
// if in middle a..x..b, split interval
if ( el>a && el<b ) { // found in this interval
int oldb = I.b;
Copy link
Contributor

Choose a reason for hiding this comment

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

Only test coverage would tell us that. Let's not touch this.

@ericvergnaud
Copy link
Contributor

ericvergnaud commented Nov 21, 2025

@parrt blessed.
I'm very supportive of this change. The class comment is as follows:
/** An immutable inclusive interval a..b */
Note the immutable in there, which expresses the original intent.
The fact that we violated it internally might have been justified by performance, memory, whatever... I suspect mutations of intervals in the cache is the root cause of issue #4901, more than the cache itself. There doesn't seem to be any code checking whether the interval being mutated is in the interval cache, and should therefore absolutely not be mutated. This change ensures that the behavior complies with the intent.
It probably also helps HotSpot do a better job since it can cache a and b.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants