scala/scala — pull request #1380 by xeno-by (2012-09-22 12:49:18+00:00)
Title: SI-6277 fix for isXXX methods in reflection

The underlying problem is that some symbols require so-called initialization before certain methods (for example, the Symbol.isSealed method) return reliable results.

This doesn't bring much trouble in the compiler, because noone usually calls these methods before making sure that the symbol is initialized, but we cannot put this burden onto users of the reflection API.

Unfortunately we cannot just do automatic initialization in misbehaving methods, because that breaks the compiler, causing nasty cyclic reference errors. Quite probably this can be done, but not a few weeks before a major release.

Luckily there's a non-invasive fix. We can decorate runtime reflection symbols with a trait, which overrides the methods of interest and performs initialization prior to calling super. Then the compiler remains unaffected, and reflection users are happy.

***

The problem is that the cleanest implementation of the fix involves a de-optimization of the compiler (removal of final from Symbol.hasFlag and its buddies).

There's also an alternative that requires some copy/paste and is more fragile (though the fragility is controlled by a unit test), but its performance characteristics are non-obvious.

Therefore I asked Greg to do performance testing, and these tests ended up with very controversial results. Greg reran the tests again, and the results are more realistic, though it's still unclear what to conclude:

**First test run**

```
[baseline] 630f1b4 minor cleanup for reflection
Vector_scala
Mean: 747.25ms ± 2.46ms (cov: 0.99%; 63:123)
scalap-src
Mean: 4317.65ms ± 13.60ms (cov: 0.95%; 35:95)

[approach #1] 6711d6e SI-6277 decorates runtime symbols with auto-init
Vector_scala
Mean: 783.92ms ± 2.59ms (cov: 0.99%; 69:129)
scalap-src
Mean: 4523.95ms ± 14.05ms (cov: 0.93%; 80:140)

[approach #2] 549267f an optimization for the previous commit
Vector_scala
Mean: 750.01ms ± 2.51ms (cov: 1.01%; 79:139)
scalap-src
Mean: 4262.53ms ± 14.12ms (cov: 1.00%; 40:100)
```

**Second test run**

```

[baseline] 630f1b4 minor cleanup for reflection
Vector_scala
Mean: 762.27ms ± 2.54ms (cov: 1.00%; 52:112)
scalap-src
Mean: 4249.28ms ± 13.90ms (cov: 0.98%; 34:94)

[approach #1] 6711d6e SI-6277 decorates runtime symbols with auto-init
Vector_scala
Mean: 797.82ms ± 2.50ms (cov: 0.94%; 58:118)
scalap-src
Mean: 4569.82ms ± 15.31ms (cov: 1.01%; 25:85)

[approach #2] 549267f an optimization for the previous commit
Vector_scala
Mean: 749.13ms ± 2.49ms (cov: 1.00%; 83:143)
scalap-src
Mean: 4321.25ms ± 14.20ms (cov: 0.99%; 34:94)
```