Note that there are some explanatory texts on larger screens.

plurals
  1. POUnsafe string creation from char[]
    primarykey
    data
    text
    <p>I'm working on a high performance code in which this construct is part of the performance critical section.</p> <p>This is what happens in some section:</p> <ol> <li>A <code>string</code> is 'scanned' and metadata is stored efficiently.</li> <li>Based upon this metadata chunks of the main string are separated into a <code>char[][]</code>.</li> <li>That <code>char[][]</code> should be transferred into a <code>string[]</code>.</li> </ol> <p>Now, I know you can just call <code>new string(char[])</code> but then the result would have to be copied.</p> <p>To avoid this extra copy step from happening I guess it must be possible to write directly to the string's internal buffer. Even though this would be an unsafe operation (and I know this bring lots of implications like overflow, forward compatibility).</p> <p>I've seen several ways of achieving this, but none I'm really satisfied with.</p> <p>Does anyone have true suggestions as to how to achieve this?</p> <p><strong>Extra information:</strong><br> The actual process doesn't include converting to <code>char[]</code> necessarily, it's practically a 'multi-substring' operation. Like 3 indexes and their lengths appended.</p> <p>The <code>StringBuilder</code> has too much overhead for the small number of concats.</p> <p><strong>EDIT:</strong><br> Due to some vague aspects of what it is exactly that I'm asking, let me reformulate it.</p> <p>This is what happens:</p> <ol> <li>Main string is indexed.</li> <li>Parts of the main string are copied to a <code>char[]</code>.</li> <li>The <code>char[]</code> is converted to a <code>string</code>.</li> </ol> <p>What I'd like to do is merge step 2 and 3, resulting in:</p> <ol> <li>Main string is indexed.</li> <li>Parts of the main string are copied to a <code>string</code> (and the GC can keep its hands off of it during the process by proper use of the <code>fixed</code> keyword?).</li> </ol> <p>And a note is that I <em>cannot</em> change the output type from string[], since this is an external library, and projects depend on it (backward compatibility).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload