Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further micro-optimize
singletonIterator
, just to be safe / waste m…
…y morning. After cl/591014189, we're seeing the expected (tiny) memory improvements. (It's also possible that we're seeing other memory improvements from clearing references earlier, but that is speculative.) However, we're also seeing higher CPU numbers. I kind of expect to learn that that's a profiling artifact. (Microbenchmarks show an _improvement_ from that CL.) But just in case it's a result of having to read `static final` fields, let's stop doing that. (Also, if it matters, this CL ends up making the bytecode of `next` and `hasNext` _almost_ as short as it was before cl/591014189.) I could imagine trying yet further things, like moving the `throw` statement into a helper method to reduce the size of `SingletonIterator.next()` itself, perhaps helping inlining (even though the method is already below the `MaxInlineSize`, which is normally 35 per `java -XX:+PrintFlagsFinal`). (Benchmarks didn't show any improvement, but I don't know how well they'd reflect inlining, especially multiple rounds of inlining? My memory of all this is fuzzy.) Mainly, though, if there is anything that actually needs to be done after cl/591014189, it's probably this CL. And we likely don't really need even that. (I had also considered a different approach to this CL that used `null` instead of `this`: cl/626382992. However, that required a second implementation class for the null case (or another `static final` sentinel), and I worry that that may make iteration over `ImmutableList`, etc. become megamorphic for some callers. (If anything, maybe we should go the opposite direction and introduce another common iterator superclass with an index and size in it (or just `remainingElements`??) so that it can implement `hasNext` for both singleton and non-singleton iterators!)) (Yet another possibility would be to keep the sentinel but make it of a special type so that I can use `instanceof`. But everything that I hear about https://github.com/RedHatPerf/type-pollution-agent suggests to me that calling `instanceof` on random objects is not a good way to improve performance :)) RELNOTES=n/a PiperOrigin-RevId: 626542813
- Loading branch information