Skip to content

Commit 0056119

Browse files
committed
Minor performance tweaks
1 parent 73910aa commit 0056119

2 files changed

Lines changed: 39 additions & 59 deletions

File tree

Json-Rpc/Handler.cs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
public class Handler
1515
{
1616
#region Members
17-
17+
private const string Name_of_JSONRPCEXCEPTION = "JsonRpcException&";
1818
private static int _sessionHandlerMasterVersion = 1;
1919
[ThreadStatic]
20+
private static Dictionary<string, Handler> _sessionHandlersLocal;
21+
[ThreadStatic]
2022
private static int _sessionHandlerLocalVersion = 0;
2123
private static ConcurrentDictionary<string, Handler> _sessionHandlersMaster;
22-
[ThreadStatic]
23-
private static Dictionary<string, Handler> _sessionHandlersLocal;
24+
2425
private static volatile string _defaultSessionId;
2526
#endregion
2627

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

213214
SMDService metadata = null;
214215
Delegate handle = null;
215-
var haveMetadata = this.MetaData.Services.TryGetValue(Rpc.Method, out metadata);
216-
if (haveMetadata)
216+
if (this.MetaData.Services.TryGetValue(Rpc.Method, out metadata))
217217
{
218218
handle = metadata.dele;
219-
}
220-
221-
if (haveMetadata == false || metadata == null)
219+
} else if (metadata == null)
222220
{
223221
JsonResponse response = new JsonResponse()
224222
{
@@ -229,53 +227,45 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
229227
return PostProcess(Rpc, response, RpcContext);
230228
}
231229

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

238-
var getCount = Rpc.Params as ICollection;
234+
239235
var loopCt = 0;
240-
236+
var getCount = Rpc.Params as ICollection;
241237
if (getCount != null)
242238
{
243239
loopCt = getCount.Count;
244240
}
245241

246242
var paramCount = loopCt;
247-
if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Contains(typeof(JsonRpcException).Name))
243+
if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Equals(Name_of_JSONRPCEXCEPTION))
248244
{
249245
paramCount++;
250246
expectsRefException = true;
251247
}
252248
parameters = new object[paramCount];
253249

254-
if (isJArray)
250+
if (Rpc.Params is Newtonsoft.Json.Linq.JArray)
255251
{
256252
var jarr = ((Newtonsoft.Json.Linq.JArray)Rpc.Params);
257-
//var loopCt = jarr.Count;
258-
//var pCount = loopCt;
259-
//if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
260-
// pCount++;
261-
//parameters = new object[pCount];
262253
for (int i = 0; i < loopCt; i++)
263254
{
264255
parameters[i] = CleanUpParameter(jarr[i], metadata.parameters[i]);
265256
}
266257
}
267-
else if (isJObject)
258+
else if (Rpc.Params is Newtonsoft.Json.Linq.JObject)
268259
{
269-
var jo = Rpc.Params as Newtonsoft.Json.Linq.JObject;
270-
//var loopCt = jo.Count;
271-
//var pCount = loopCt;
272-
//if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException))
273-
// pCount++;
274-
//parameters = new object[pCount];
275-
var asDict = jo as IDictionary<string, Newtonsoft.Json.Linq.JToken>;
260+
var asDict = Rpc.Params as IDictionary<string, Newtonsoft.Json.Linq.JToken>;
276261
for (int i = 0; i < loopCt && i < metadata.parameters.Length; i++)
277262
{
278-
if (asDict.ContainsKey(metadata.parameters[i].Name) == false)
263+
if (asDict.ContainsKey(metadata.parameters[i].Name) == true)
264+
{
265+
parameters[i] = CleanUpParameter(asDict[metadata.parameters[i].Name], metadata.parameters[i]);
266+
continue;
267+
}
268+
else
279269
{
280270
JsonResponse response = new JsonResponse()
281271
{
@@ -289,7 +279,6 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
289279
};
290280
return PostProcess(Rpc, response, RpcContext);
291281
}
292-
parameters[i] = CleanUpParameter(jo[metadata.parameters[i].Name], metadata.parameters[i]);
293282
}
294283
}
295284

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

