Skip to content

Commit

Permalink
Serialization improvements.
Browse files Browse the repository at this point in the history
Calculate hash of strings (instead of allocating string)
  • Loading branch information
zapov committed Dec 1, 2014
1 parent c8f33c6 commit 58f9bb5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Code/Core/Revenj.Serialization/Json/JsonSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,24 @@ public static int FillName(TextReader sr, char[] buffer, int nextToken)
return (int)hash;
}

public static int CalcHash(TextReader sr, char[] buffer, int nextToken)
{
if (nextToken != '"') throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
nextToken = sr.Read();
char c = (char)nextToken;
int i = 0;
var hash = 0x811C9DC5;
for (; i < buffer.Length && c != '"'; i++, c = (char)sr.Read())
{
buffer[i] = c;
hash = (hash ^ c) * 0x1000193;
}
nextToken = c;
if (i < buffer.Length) buffer[i] = '\0';
if (nextToken != '"') throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
return (int)hash;
}

public static List<T> DeserializeObjectCollection<T>(TextReader sr, int nextToken, Func<T> factory)
{
var res = new List<T>();
Expand Down

0 comments on commit 58f9bb5

Please sign in to comment.