diff options
author | Sébastien Doeraene <sjrdoeraene@gmail.com> | 2016-09-14 17:06:53 +0200 |
---|---|---|
committer | Sébastien Doeraene <sjrdoeraene@gmail.com> | 2016-09-15 11:42:02 +0200 |
commit | 44971d104f4364a0ddaa4f05afc4cc61ee39cdf7 (patch) | |
tree | e8d132dc6adb5ab782e6d93de9a80eeb61792a2d /test | |
parent | 05016d9035ab9b1c866bd9f12fdd0491f1ea0cbb (diff) | |
download | scala-44971d104f4364a0ddaa4f05afc4cc61ee39cdf7.tar.gz scala-44971d104f4364a0ddaa4f05afc4cc61ee39cdf7.tar.bz2 scala-44971d104f4364a0ddaa4f05afc4cc61ee39cdf7.zip |
Rewrite TraversableLike.stringPrefix not to blow up code size in Scala.js.
The commit 30876fe2dd8cbe657a6cad6b11bbc34f10c29b36 changed
`TraversableLike.stringPrefix` to report nicer results for inner
classes and method-local classes. The changes included calls to
`String.split()`, `Character.isDigit()` and `Character.isUpperCase()`.
This was particularly bad for Scala.js, because those methods
bring with them huge parts of the JDK (the `java.util.regex.*`
implementation on the one hand, and the Unicode database on the
other hand), which increased generated code size by 6 KB after
minimification and gzip for an application that does not otherwise
use those methods. This sudden increase is tracked in the Scala.js
bug tracker at https://github.com/scala-js/scala-js/issues/2591.
This commit rewrites `TraversableLike.stringPrefix` in a very
imperative way, without resorting to those methods. The behavior
is (mostly) preserved. There can be different results when
`getClass().getName()` contains non-ASCII lowercase letters and/or
digits. Those will now be recognized as user-defined instead of
likely compiler-synthesized (which is a progression). There still
are false positives for ASCII lowercase letters, which cause the
`stringPrefix` to be empty (as before).
Since the new implementation is imperative anyway, at least I made
it not allocate anything but the result `String` in the common
case where the result does not contain any `.`.
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/scala/collection/TraversableLikeTest.scala | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/test/junit/scala/collection/TraversableLikeTest.scala b/test/junit/scala/collection/TraversableLikeTest.scala index 8588956016..f703abf3e4 100644 --- a/test/junit/scala/collection/TraversableLikeTest.scala +++ b/test/junit/scala/collection/TraversableLikeTest.scala @@ -5,29 +5,65 @@ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +object TraversableLikeTest { + abstract class FakeIndexedSeq[A] extends IndexedSeq[A] { + def apply(i: Int): A = ??? + def length: Int = 0 + } +} + @RunWith(classOf[JUnit4]) class TraversableLikeTest { + import TraversableLikeTest._ + // For test_SI9019; out here because as of test writing, putting this in a method would crash compiler class Baz[@specialized(Int) A]() extends IndexedSeq[A] { def apply(i: Int) = ??? def length: Int = 0 } - + @Test def test_SI9019 { object Foo { def mkBar = () => { - class Bar extends IndexedSeq[Int] { - def apply(i: Int) = ??? - def length: Int = 0 - } + class Bar extends FakeIndexedSeq[Int] new Bar } + + def mkFalsePositiveToSyntheticTest = () => { + /* A class whose name tarts with an ASCII lowercase letter. + * It will be a false positive to the synthetic-part test. + */ + class falsePositive extends FakeIndexedSeq[Int] + new falsePositive + } + + def mkFrench = () => { + // For non-French speakers, this means "strange class name" + class ÉtrangeNomDeClasse extends FakeIndexedSeq[Int] + new ÉtrangeNomDeClasse + } + + def mkFrenchLowercase = () => { + class étrangeNomDeClasseMinuscules extends FakeIndexedSeq[Int] + new étrangeNomDeClasseMinuscules + } } + val bar = Foo.mkBar() assertEquals("Bar", bar.stringPrefix) // Previously would have been outermost class, TraversableLikeTest val baz = new Baz[Int]() assertEquals("TraversableLikeTest.Baz", baz.stringPrefix) // Make sure we don't see specialization $mcI$sp stuff + + // The false positive unfortunately produces an empty stringPrefix + val falsePositive = Foo.mkFalsePositiveToSyntheticTest() + assertEquals("", falsePositive.stringPrefix) + + val french = Foo.mkFrench() + assertEquals("ÉtrangeNomDeClasse", french.stringPrefix) + + val frenchLowercase = Foo.mkFrenchLowercase() + assertEquals("étrangeNomDeClasseMinuscules", frenchLowercase.stringPrefix) } } |