0

I am trying to create a csv string from a list of GUIDs (around 10000) of them. My snippet is below.

private string GetAppInformationForCache()
{
    StringBuilder appInfo = new ();
    var expiresString = $"Expires:{ExpirationDateTime:u};";

    appInfo.Append("1:" + string.Join(",", DefaultCatalogAppIds) + ";");
    appInfo.Append("L:" + string.Join(",", PrivateCatalogAppIds) + ";");
    appInfo.Append("3:" + string.Join(",", GlobalCatalogAppIds) + ";");
    appInfo.Append(expiresString);

    return appInfo.ToString();
}

This is in a high traffic API around 30K RPS. I am sure string.Join has some memory allocation concerns in such a high volume. I was asked to change this to a more performant implementation. I could not find a better way to do this except looping throught the guids and appending to resultant string builder.

Is there a more performant way to handle this?

6
  • 2
    Using string.join here essentially negates any performance improvement that the stringbuilder is giving you. If you need performance, generally manually looping values will be faster. Takes a little more work up front, but pays out with a few ms savings on the back-side. Also, if this is cached, it really shouldn't be that concerning either way. Commented Jun 8, 2023 at 3:35
  • can you please explain the last part? if this is cached? I didnt get it Commented Jun 8, 2023 at 3:37
  • 1
    @Nieminen There's a dedicated method equivalent to string.Join that OP can use. Manual looping is just more effort here. Commented Jun 8, 2023 at 3:38
  • 1
    @Nieminen - That's not true to say it "essentially negates any performance". String.Join is fast and using StringBuilder to put those parts together is still a huge saving. Commented Jun 8, 2023 at 4:06
  • 1
    You should also do appInfo.AppendFormat("Expires:{0:u};", ExpirationDateTime); Commented Jun 8, 2023 at 11:26

1 Answer 1

4

You can use AppendJoin:

appInfo.Append("1:");
appInfo.AppendJoin(",", DefaultCatalogAppIds);
appInfo.Append(";");

And so on.

Sign up to request clarification or add additional context in comments.

5 Comments

can you please explain how this is better than what I have now?
@Programmerzzz string.Join constructs a string, interns it, and then adds it to the StringBuilder as a whole string. AppendJoin appends the individual parts instead of first constructing a big string. It's roughly similar to why StringBuilder is considered more efficient than string.Concat or string + string.
While I was having a walk, I realised that my comment might be misinterpreted. I don't want to say that string.Join is as inefficient as string + string can be, rather that with AppendJoin you're not building a big string, only to incorporate it into a bigger string, so you're cutting out a small part of it by skipping that and going directly to AppendJoin. It's also just easier to read because there's less going on.
To be fair, StringBuilder is slower than String.Concat or string + string when the concatenation is done once. It is only beneficial when you are repeatedly concatenating strings.
@Enigmativity That's true. I guess it's like a lot of things where X is more efficient than Y until N reaches a large enough number and then Y becomes the more efficient one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.