349338
var last = parameters.LastOrDefault();
350339
var contextException = RpcGetAndRemoveRpcException();
340+
JsonResponse response = null;
351341
if (contextException != null)
352342
{
353-
JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id };
354-
return PostProcess(Rpc, response, RpcContext);
343+
response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id };
355344
}
356-
if (expectsRefException && last != null && last is JsonRpcException)
345+
else if (expectsRefException && last != null && last is JsonRpcException)
357346
{
358-
JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id };
359-
return PostProcess(Rpc, response, RpcContext);
347+
response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id };
348+
}
349+
else
350+
{
351+
response = new JsonResponse() { Result = results };
360352
}
361-
return PostProcess(Rpc, new JsonResponse() { Result = results }, RpcContext);
353+
return PostProcess(Rpc, response, RpcContext);
362354
}
363355
catch (Exception ex)
364356
{
@@ -492,9 +484,7 @@ private object CleanUpParameter(object p, SMDAdditionalParameters metaData)
492484

493485
private JsonRpcException PreProcess(JsonRequest request, object context)
494486
{
495-
if (externalPreProcessingHandler == null)
496-
return null;
497-
return externalPreProcessingHandler(request, context);
487+
return externalPreProcessingHandler == null ? null : externalPreProcessingHandler(request, context);
498488
}
499489

500490
private JsonResponse PostProcess(JsonRequest request, JsonResponse response, object context)

TestServer_Console/Program.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using AustinHarris.JsonRpc;
66
using System.Threading;
77
using System.Diagnostics;
8+
using System.Threading.Tasks;
89

910
namespace TestServer_Console
1011
{
@@ -47,38 +48,27 @@ private static void Benchmark()
4748
{
4849
Console.WriteLine("Starting benchmark");
4950

50-
var cnt = 40;
51+
var cnt = 50;
5152
var iterations = 7;
5253
for (int iteration = 1; iteration <= iterations; iteration++)
5354
{
5455
cnt *= iteration;
5556
ctr = 0;
57+
Task<string>[] tasks = new Task<string>[cnt];
5658
var sw = Stopwatch.StartNew();
57-
AutoResetEvent are = new AutoResetEvent(false);
58-
59-
var fn = new Action<System.Threading.Tasks.Task<String>>(_ => {
60-
if(Interlocked.Increment(ref ctr) == cnt)
61-
{
62-
sw.Stop();
63-
Console.WriteLine("processed {0} rpc in {1}ms for {2} rpc/sec",cnt,sw.ElapsedMilliseconds, (double)cnt * 1000d / sw.ElapsedMilliseconds);
64-
are.Set();
65-
}
66-
});
6759

6860
var sessionid = Handler.DefaultSessionId();
6961
for (int i = 0; i < cnt; i+=5)
7062
{
71-
JsonRpcProcessor.Process(sessionid, "{'method':'add','params':[1,2],'id':1}").ContinueWith(fn);
72-
73-
JsonRpcProcessor.Process(sessionid, "{'method':'addInt','params':[1,7],'id':2}").ContinueWith(fn);
74-
75-
JsonRpcProcessor.Process(sessionid, "{'method':'NullableFloatToNullableFloat','params':[1.23],'id':3}").ContinueWith(fn);
76-
77-
JsonRpcProcessor.Process(sessionid, "{'method':'Test2','params':[3.456],'id':4}").ContinueWith(fn);
78-
79-
JsonRpcProcessor.Process(sessionid, "{'method':'StringMe','params':['Foo'],'id':5}").ContinueWith(fn);
63+
tasks[i] = JsonRpcProcessor.Process(sessionid, "{'method':'add','params':[1,2],'id':1}");
64+
tasks[i+1] = JsonRpcProcessor.Process(sessionid, "{'method':'addInt','params':[1,7],'id':2}");
65+
tasks[i+2] = JsonRpcProcessor.Process(sessionid, "{'method':'NullableFloatToNullableFloat','params':[1.23],'id':3}");
66+
tasks[i+3] = JsonRpcProcessor.Process(sessionid, "{'method':'Test2','params':[3.456],'id':4}");
67+
tasks[i+4] = JsonRpcProcessor.Process(sessionid, "{'method':'StringMe','params':['Foo'],'id':5}");
8068
}
81-
are.WaitOne();
69+
Task.WaitAll(tasks);
70+
sw.Stop();
71+
Console.WriteLine("processed {0} rpc in {1}ms for {2} rpc/sec", cnt, sw.ElapsedMilliseconds, (double)cnt * 1000d / sw.ElapsedMilliseconds);
8272
}
8373

8474

0 commit comments

Comments
 (0)