1. 16 2月, 2015 1 次提交
    • S
      Avoid unnecessary boxing and string allocs with String.Concat · 70d2dee7
      Stephen Toub 提交于
      When string concatenation encounters non-strings (e.g. path + '/', name + someInt32, etc.), it calls overloads of String.Concat that accept objects. These overloads then just call ToString on each of the objects, mapping to the C# spec which states that "any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object." When any of the individual items being concatenated is a value type, that object first gets boxed to be passed to String.Concat as an object, only to then have ToString called on the boxed object.
      
      This commit changes the local rewriter for string concatenation to test whether an argument is of an appropriate type, and if it is, to call ToString on it directly, rather than first boxing it. This can then affect which overload of Concat is used, as the type of the argument has changed to be String. The primary benefit of this is saving the allocation per value-type item. There are some secondary benefits, as well; for example, as there is a four-string overload of Concat but no four-object overload of Concat, if this optimization is able to force all of the items to be strings, the four-string overload can be used rather than allocating an object array for the inputs and then another string array for the resulting strings (inside of Concat).
      
      This commit also includes an additional optimization specific to const chars; rather than doing a ToString call at run time, we can do it at compile-time and emit a literal string instead of a literal char, saving both the boxing and the string allocation at run time.
      70d2dee7
  2. 12 2月, 2015 19 次提交
  3. 11 2月, 2015 20 次提交