I'm working on a high performance code in which this construct is part of the performance critical section.
This is what happens in some section:
- A
string
is 'scanned' and metadata is stored efficiently. - Based upon this metadata chunks of the main string are separated into a
char[][]
. - That
char[][]
should be transferred into astring[]
.
Now, I know you can just call new string(char[])
but then the result would have to be copied.
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).
I've seen several ways of achieving this, but none I'm really satisfied with.
Does anyone have true suggestions as to how to achieve this?
Extra information:
The actual process doesn't include converting to char[]
necessarily, it's practically a 'multi-substring' operation. Like 3 indexes and their lengths appended.
The StringBuilder
has too much overhead for the small number of concats.
EDIT:
Due to some vague aspects of what it is exactly that I'm asking, let me reformulate it.
This is what happens:
- Main string is indexed.
- Parts of the main string are copied to a
char[]
. - The
char[]
is converted to astring
.
What I'd like to do is merge step 2 and 3, resulting in:
- Main string is indexed.
- Parts of the main string are copied to a
string
(and the GC can keep its hands off of it during the process by proper use of thefixed
keyword?).
And a note is that I cannot change the output type from string[], since this is an external library, and projects depend on it (backward compatibility).