Reputation: 4199
I am getting large volume data over TCP. There are 2 type of XML packets in data. I need to process it as fast as possible.
<?xml version="1.0" encoding="UTF-8"?><xsi:Event> .... [dynamic length data] .... </xsi:Event>
and
<?xml version="1.0" encoding="UTF-8"?><ChannelHeartBeat xmlns="http://schema.broadsoft.com/xsi"/>
Sometime packets doesn't have xml declaration.
This is old code.
char c = (char)streamReader.Read();
sb.Append(c);
if(sb.ToString().EndsWith("</xsi:Event>",StringComparison.OrdinalIgnoreCase))
{
....
sb.Clear();
}
if(sb.ToString().EndsWith("<ChannelHeartBeat xmlns=\"http://schema.broadsoft.com/xsi\"/>", StringComparison.OrdinalIgnoreCase))
{
....
sb.Clear();
}
ToString()
was taking 26% of CPU time in above code.
Below is optimized code. It enhanced performance by 30%
char c = (char)streamReader.Read();
sb.Append(c);
n++;
if (n > 60)
{
if (c == '>')
{
if (n < 105)
{
string temp = sb.ToString();
if (temp.EndsWith("<ChannelHeartBeat xmlns=\"http://schema.broadsoft.com/xsi\"/>", StringComparison.OrdinalIgnoreCase))
{
sb.Clear();
n = 0;
}
}
if (n > 700)
{
string temp = sb.ToString();
if (temp.EndsWith("</xsi:Event>", StringComparison.OrdinalIgnoreCase))
{
sb.Clear();
n = 0;
}
}
}
}
}
ToString()
is now taking 8% of CPU time.
I want to optimize code further. Any suggestion is welcome.
Thanks in advance.
Upvotes: 1
Views: 282
Reputation: 101483
You can try to check if string builder ends with given string using loop and indexer and see if that improves perfomance. For example:
public static class Extensions {
public static bool EndsWith(this StringBuilder sb, string target, bool caseInsensetive = false) {
// if sb length is less than target string
// it cannot end with it
if (sb.Length < target.Length)
return false;
var offset = sb.Length - target.Length;
for (int i = sb.Length - 1; i >= offset; i--) {
var left = sb[i];
var right = target[i - offset];
// conver to upper-case for insensetive comparision
// if necessary
if (caseInsensetive) {
left = Char.ToUpper(left, CultureInfo.InvariantCulture);
right = Char.ToUpper(right, CultureInfo.InvariantCulture);
}
// fail fast
if (left != right)
return false;
}
return true;
}
}
Usage:
const string HeartBeatEnding = "<ChannelHeartBeat xmlns=\"http://schema.broadsoft.com/xsi\"/>";
const string EventEnding = "</xsi:Event>";
char c = (char)streamReader.Read();
sb.Append(c);
if (sb.EndsWith(EventEnding)) {
// do stuff
}
else if (sb.EndsWith(HeartBeatEnding)) {
// do stuff
}
Upvotes: 1