String.Concat低效code? [英] String.Concat inefficient code?

查看:178
本文介绍了String.Concat低效code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查String.Concat:(反射)

很奇怪:

的有值数组,

他们创建一个新的数组,后来他们把他送到 ConcatArray

问:

为什么他们创建一个阵列?他们有从第一个地方...

修改

code:

 公共静态字符串连接(PARAMS字符串[]值)
{
    如果(价值== NULL)
    {
        抛出新ArgumentNullException(值);
    }
    INT totalLength = 0;
    字符串[] strArray =新的字符串[values​​.Length]
    的for(int i = 0; I< values​​.Length;我++)
    {
        字符串str =值[I]
        strArray [I] =(STR == NULL)?空:STR;
        totalLength + = strArray [I] .Length;
        如果(totalLength℃,)
        {
            抛出新OutOfMemoryException异常();
        }
    }
    返回ConcatArray(strArray,totalLength);
}
 

解决方案

好了一件事,这意味着新的数组的内容可以被信任是非空....不变。

如果没有这种复制,另一个线程的可以的调用 ConcatArray ,其中presumably会抛出异常时修改原始数组或甚至引发安全漏洞。该拷贝,输入数组可以在任何时候被改变 - 的每个元素将被精确地一次读取,所以不可能有不一致的情况。 (结果可能是旧的和新元素的混合,但你不会落得内存损坏。)

假设 ConcatArray 是可信执行批量复制出来它是通过数组中的字符串,不检查缓冲区溢出。然后如果更改输入数组在适当的时候,你可能最终分配的内存之外写作。不良。有了这个防守副本,该系统可以确保 1 ,总长度确实是总长度。


1 好了,除非反射用来改变一个字符串的内容。但是,这并非没有相当高的权限来完成 - 而更改数组中的内容很容易

I was investigating String.Concat : (Reflector)

very strange :

the have the values array ,

they creating a NEW ARRAY for which later they send him to ConcatArray.

Question :

Why they created a new array ? they had values from the first place...

edit

code :

public static string Concat(params string[] values)
{
    if (values == null)
    {
        throw new ArgumentNullException("values");
    }
    int totalLength = 0;
    string[] strArray = new string[values.Length];
    for (int i = 0; i < values.Length; i++)
    {
        string str = values[i];
        strArray[i] = (str == null) ? Empty : str;
        totalLength += strArray[i].Length;
        if (totalLength < 0)
        {
            throw new OutOfMemoryException();
        }
    }
    return ConcatArray(strArray, totalLength);
}

解决方案

Well for one thing, it means that the contents of the new array can be trusted to be non-null.... and unchanging.

Without that copying, another thread could modify the original array during the call to ConcatArray, which presumably could throw an exception or even trigger a security bug. With the copying, the input array can be changed at any time - each element will be read exactly once, so there can be no inconsistency. (The result may be a mixture of old and new elements, but you won't end up with memory corruption.)

Suppose ConcatArray is trusted to do bulk copying out of the strings in the array it's passed, without checking for buffer overflow. Then if you change the input array at just the right time, you could end up writing outside the allocated memory. Badness. With this defensive copy, the system can be sure1 that the total length really is the total length.


1 Well, unless reflection is used to change the contents of a string. But that can't be done without fairly high permissions - whereas changing the contents of an array is easy.

这篇关于String.Concat低效code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