This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Optimization on String methods

8 replies
Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.

I'm not satisfied with some methods of String. Thats due to the fact
that there are the native methods from the JDK like `length` and
`charAt(Int)` and the methods of Scala like `size` and `apply(Int)`.

In Scala the preferred way is to call the methods of the Scala
collections. So it looks better to write `str.size` than `str.length` or
`str(3)` than `str.charAt(3)`.

The only problem is that the methods of Scala are not as efficient as
the native ones because of the implicit conversions.

Are there already plans to let the compiler optimize this (when one call
`str.size` it is transformed by the compiler to `str.length`)?
Or is this not useful?

soc
Joined: 2010-02-07,
User offline. Last seen 34 weeks 5 days ago.
Re: Optimization on String methods

Hi,

> The only problem is that the methods of Scala are not as efficient as
> the native ones because of the implicit conversions.

How did you measure that?

> Are there already plans to let the compiler optimize this (when one
> call `str.size` it is transformed by the compiler to `str.length`)?
> Or is this not useful?

I pretty much assume that the JVM does these trivial optimizations already.

Thanks and bye,

Simon

Ruediger Keller 2
Joined: 2010-04-30,
User offline. Last seen 42 years 45 weeks ago.
Re: Optimization on String methods

Hi,

this topic is being discussed quite frequently on the lists. The
consensus has always been that the conversion overhead is sometimes
optimized away if you use the latest and greatest JVM (Hotspot
Server). But *in general* the implicit conversions are not optimized
and you will have a performance penalty.

The question is whether the performance penalty really matters for you program.

Also regarding this:

> In Scala the preferred way is to call the methods of the Scala collections. So it looks better to write `str.size` than `str.length` or `str(3)` than `str.charAt(3)`.

Who says that in Scala the preferred way is to use the methods
provided by implicit conversion? I never heard of such a "best
practice" and I certainly wouldn't use implicit conversions carelessly
myself. Why is "size" better than "length"? Scala's Seq offers both,
so that's no indication either.

Regards,
Rüdiger

2011/7/27 Simon Ochsenreither :
> Hi,
>
>> The only problem is that the methods of Scala are not as efficient as
>> the native ones because of the implicit conversions.
>
> How did you measure that?
>
>> Are there already plans to let the compiler optimize this (when one
>> call `str.size` it is transformed by the compiler to `str.length`)?
>> Or is this not useful?
>
> I pretty much assume that the JVM does these trivial optimizations already.
>
> Thanks and bye,
>
> Simon
>
>

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Optimization on String methods

On 27/07/2011 10:25, Simon Ochsenreither wrote:
>> The only problem is that the methods of Scala are not as efficient as
>> the native ones because of the implicit conversions.
>
> How did you measure that?

I just did, using Caliper to ensure the test isn't too biased.
Perhaps the test is too simple, but looping over an array of strings of various size,
getting s(n % s.size) vs. s.charAt(n % s.length) (n is just a loop number), the first one
is run in 1.76ms while the second one scores 2.79ms.

Now, if you know that, and if you really need speed in real cases, you know you can go to
the Java version of the API... It is similar to Martin Odersky's pragmatism about
imperative coding (using 'while' for example, as opposed to 'for' or other collection
methods) vs. pure functional one...

It doesn't reduce the interest of the question, it just puts it in perspective: I fear
Scala developers has other cats to skin, bigger ones...

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Optimization on String methods

On 27.07.2011 10:25, Simon Ochsenreither wrote:
> Hi,
>
>> The only problem is that the methods of Scala are not as efficient as
>> the native ones because of the implicit conversions.
>
> How did you measure that?
I tested it with micro-benchmarks. In average they code with conversions
took more time.
But its only a theoretical question. I have no real application where I
got performance problems because of implicit conversions.
>
>> Are there already plans to let the compiler optimize this (when one
>> call `str.size` it is transformed by the compiler to `str.length`)?
>> Or is this not useful?
>
> I pretty much assume that the JVM does these trivial optimizations
> already.
No, it doesn't. It optimizes some things, that is correct, but not all.

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Optimization on String methods

On 27.07.2011 13:26, Ruediger Keller wrote:
>> In Scala the preferred way is to call the methods of the Scala collections. So it looks better to write `str.size` than `str.length` or `str(3)` than `str.charAt(3)`.
> Who says that in Scala the preferred way is to use the methods
> provided by implicit conversion? I never heard of such a "best
> practice" and I certainly wouldn't use implicit conversions carelessly
> myself. Why is "size" better than "length"? Scala's Seq offers both,
> so that's no indication either.

I thought of the preferred way due to programming to interfaces. If I
use `length` then I choose a method of an implementation and not of the
interface. I didn't know of the existence of `length` in Seq. I thought
there is only `size`. So this argument is not correct any more by
mentioning `size` and `length`.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Optimization on String methods

On Wed, Jul 27, 2011 at 18:02, Antoras wrote:
> On 27.07.2011 13:26, Ruediger Keller wrote:
>>>
>>> In Scala the preferred way is to call the methods of the Scala
>>> collections. So it looks better to write `str.size` than `str.length` or
>>> `str(3)` than `str.charAt(3)`.
>>
>> Who says that in Scala the preferred way is to use the methods
>> provided by implicit conversion? I never heard of such a "best
>> practice" and I certainly wouldn't use implicit conversions carelessly
>> myself. Why is "size" better than "length"? Scala's Seq offers both,
>> so that's no indication either.
>
> I thought of the preferred way due to programming to interfaces. If I use
> `length` then I choose a method of an implementation and not of the
> interface. I didn't know of the existence of `length` in Seq. I thought
> there is only `size`. So this argument is not correct any more by mentioning
> `size` and `length`.

Also, consider:

(_: Array[Int]).length
(_: Array[Int]).size
(_: String).length
(_: String).size

Arrays have size, Strings have length.

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Optimization on String methods

did you test with the newest jdk7?

-------- Original-Nachricht --------
> Datum: Wed, 27 Jul 2011 22:59:04 +0200
> Von: Antoras
> An: scala-debate [at] googlegroups [dot] com
> Betreff: Re: [scala-debate] Optimization on String methods

> On 27.07.2011 10:25, Simon Ochsenreither wrote:
> > Hi,
> >
> >> The only problem is that the methods of Scala are not as efficient as
> >> the native ones because of the implicit conversions.
> >
> > How did you measure that?
> I tested it with micro-benchmarks. In average they code with conversions
> took more time.
> But its only a theoretical question. I have no real application where I
> got performance problems because of implicit conversions.
> >
> >> Are there already plans to let the compiler optimize this (when one
> >> call `str.size` it is transformed by the compiler to `str.length`)?
> >> Or is this not useful?
> >
> > I pretty much assume that the JVM does these trivial optimizations
> > already.
> No, it doesn't. It optimizes some things, that is correct, but not all.
>

Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Optimization on String methods

On 28.07.2011 10:11, Dennis Haupt wrote:
> did you test with the newest jdk7?
No, I don't. I did it with JDK6. But today JDK7 is officially released.
I will run the tests which the newest version of the JVM soon.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland