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?
string.Jointhat OP can use. Manual looping is just more effort here.String.Joinis fast and usingStringBuilderto put those parts together is still a huge saving.appInfo.AppendFormat("Expires:{0:u};", ExpirationDateTime);