1

I am accessing a shared memory (that is created in C-code) with a program in C#.

The content of the memory mapped file consists of bytes that represent text that has to be processed with C#.

Encoding issues are not the problem here...

Basically the C-Code writes text into the shared memory and increments a write index (at the beginning of the shared memory) to the next write position. At the end of the memory the index wraps around and begins from 0.

To read in C# I maintain a read index (also at the beginning of the shared memory) and as long as both indices are not equal, I am processing the data in C#. Managing these two indices works, and should not be a matter of this question.

C-Code can only write new data as long as the write index does not equal to the read index - because then data would be lost.

In C# I am using a MemoryMappedFile object, and access the bytes with a MemoryMappedViewAccessor object. From this view accessor I get an IntPtr:

This code is an excerpt from my project, it works as expected...

public MemoryMappedFile mmf;
public MemoryMappedViewAccessor mma;
public byte* pointer;
public void Init()
{
    mmf = MemoryMappedFile.OpenExisting("MyFileMappingObject", MemoryMappedFileRights.ReadWrite);
    mma = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite);
    mma.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);

}

I tried to use stream access to the shared memory but this was slower than using unsafe IntPtr variables.

The bytes have to be read out and converted to a C# string. Because I have to do some interpreting and reformatting, i can not simply take all bytes and convert them in a single step to a C# String.

So I end up with many calls of adding a C# string to an existing one:

string zeile = string.Empty;
while (!eof())
{
    string newpart = GetTextFromSharedMemory(); // this works fine
    zeile = zeile + newpart;  // Makes problems
}

If I meassure the time for a certain amount of text to be processed (a single walk through the whole shared memory) , GetTextFromSharedMemory() alone may take around 50 milliseconds. So this is not a bottle neck.

As soon as i add the newpart string to the zeile string my processing time raises up to 5 to 6 seconds ( while I processed about 4 198 528 bytes of data in the shared memory, this roughly 2048 lines of 2048 bytes)

This means 100 times longer than only reading from shared memory.

How can I make these string concatenations more efficient?
I tried to read out the text parts as byte and concatenate lists of bytes together before converting them to strings. This helps a lot, but I would have to work on single bytes for interpreting and reformating...

Allocating and disposing many strings during execution time may trigger the Garbage Collector and this could als have a negative impact on performance.

Because processing time in C# is that "bad", the shared memory eventually gets filled up, and then the C-Code would have to wait for it to get emptied befor adding more data. But this would affect performance of other processes...

1
  • Question as of now is not answerable for what you probably looking for as no code shown that actually have performance problem (as you've already established that nothing related to shared memory is an issue)… So the only answerable part is "How can I make these string concatenations more efficient?" which you probably already bing.com/search?q=c%23+concatenate+string+fast... but for future readers closing as the same as stackoverflow.com/questions/21078/…. Oct 30, 2019 at 23:52

1 Answer 1

3

Use StringBuilder for appending strings. String append operations with simple strings are known to be inefficient because strings are immutable.

StringBuilder zeile = new StringBuilder();
while (!eof())
{
   string newpart = GetTextFromSharedMemory(); // this works fine
   zeile.Append(newpart); 
}

Then use zeile.ToString() to get the string value at the end.

1
  • thank you for this hint. In my test application, using the stringbuilder only added about 20ms to the duration... Oct 31, 2019 at 7:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.