Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't string concatenation automatically converted to StringBuilder in C#? [duplicate]

Possible Duplicate:
Why is String.Concat not optimized to StringBuilder.Append?

One day I was ranting about a particular Telerik control to a friend of mine. I told him that it took several seconds to generate a controls tree, and after profiling I found out that it is using a string concatenation in a loop instead of a StringBuilder. After rewriting it worked almost instantaneously.

So my friend heard that and seemed to be surprised that the C# compiler didn't do that conversion automatically like the Java compiler does. Reading many of Eric Lippert's answers I realize that this feature didn't make it because it wasn't deemed worthy enough. But if, hypothetically, costs were small to implement it, what rationale would stop one from doing it?

like image 690
Roman Royter Avatar asked Feb 03 '12 05:02

Roman Royter


2 Answers

But if, hypothetically, costs were small to implement it, what rationale would stop one from doing it?

It sounds like you're proposing a bit of a tautology: if there is no reason to not do X, then is there a reason to not do X? No.

I see little value in knowing the answers to hypothetical, counterfactual questions. Perhaps a better question to ask would be a question about the real world:

Are there programming languages that use this optimization?

Yes. In JScript.NET, we detect string concatenations in loops and the compiler turns them into calls to a string builder.

That might then be followed up with:

What are some of the differences between JScript .NET and C# that justify the optimization in the one language but not in the other?

A core assumption of JScript.NET is that its programmers are mostly going to be JavaScript programmers, and many of them will have already built libraries that must run in any implementation of ECMAScript. Those programmers might not know the .NET framework well, and even if they do, they might not be able to use StringBuilder without making their library code non-portable. It is also reasonable to assume that JavaScript programmers may be either novice programmers, or programmers who came to programming via their line of business rather than a course of study in computer science.

C# programmers are far more likely to know the .NET framework well, to write libraries that work with the framework, and to be experienced programmers who understand why looped string concatenation is O(n2) in the naive implementation. They need this optimization generated by the compiler less because they can just do it themselves if they deem it necessary.

In short: compiler features are about spending our budget to add value for the customer; you get more "bang for buck" adding the feature to JScript.NET than you do adding it to C#.

like image 58
Eric Lippert Avatar answered Sep 29 '22 06:09

Eric Lippert


The C# compiler does better than that.

a + b + c is compiled to String.Concat(a, b, c), which is faster than StringBuilder.
"a" + "b" is compiled directly to "ab" (useful for multi-line literals).

The only place to use StringBuilder is when concatenating repetitively inside a loop; the compiler cannot easily optimize that.

like image 35
SLaks Avatar answered Sep 29 '22 05:09

SLaks