Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few minor performance refactorings #84

Merged
merged 4 commits into from Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions AustinHarris.JsonRpcTestN/Test.cs
Expand Up @@ -79,14 +79,14 @@ public void TestCanCreateAndRemoveSession()

var actual1 = JObject.Parse(result.Result);
var expected1 = JObject.Parse(expectedResult);
Assert.AreEqual(expected1, actual1);
Assert.IsTrue(JToken.DeepEquals(expected1, actual1));

h.Destroy();

var result2 = JsonRpcProcessor.Process("this one", request);
result2.Wait();

Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)));
}

[Test()]
Expand Down Expand Up @@ -173,7 +173,7 @@ public void StringToRefException()
string expectedResult = "{\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"refException worked\",\"code\":-1,\"data\":null},\"id\":1}";
var result = JsonRpcProcessor.Process(request);
result.Wait();
Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result));
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResult), JObject.Parse(result.Result)));
}

[Test()]
Expand Down Expand Up @@ -1581,7 +1581,7 @@ public void TestPreProcessOnSession()

var actual1 = JObject.Parse(result.Result);
var expected1 = JObject.Parse(expectedResult);
Assert.AreEqual(expected1, actual1);
Assert.IsTrue(JToken.DeepEquals(expected1, actual1));
Assert.AreEqual(1, preHandler.run);

h.Destroy();
Expand All @@ -1590,7 +1590,7 @@ public void TestPreProcessOnSession()
result2.Wait();

Assert.AreEqual(1, preHandler.run);
Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)));
}

class PostProcessHandlerLocal
Expand Down Expand Up @@ -1821,7 +1821,7 @@ public void TestPostProcessOnSession()

var actual1 = JObject.Parse(result.Result);
var expected1 = JObject.Parse(expectedResult);
Assert.AreEqual(expected1, actual1);
Assert.IsTrue(JToken.DeepEquals(expected1, actual1));
Assert.AreEqual(1, postHandler.run);

h.Destroy();
Expand All @@ -1830,7 +1830,7 @@ public void TestPostProcessOnSession()
result2.Wait();

Assert.AreEqual(1, postHandler.run);
Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)));
}

[Test()]
Expand Down
76 changes: 33 additions & 43 deletions Json-Rpc/Handler.cs
Expand Up @@ -14,13 +14,14 @@
public class Handler
{
#region Members

private const string Name_of_JSONRPCEXCEPTION = "JsonRpcException&";
private static int _sessionHandlerMasterVersion = 1;
[ThreadStatic]
private static Dictionary<string, Handler> _sessionHandlersLocal;
[ThreadStatic]
private static int _sessionHandlerLocalVersion = 0;
private static ConcurrentDictionary<string, Handler> _sessionHandlersMaster;
[ThreadStatic]
private static Dictionary<string, Handler> _sessionHandlersLocal;

private static volatile string _defaultSessionId;
#endregion

Expand Down Expand Up @@ -212,13 +213,10 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)

SMDService metadata = null;
Delegate handle = null;
var haveMetadata = this.MetaData.Services.TryGetValue(Rpc.Method, out metadata);
if (haveMetadata)
if (this.MetaData.Services.TryGetValue(Rpc.Method, out metadata))
{
handle = metadata.dele;
}

if (haveMetadata == false || metadata == null)
} else if (metadata == null)
{
JsonResponse response = new JsonResponse()
{
Expand All @@ -229,53 +227,45 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
return PostProcess(Rpc, response, RpcContext);
}

bool isJObject = Rpc.Params is Newtonsoft.Json.Linq.JObject;
bool isJArray = Rpc.Params is Newtonsoft.Json.Linq.JArray;
object[] parameters = null;
bool expectsRefException = false;
var metaDataParamCount = metadata.parameters.Count(x => x != null);

var getCount = Rpc.Params as ICollection;

var loopCt = 0;

var getCount = Rpc.Params as ICollection;
if (getCount != null)
{
loopCt = getCount.Count;
}

var paramCount = loopCt;
if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Contains(typeof(JsonRpcException).Name))
if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Equals(Name_of_JSONRPCEXCEPTION))
{
paramCount++;
expectsRefException = true;
}
parameters = new object[paramCount];

if (isJArray)
if (Rpc.Params is Newtonsoft.Json.Linq.JArray)
{
var jarr = ((Newtonsoft.Json.Linq.JArray)Rpc.Params);
//var loopCt = jarr.Count;
//var pCount = loopCt;
//if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
// pCount++;
//parameters = new object[pCount];
for (int i = 0; i < loopCt; i++)
{
parameters[i] = CleanUpParameter(jarr[i], metadata.parameters[i]);
}
}
else if (isJObject)
else if (Rpc.Params is Newtonsoft.Json.Linq.JObject)
{
var jo = Rpc.Params as Newtonsoft.Json.Linq.JObject;
//var loopCt = jo.Count;
//var pCount = loopCt;
//if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
// pCount++;
//parameters = new object[pCount];
var asDict = jo as IDictionary<string, Newtonsoft.Json.Linq.JToken>;
var asDict = Rpc.Params as IDictionary<string, Newtonsoft.Json.Linq.JToken>;
for (int i = 0; i < loopCt && i < metadata.parameters.Length; i++)
{
if (asDict.ContainsKey(metadata.parameters[i].Name) == false)
if (asDict.ContainsKey(metadata.parameters[i].Name) == true)
{
parameters[i] = CleanUpParameter(asDict[metadata.parameters[i].Name], metadata.parameters[i]);
continue;
}
else
{
JsonResponse response = new JsonResponse()
{
Expand All @@ -289,7 +279,6 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
};
return PostProcess(Rpc, response, RpcContext);
}
parameters[i] = CleanUpParameter(jo[metadata.parameters[i].Name], metadata.parameters[i]);
}
}

Expand Down Expand Up @@ -348,17 +337,20 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)

var last = parameters.LastOrDefault();
var contextException = RpcGetAndRemoveRpcException();
JsonResponse response = null;
if (contextException != null)
{
JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id };
return PostProcess(Rpc, response, RpcContext);
response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id };
}
if (expectsRefException && last != null && last is JsonRpcException)
else if (expectsRefException && last != null && last is JsonRpcException)
{
JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id };
return PostProcess(Rpc, response, RpcContext);
response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id };
}
return PostProcess(Rpc, new JsonResponse() { Result = results }, RpcContext);
else
{
response = new JsonResponse() { Result = results };
}
return PostProcess(Rpc, response, RpcContext);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -431,13 +423,13 @@ internal void SetParseErrorHandler(Func<string, JsonRpcException, JsonRpcExcepti
private object CleanUpParameter(object p, SMDAdditionalParameters metaData)
{
var bob = p as JValue;
//if (bob != null && (bob.Value == null || bob.Value.GetType() == metaData.ObjectType))
if (bob != null && (bob.Value == null))
{
return bob.Value;
}

if (bob != null)
{
if (bob.Value == null || metaData.ObjectType == bob.Value.GetType())
{
return bob.Value;
}

// Avoid calling DeserializeObject on types that JValue has an explicit converter for
// try to optimize for the most common types
Expand Down Expand Up @@ -492,9 +484,7 @@ private object CleanUpParameter(object p, SMDAdditionalParameters metaData)

private JsonRpcException PreProcess(JsonRequest request, object context)
{
if (externalPreProcessingHandler == null)
return null;
return externalPreProcessingHandler(request, context);
return externalPreProcessingHandler == null ? null : externalPreProcessingHandler(request, context);
}

private JsonResponse PostProcess(JsonRequest request, JsonResponse response, object context)
Expand Down