Name.contentEquals() performance

Ron Shapiro ronshapiro at
Wed Oct 3 11:20:48 UTC 2018

In my profiling of javac compilations of large builds (>30 seconds), I'm
seeing about a noticeable amount of time being attributed to, because it creates a new
String each time in it's toString().equals(cs.toString()) check. I'm
wondering if we can optimize this without much cost.

I had suggested to Liam that we could add an `asString` variable that is
lazily computed in toString(). This would also benefit all of the other
CharSequence methods that delegate to the toString() representation.

Liam had another idea that would avoid adding a field (and the associated
memory that comes with that), taking advantage of the fact that Names are

if (cs instanceof Name) {
  Name other = (Name) cs;
  if (table == other.table) {
    return this == other;
return toString().equals(cs.toString());

The first option reduced builds that I tested of 45 seconds by 450ms (1%).
Liam's suggestion reduced bulilds by 400-420ms, so in the same ballpark but
a bit slower (but again, it allows for less retained memory?).

I figured both are simple, so I'd propose both.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list