Bug 11560 - Compiler should constant fold .Length calls on string constants
Summary: Compiler should constant fold .Length calls on string constants
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: Lowest enhancement
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2013-04-03 17:24 UTC by Bojan Rajkovic [MSFT]
Modified: 2013-04-04 16:48 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Bojan Rajkovic [MSFT] 2013-04-03 17:24:54 UTC
To save the JITter some time, it'd be nice if the compiler could transform Console.WriteLine ("foo".Length); into Console.WriteLine (3);. Currently, the IL produced does a ldstr followed by a callvirt to get_Length().
Comment 1 Marek Safar 2013-04-04 03:08:56 UTC
This cannot be done by compiler because people are running they own mscorlib with they own string implementation.
Comment 2 Mikayla Hutchinson [MSFT] 2013-04-04 13:26:16 UTC
Wait, what kind of argument is that? What are the chances anyones will ever replace string.Length with an implementation that behaves differently?
Comment 3 Marek Safar 2013-04-04 13:36:09 UTC
I saw 2 different mscorlib(s) with custom string, novell bugzilla should still have it somewhere in backlog
Comment 4 Mikayla Hutchinson [MSFT] 2013-04-04 13:57:22 UTC
I'm not disputing that there might be custom string implementations, but how likely is it that their .Length would behave differently for string constants in C# source? Is that edge case (which may never happen) really important enough to prevent this optimization?
Comment 5 Marek Safar 2013-04-04 16:48:50 UTC
It's as likely as this optimisation will have any effect. If you really need this optimisation it should be done at JIT/AOT level (it could be done already I didn't check) before doing this in all compilers. Just for C# you have normal C#, expressions trees, dynamic emitter + .net csc and I could continue with F# etc