Skip to content

Commit

Permalink
fix: fix message parser performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Bacis committed Jun 27, 2023
1 parent e8c56db commit b97bf7b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions WeArtClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ private bool ReceiveMessages(out IWeArtMessage[] messages)
messages = new IWeArtMessage[split.Length];
for (int i = 0; i < messages.Length; i++)
{
_messageSerializer.Deserialize(split[i], out messages[i]);
ForwardMessage(split[i], messages[i]);
if(_messageSerializer.Deserialize(split[i], out messages[i]))
ForwardMessage(split[i], messages[i]);
}
return true;
}
Expand Down
65 changes: 46 additions & 19 deletions WeArtMessageCustomSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override bool Serialize(IWeArtMessage message, out string data)

if (message is WeArtJsonMessage jsonMessage)
{
data = SerializeJsonMessage(reflectionData, jsonMessage);
data = SerializeJsonMessage(reflectionData, jsonMessage);
return true;
}

Expand Down Expand Up @@ -105,35 +105,52 @@ public override bool Deserialize(string data, out IWeArtMessage message)
{
try
{
string[] split = data.Split(separator);
var messageID = split[0];
if (IsJsonMessage(data))
message = DeserializeJsonMessage(data);
else
message = DeserializeCsvMessage(data);
}
catch (Exception)
{
message = null;
}
return message != null;
}

var reflectionData = ReflectionData.GetFrom(messageID);
private bool IsJsonMessage(string data)
{
// Heuristic to see fast (without regex or parsing) if the data we received could be a json message
// Old protocol messages does not start with "[" or "{" so it should be ok
if (string.IsNullOrWhiteSpace(data)) return false;
data = data.Trim();

bool isObject = data.StartsWith("{") && data.EndsWith("}");
bool isArray = data.StartsWith("[") && data.EndsWith("]");
return isObject || isArray;
}

message = (IWeArtMessage)Activator.CreateInstance(reflectionData.Type);
private IWeArtMessage DeserializeCsvMessage(string data)
{
string[] split = data.Split(separator);
var messageID = split[0];

if (message is IWeArtMessageCustomSerialization deserializableMessage)
return deserializableMessage.Deserialize(split);
var reflectionData = ReflectionData.GetFrom(messageID);
var message = (IWeArtMessage)Activator.CreateInstance(reflectionData.Type);

if (message is IWeArtMessageCustomSerialization deserializableMessage)
{
deserializableMessage.Deserialize(split);
}
else
{
for (int i = 1; i < split.Length; i++)
{
var field = reflectionData.Fields[i - 1];
var value = Deserialize(split[i], field.FieldType);
field.SetValue(message, value);
}
return true;
}
catch (MessageTypeNotFound)
{
// Try with json
message = DeserializeJsonMessage(data);
return message != null;
}
catch (Exception)
{
message = null;
return false;
}
return message;
}

private IWeArtMessage DeserializeJsonMessage(string data)
Expand All @@ -142,6 +159,16 @@ private IWeArtMessage DeserializeJsonMessage(string data)
{
JsonMessageTemplate json = JsonConvert.DeserializeObject<JsonMessageTemplate>(data);
ReflectionData reflectionData = ReflectionData.GetFrom(json.Type);

// Message without parameters
if (json.Data is null)
{
var message = (WeArtJsonMessage)Activator.CreateInstance(reflectionData.Type);
message.Timestamp = json.Timestamp;
return message;
}

// Message with parameters
return (IWeArtMessage)json.Data.ToObject(reflectionData.Type) ?? null;
}
catch (MessageTypeNotFound)
Expand Down

0 comments on commit b97bf7b

Please sign in to comment.