Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation optimization in the F# compiler

Tags:

f#

The C# compiler is smart enough to optimize string concatenation with the + operator into String.Concat calls.

The following code:

var a = "one";
var b = "two";
var c = "three";
var d = "four";

var x = a + b + c + d;

Is compiled into this IL:

IL_0000:  ldstr       "one"
IL_0005:  stloc.0     // a
IL_0006:  ldstr       "two"
IL_000B:  stloc.1     // b
IL_000C:  ldstr       "three"
IL_0011:  stloc.2     // c
IL_0012:  ldstr       "four"
IL_0017:  stloc.3     // d
IL_0018:  ldloc.0     // a
IL_0019:  ldloc.1     // b
IL_001A:  ldloc.2     // c
IL_001B:  ldloc.3     // d
IL_001C:  call        System.String.Concat

The compiler figured out the correct overload of String.Concat that takes 4 arguments and used that.

The F# compiler doesn't do that. Instead, each + is compiled into a separate call of String.Concat:

IL_0005:  ldstr       "one"
IL_000A:  ldstr       "two"
IL_000F:  call        System.String.Concat
IL_0014:  ldstr       "three"
IL_0019:  call        System.String.Concat
IL_001E:  ldstr       "four"
IL_0023:  call        System.String.Concat

Obviously this is because this particular optimization is not implemented in the F# compiler.

The question is why: is it technically hard to do or is there some other reason?

String concatenation is a fairly common operation and while I realize that the performance of compiled code is not a top priority, I imagine this kind of optimization would be useful in many cases.

like image 741
MisterMetaphor Avatar asked Feb 03 '14 14:02

MisterMetaphor


People also ask

Is F string faster than concatenate?

Summary: Using the string concatenation operator is slightly faster than using format string literals. Unless you are performing many hundreds of thousands of string concatenations and need them done very quickly, the implementation chosen is unlikely to make a difference.

How do you concatenate F-strings?

concatenate with f-strings In Python 3.6 and above, use f-strings(formatted string literals) which makes the format() function easier to use. This can be used by simply adding a f or F before the quotes in the string literal. Expand any variable in f-strings , and call any process in {} .

Is F string faster than format?

f-strings are faster than both %-formatting and str. format() . At runtime, each expression inside the curly braces gets evaluated within its own scope, and then it's put together into the final string.


1 Answers

I don't think there is anything hard about the optimization - I think the main reason why it is not implemented is that it is specific to string concatenation and does not apply more generally. However, it sounds like something that would be an interesting project using the F# open source release!

That said, even the C# optimization done above is not that clever. There is no reason why the compiler shouldn't just concatenate the strings directly when they are constants and produce:

IL_0000:  ldstr   "onetwothreefour"

In other words, there is always a tradeoff between adding something that is generally useful and adding more and more special cases - the C# compiler apparently has a few more special cases related to string concatenation...

like image 155
Tomas Petricek Avatar answered Oct 02 '22 02:10

Tomas Petricek