StringBuilder测评

一、主要接口

    1. Append
    • 这个接口, 除了参数string类型char[] 类型都会调用参数的 ToString
    • unsafe fixed 拷贝。Buffer.Memcpy((byte)dmem, (byte)smem, charCount * 2); // 2 used everywhere instead of sizeof(char)
    1. AppendFormat 实现方式通过 format 逐字节解析 进行Append, 参数通过参数的IFormattable接口ToString、或者类型的 ToString。 所以非 string类型char[]类型都会调用参数的 ToString
    1. AppendLine 等于 Append "\r\n"
    1. ToString string.FastAllocateString(Length); unsafe fixed 拷贝。
    1. Clear ~= Length = 0
    1. 其他辅助方法 Insert、Remove、Replace

二、 ValueType 类型的ToString

ToString

1. (System.SByte)       无gc
2. (System.Int16)       无gc
3. (System.Int32)       无gc
5. (System.Int64)       无gc
6. (System.Single)          有gc、 28b
7. (System.Double)          有gc、 56b、 两次gc
8. (System.Byte)        无gc 
9. (System.UInt16)      无gc
10. (System.UInt32)     无gc
11. (System.UInt64)     无gc
12. (System.Boolean)        有gc、 首次70b,后面无gc
14. (System.Decimal)        有gc、 76b
15. (System.Enum)           有gc、 首次gc和enum 名称字节数有关。后面固定 20b
16. (System.Char)           有gc、 28b

类型IFormattable的ToString方法默认等同于类型的ToString, 除非设置IFormatProvider charBoolean 没有IFormattable接口

三、总结

    1. 无论是StringBuilder的Append,还是StringBuilder的AppendFormat,参数类型为 Stringchar[],则无gc
    1. 值类型 使用StringBuilder 是否有gc依据 类型ToString()
    1. 浮点型的gc,主要来自内存储存方式。
  • Int32.ToString
    [SecuritySafeCritical]
    public string ToString()
    {
        return Number.FormatInt32(this, null, NumberFormatInfo.CurrentInfo);
    }

        public static NumberFormatInfo CurrentInfo
        {
            get
            {
                CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
                if (!currentCulture.m_isInherited)
                {
                    NumberFormatInfo numInfo = currentCulture.numInfo;
                    if (numInfo != null)
                    {
                        return numInfo;
                    }
                }
                return (NumberFormatInfo)currentCulture.GetFormat(typeof(NumberFormatInfo));
            }
        }

        // Token: 0x060011BA RID: 4538 RVA: 0x00048649 File Offset: 0x00046849
        public static string FormatInt32(int value, string format, NumberFormatInfo info)
        {
            return NumberFormatter.NumberToString(format, value, info);
        }


                public static string NumberToString(string format, int value, IFormatProvider fp)
                {
                    NumberFormatter instance = NumberFormatter.GetInstance(fp);
                    instance.Init(format, value, 10);
                    string result = instance.IntegerToString(format, fp);
                    instance.Release();
                    return result;
                }

                    // Token: 0x06001A02 RID: 6658 RVA: 0x00060C24 File Offset: 0x0005EE24
                    private void Init(string format, int value, int defPrecision)
                    {
                        this.Init(format);
                        this._defPrecision = defPrecision;
                        this._positive = (value >= 0);
                        if (value == 0 || this._specifier == 'X')
                        {
                            this.InitHex((ulong)((long)value));
                            return;
                        }
                        if (value < 0)
                        {
                            value = -value;
                        }
                        this.InitDecHexDigits((uint)value);
                        this._decPointPos = (this._digitsLen = this.DecHexLen());
                    }

                    case 'G':
                    if (this._precision <= 0)
                    {
                        return this.FormatDecimal(-1, numberFormatInstance);
                    }
                    return this.FormatGeneral(this._precision, numberFormatInstance);

区分符

  1. format == null|| format.Length == 0 G
  2. format 首字符小写 Upper =false 首字符 - ·a· + ·A·
  3. format 首字符大写 0

StringBuilder源码参考


文章作者: Yonggang Long
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Yonggang Long !
 上一篇
2022-08-10 Yonggang Long
下一篇 
2022-08-10 Yonggang Long
  目录