Skip to content

SI-9571 Avoid boxing primitives in string concatenation#4944

Merged
lrytz merged 2 commits intoscala:2.12.xfrom
lrytz:stringBuilderNoBox
Feb 12, 2016
Merged

SI-9571 Avoid boxing primitives in string concatenation#4944
lrytz merged 2 commits intoscala:2.12.xfrom
lrytz:stringBuilderNoBox

Conversation

@lrytz
Copy link
Member

@lrytz lrytz commented Feb 4, 2016

No description provided.

@scala-jenkins scala-jenkins added this to the 2.12.0-M4 milestone Feb 4, 2016
@lrytz lrytz added the welcome hello new contributor! label Feb 4, 2016
@lrytz
Copy link
Member Author

lrytz commented Feb 4, 2016

@melezov thanks a lot for your patch, very nice work! I refined it a bit and added some tests.

Review by @soc. For the record, @melezov signed the CLA.

@lrytz lrytz force-pushed the stringBuilderNoBox branch from 8a0a410 to 89343e9 Compare February 4, 2016 16:00
@soc
Copy link
Contributor

soc commented Feb 5, 2016

Maybe we should rename genStringConcat to something like genReferenceConcat given the name of genPrimitiveConcat?

@soc
Copy link
Contributor

soc commented Feb 5, 2016

Apart from that, LGTM.

@lrytz lrytz force-pushed the stringBuilderNoBox branch from 89343e9 to 96283ed Compare February 5, 2016 09:00
@lrytz
Copy link
Member Author

lrytz commented Feb 5, 2016

Thanks for the review, @soc - with fresh eyes I managed to get rid of some duplicate code, now things look better.

@lrytz lrytz force-pushed the stringBuilderNoBox branch from 96283ed to bea2860 Compare February 5, 2016 09:02
case ct: ClassBType if ct.isSubtypeOf(jlCharSequenceRef).get => jlCharSequenceRef
case rt: RefBType => ObjectRef
case pt: PrimitiveBType => pt // Currently this ends up being boxed in erasure
// Don't match for `ArrayBType(CHAR)`, even though StringBuilder has an such an overload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry 😓 really minor "has an such an overload" -> "has such an overload"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks!

@lrytz lrytz force-pushed the stringBuilderNoBox branch from bea2860 to cb68d9c Compare February 6, 2016 07:58
@lrytz
Copy link
Member Author

lrytz commented Feb 9, 2016

@soc does this LGTY now?

@soc
Copy link
Contributor

soc commented Feb 11, 2016

LGTM!

case Apply(boxOp, value :: Nil) if currentRun.runDefinitions.isBox(boxOp.symbol) =>
// Eliminate boxing of primitive values. Boxing is introduced by erasure because
// there's only a single synthetic `+` method "added" to the string class.
value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will incorrectly transform:

"xx" + {println("side effect"); Int}.box(1)

to

val sb = new StringBuilder(); sb.append("xx"); sb.append(1)

However, this isn't the only place in the backend that has this bug:

scala> {???; Int}.box(1)
res4: Integer = 1

We can treat this as a separate bug: https://github.com/scala/scala-dev/issues/85

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find..!

lrytz added a commit that referenced this pull request Feb 12, 2016
SI-9571 Avoid boxing primitives in string concatenation
@lrytz lrytz merged commit 7c25282 into scala:2.12.x Feb 12, 2016
@lrytz lrytz deleted the stringBuilderNoBox branch February 15, 2016 08:28
@adriaanm adriaanm added 2.12 and removed 2.12 labels Oct 29, 2016
nicolasstucki added a commit to lampepfl/scala that referenced this pull request Oct 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

welcome hello new contributor!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants