You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The good news is that in several places CharSequence could be used instead of (or additionally to) String. This would avoid some possible extra allocations.
I have a simple example: printing to a PrintStream a StringBuilder. PrintStream implements a method like print(obj: AnyRef) that can thus accept a StringBuilder:
However, it will call String.valueOf(obj: AnyRef) that will trigger under the hood StringBuilder.toString implemented in AbstractStringBuilder.
This means that currently, printing a StringBuilder to a console or a file, will allocate a new String systematically (even if the StringBuilder is reused in a loop for instance).
So what is my point? If PrintStream was modified internally to call encoder.append(csq: CharSequence), it would avoid the StringBuilder.toString trip and thus save additional String object allocation.
Remark: this problem is even more important because of the way AbstractStringBuilder.toString currently works, and as described in this other issue: #2905
It means that currently it will not only create a new String object instance, but it will also copy the underlying Array systematically.
The text was updated successfully, but these errors were encountered:
I came accross another interesting finding. It looks like the
CharSequence/Appendable
interfaces/traits are not systematically used in many places of the javalib. I think it is inherited from initial Java design (see: https://stackoverflow.com/questions/5949926/what-is-the-difference-between-append-and-write-methods-of-java-io-writer).The good news is that in several places
CharSequence
could be used instead of (or additionally to)String
. This would avoid some possible extra allocations.I have a simple example: printing to a
PrintStream
aStringBuilder
.PrintStream
implements a method likeprint(obj: AnyRef)
that can thus accept aStringBuilder
:scala-native/javalib/src/main/scala/java/io/PrintStream.scala
Line 168 in 63d0709
However, it will call
String.valueOf(obj: AnyRef)
that will trigger under the hoodStringBuilder.toString
implemented inAbstractStringBuilder
.This means that currently, printing a
StringBuilder
to a console or a file, will allocate a newString
systematically (even if theStringBuilder
is reused in a loop for instance).So what is my point? If PrintStream was modified internally to call
encoder.append(csq: CharSequence)
, it would avoid theStringBuilder.toString
trip and thus save additional String object allocation.Remark: this problem is even more important because of the way
AbstractStringBuilder.toString
currently works, and as described in this other issue: #2905It means that currently it will not only create a new
String
object instance, but it will also copy the underlyingArray
systematically.The text was updated successfully, but these errors were encountered: