简体   繁体   English

stackalloc(C#参考)

[英]stackalloc (C# Reference)

i'm trying to parse 130,000 document , and i'm trying to do that as fast as i can. 我正在尝试解析130,000个文档,并且我试图尽快完成该操作。

this function is for removing the delimiter char in Document. 此功能用于删除Document中的定界符char。

public static unsafe string StripRestAndNewlines(string s)
{
    int len = s.Length;
    char* newChars = stackalloc char[len];
    char* currentChar = newChars;

    for (int i = 0; i < len; ++i)
    {
        char c = s[i];
        switch (c)
        {
            case ',':
            case '.':
            case ':':
            case ';':
            case '-':
            case '>':
            case '<':
            case '/':
            case '\\':
            case '?':
            case '"':
            case '*':
            case '&':
            case '_':
            case '+':
            case '@':
            case '[':
            case ']':
            case '!':
            case '=':
            case '%':
            case '#':
                continue;
            default:
                *currentChar++ = c;
                break;
        }
    }
  return new string(newChars, 0, (int)(currentChar - newChars));            
}

but after 2 min of running the program stop and i'm getting 但是在运行程序2分钟后,我停了下来

system.StackOverflowException system.StackOverflowException

is there any delete[] of free for the allocate? 是否有免费的delete []用于分配?

thanks! 谢谢!

is there any delete[] of free for the allocate? 是否有免费的delete []用于分配?

Yes, doing nothing. 是的,什么也不做。 Since its stack-allocated it will be immediately deleted as soon as the method returns (your mentioning delete[] suggests you are drawing analogy to C++, but note that in C++ you don't delete[] stack-allocated variables). 由于其堆栈已分配,因此方法返回后将立即删除它(您提到的delete[]建议您与C ++类似,但请注意,在C ++中,您不会delete[]堆栈已分配变量)。

You won't get that far though, because you are stack-allocating too much. 但是,您不会走那么远,因为您的堆栈分配过多。

stackalloc is of very limited use. stackalloc的用途非常有限。 It tends to be slower than just using heap memory unless you are using it as an alternative to fixed or in a few situations where different threads are allow allocating large arrays at the same time. 它往往比仅使用堆内存要慢,除非您将其用作fixed内存的替代方法,或者在不同线程允许同时分配大数组的少数情况下使用。 It's only appropriate for use arrays smaller than a few kilobytes at the outside. 仅适用于外部小于几千字节的阵列。

You're going to be better off using a heap array. 使用堆数组会更好。 You may or may not be better off using pointers and fixed . 使用指针和fixed可能会更好,也可能不会更好。

You'd be much, much better off parsing the document(s) in chunks. 将文档分块地解析会好得多。 If at all possible load them in from streams only in moderate-sized segments of 4kiB or 8kiB and process each such chunk as it comes. 如果可能的话,仅从4kiB或8kiB的中等大小的段中的流中加载它们,并处理每个这样的块。

There is no delete method I think. 我认为没有删除方法。 It's a stack, you can add something on top, and remove things from top. 它是一个堆栈,您可以在顶部添加内容,也可以从顶部删除内容。 You cannot remove things from middle of stack. 您不能从堆栈中间删除内容。 Allocated memory is automatically freed when method returns. 方法返回时,分配的内存将自动释放。 I think stackoverflow occurs when incoming string is very long. 我认为当输入字符串很长时会发生stackoverflow。 Use heap memory to this task. 使用堆内存执行此任务。 Just create a new array. 只需创建一个新数组即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM