-
Notifications
You must be signed in to change notification settings - Fork 397
Fix #1524: Implement constant folding for +[string]. #2007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Can one of the admins verify this patch? |
What prevents you from folding |
There is already an issue for this. Please prefix the commit message with "Fix #1524: ". |
And as always I forget to write the most important thing first: Thank you! |
Jenkins, ok to test |
The result needs to depend on the |
@@ -81,6 +81,64 @@ object OptimizerTest extends JasmineTest { | |||
|
|||
} | |||
|
|||
describe("String_+ constant folding") { | |||
it("must fold two constant string to the correct value") { | |||
expect("I am " + "constant").toEqual("I am constant") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check that this is not simply constant-folded by scalac? I think not, but one way to make sure is to extract one of the two sides in an @inline def
.
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2395/ |
def someString(a: Int) = "awesome! " + a + "/10" | ||
expect("ScalaJS" + (" is " + someString(10))).toEqual("ScalaJS is awesome! 10/10") | ||
expect((someString(10) + " is ") + "ScalaJS").toEqual("awesome! 10/10 is ScalaJS") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about "ScalaJS" + (1.4f + someString(10))"
?
"and test it" in the commit message should not be there. If it was not tested, it wouldn't get it in the first place ;) |
What do you mean by tracked? If you are talking about incrementality, changing the semantics requires creating a new optimizer anyways. |
Of you're right, I went through that reasoning, now I remember. The real problem is that we also need to know whether the We could add one, of course, and track it. But it's quite overkill, just to be able to constant fold a |
Ah, nice one. @tOverney please add a comment explaining this. Also, a (doc) comment on |
ee81a96
to
17b1f3f
Compare
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2396/ |
@@ -2183,6 +2216,43 @@ private[optimizer] abstract class OptimizerCore( | |||
case _ => default | |||
} | |||
|
|||
case String_+ => | |||
(lhs, rhs) match { | |||
case (l: Literal, r: Literal) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK now I think I see how you can factor that much more. Instead of defining literalToString(Literal): String
, define foldToStringForString_+(tree: Tree): Tree
which transforms IntLiteral(v)
into StringLiteral(v.toString)
, for example, and returns tree
if it cannot fold the conversion of tree
to string.
Then, here, you can just do:
val lhs1 = foldToStringForString_+(lhs)
val rhs1 = foldToStringForString_+(rhs)
(lhs1, rhs1) match {
...
}
and now you know that you only have to deal with StringLiteral
s. Any other stuff that you find is not foldable to a String
, and therefore you need not worry about it. Then you will have way less cases here.
(note: from there you can't use default
anymore, since at least you need to use lhs1
and rhs1
, not lhs
and rhs
, so default
s should become BinaryOp(String_+, lhs1, rhs1)
instead.
@sjrd @gzm0 Thanks for the feedback, Hopefully I've updated everything. |
That's all. Good job! |
The funny thing is that running that exact regex on the file would break some tests :) |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2453/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2454/ |
Scalastyle says:
|
Otherwise it's good.
|
6264a74
to
4360190
Compare
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2455/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2456/ |
f63010f
to
7367804
Compare
LGTM |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2457/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2458/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2459/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2460/ |
Guess what! Google Closure Compiler wrongly constant-folds In any case, you should therefore add a |
A new entry for the hall of fame: google/closure-compiler#1262 |
7367804
to
bfbbf7f
Compare
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2461/ |
The bootstrap test fails with the long strings, at the time of deserializing the Trees (so even before we reach the optimizer). I suggest you simply remove the test with long strings. We have enough evidence that the JS VMs can cope with the long literals, since all the "normal" tests passed. |
bfbbf7f
to
b7baee3
Compare
LGTM |
b7baee3
to
61fa19c
Compare
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2468/ |
Refer to this link for build results (access rights to CI server needed): https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/2469/ |
Fix #1524: Implement constant folding for +[string].
Yay! |
/cc @paullepoulpe