Skip to content

Commit

Permalink
Revert "Perf/Reduce RPC deserialization overhead (#4903)" (#4979)
Browse files Browse the repository at this point in the history
This reverts commit 43a10af.
  • Loading branch information
asdacap committed Dec 8, 2022
1 parent ba92a0a commit be9f8ca
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 407 deletions.
193 changes: 0 additions & 193 deletions src/Nethermind/Nethermind.JsonRpc.Benchmark/Data/samplenewpayload.json

This file was deleted.

117 changes: 0 additions & 117 deletions src/Nethermind/Nethermind.JsonRpc.Benchmark/DeserializeBenchmarks.cs

This file was deleted.

Expand Up @@ -6,18 +6,11 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.15" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Nethermind.Core.Test\Nethermind.Core.Test.csproj" />
<ProjectReference Include="..\Nethermind.JsonRpc\Nethermind.JsonRpc.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Data\samplenewpayload.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
25 changes: 1 addition & 24 deletions src/Nethermind/Nethermind.JsonRpc.Test/RpcTest.cs
Expand Up @@ -5,15 +5,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Castle.Core.Internal;
using FluentAssertions;
using FluentAssertions.Json;
using Nethermind.JsonRpc.Modules;
using Nethermind.JsonRpc.Test.Modules;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace Nethermind.JsonRpc.Test
Expand Down Expand Up @@ -83,27 +80,7 @@ public static JsonRpcRequest GetJsonRequest(string method, params string[] param
{
JsonRpc = "2.0",
Method = method,
Params = parameters?.Select((param) =>
{
if (param is null)
{
return JValue.CreateNull();
}
if (param.IsNullOrEmpty())
{
return JValue.FromObject(param);
}
if (param.StartsWith("{") || param.StartsWith("["))
{
return JToken.ReadFrom(new JsonTextReader(new StringReader(param)));
}
else
{
return JValue.FromObject(param);
}
}).ToArray() ?? Array.Empty<JToken>(),
Params = parameters?.ToArray() ?? Array.Empty<string>(),
Id = 67
};

Expand Down
63 changes: 24 additions & 39 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcProcessor.cs
Expand Up @@ -10,6 +10,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.JsonRpc.Utils;
using Nethermind.Logging;
Expand All @@ -25,10 +26,10 @@ public class JsonRpcProcessor : IJsonRpcProcessor
private JsonSerializer _traceSerializer;
private readonly IJsonRpcConfig _jsonRpcConfig;
private readonly ILogger _logger;
private readonly JsonSerializer _obsoleteBasicJsonSerializer = new();
private readonly IJsonRpcService _jsonRpcService;
private readonly IJsonSerializer _jsonSerializer;
private readonly Recorder _recorder;
private readonly IdConverter _idConverter = new();

public JsonRpcProcessor(IJsonRpcService jsonRpcService, IJsonSerializer jsonSerializer, IJsonRpcConfig jsonRpcConfig, IFileSystem fileSystem, ILogManager logManager)
{
Expand Down Expand Up @@ -77,62 +78,46 @@ private IEnumerable<(JsonRpcRequest Model, List<JsonRpcRequest> Collection)> Des
{
if (token is JArray array)
{
yield return (null, array.Select(JTokenToJsonRpcRequest).ToList());
foreach (JToken tokenElement in array)
{
UpdateParams(tokenElement);
}

yield return (null, array.ToObject<List<JsonRpcRequest>>(_obsoleteBasicJsonSerializer));
}
else
{
yield return (JTokenToJsonRpcRequest(token), null);
UpdateParams(token);
yield return (token.ToObject<JsonRpcRequest>(_obsoleteBasicJsonSerializer), null);
}
}
}

/// <summary>
/// Manually convert JToken to JsonRpcRequests as the param inside JsonRpcRequests is a JToken. If we use
/// a JTokenReader, it would iterate through the JSON which is a waste of time, as we already have a JToken.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private JsonRpcRequest JTokenToJsonRpcRequest(JToken token)
private void UpdateParams(JToken token)
{
JsonRpcRequest jsonRpcRequest = new();

JToken? jsonRpcField = token["jsonrpc"] ?? token["JsonRpc"];
if (jsonRpcField != null)
{
jsonRpcRequest.JsonRpc = jsonRpcField.Value<string>();
}

JToken? methodField = token["method"] ?? token["Method"];
if (methodField != null)
var paramsToken = token.SelectToken("params");
if (paramsToken is null)
{
jsonRpcRequest.Method = methodField.Value<string>();
paramsToken = token.SelectToken("Params");
if (paramsToken is null)
{
return;
}
}

JToken? idField = token["id"] ?? token["Id"];
if (idField != null)
if (paramsToken is JValue)
{
// Not sure what is the logic here.. not gonna unuse the IdConverter
JTokenReader reader = new(idField);
if (reader.Read())
{
jsonRpcRequest.Id = _idConverter.ReadJson(reader, null, null, null);
}
return; // null
}

JToken? paramsField = token["params"] ?? token["Params"];
if (paramsField != null)
JArray arrayToken = (JArray)paramsToken;
for (int i = 0; i < arrayToken.Count; i++)
{
if (paramsField is JArray asArray)
if (arrayToken[i].Type == JTokenType.Array || arrayToken[i].Type == JTokenType.Object)
{
jsonRpcRequest.Params = asArray.ToArray();
}
else
{
throw new JsonSerializationException($"Params is expected to be an array. Got {paramsField.Type}");
arrayToken[i].Replace(JToken.Parse(_jsonSerializer.Serialize(arrayToken[i].Value<object>().ToString())));
}
}

return jsonRpcRequest;
}

public async IAsyncEnumerable<JsonRpcResult> ProcessAsync(TextReader request, JsonRpcContext context)
Expand Down
6 changes: 2 additions & 4 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcRequest.cs
@@ -1,10 +1,8 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Linq;
using Nethermind.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Nethermind.JsonRpc
{
Expand All @@ -14,14 +12,14 @@ public class JsonRpcRequest
public string Method { get; set; }

[JsonProperty(Required = Required.Default)]
public JToken[]? Params { get; set; }
public string[]? Params { get; set; }

[JsonConverter(typeof(IdConverter))]
public object Id { get; set; }

public override string ToString()
{
string paramsString = Params is null ? string.Empty : $"{string.Join(",", Params.Select(p => p.ToString()))}";
string paramsString = Params is null ? string.Empty : $"{string.Join(",", Params)}";
return $"ID {Id}, {Method}({paramsString})";
}
}
Expand Down

0 comments on commit be9f8ca

Please sign in to comment.