-
Notifications
You must be signed in to change notification settings - Fork 640
Cache PropertyInfo lookups for JsonRpc #8976
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
|
|
||
| namespace Nethermind.Core.Collections; | ||
|
|
||
| public readonly struct TypeAsKey(Type key) : IEquatable<TypeAsKey> | ||
| { | ||
| private readonly Type _key = key; | ||
|
|
||
| public static implicit operator Type(TypeAsKey key) => key._key; | ||
| public static implicit operator TypeAsKey(Type key) => new(key); | ||
|
|
||
| public bool Equals(TypeAsKey other) => ReferenceEquals(_key, other._key); | ||
| public override int GetHashCode() => _key?.GetHashCode() ?? 0; | ||
| public override bool Equals(object? obj) => obj is TypeAsKey && Equals((TypeAsKey)obj); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,10 @@ | |
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Nethermind.Core; | ||
| using Nethermind.Core.Collections; | ||
| using Nethermind.Core.Threading; | ||
| using Nethermind.JsonRpc.Exceptions; | ||
| using Nethermind.JsonRpc.Modules; | ||
|
|
@@ -27,8 +29,11 @@ public class JsonRpcService : IJsonRpcService | |
| private readonly ILogger _logger; | ||
| private readonly IRpcModuleProvider _rpcModuleProvider; | ||
| private readonly HashSet<string> _methodsLoggingFiltering; | ||
| private readonly Lock _propertyInfoModificationLock = new(); | ||
| private readonly int _maxLoggedRequestParametersCharacters; | ||
|
|
||
| private Dictionary<TypeAsKey, PropertyInfo?> _propertyInfoCache = []; | ||
|
|
||
| public JsonRpcService(IRpcModuleProvider rpcModuleProvider, ILogManager logManager, IJsonRpcConfig jsonRpcConfig) | ||
| { | ||
| _logger = logManager.GetClassLogger<JsonRpcService>(); | ||
|
|
@@ -196,7 +201,7 @@ private async Task<JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string | |
| break; | ||
| case Task task: | ||
| await task; | ||
| resultWrapper = task.GetType().GetProperty("Result")?.GetValue(task) as IResultWrapper; | ||
| resultWrapper = GetResultProperty(task)?.GetValue(task) as IResultWrapper; | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -244,6 +249,42 @@ private async Task<JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string | |
| : GetSuccessResponse(methodName, resultWrapper.Data, request.Id, returnAction); | ||
| } | ||
|
|
||
| private PropertyInfo? GetResultProperty(Task task) | ||
| { | ||
| Type type = task.GetType(); | ||
| if (_propertyInfoCache.TryGetValue(type, out PropertyInfo? value)) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| return GetResultPropertySlow(type); | ||
| } | ||
|
|
||
| private PropertyInfo? GetResultPropertySlow(Type type) | ||
| { | ||
| lock (_propertyInfoModificationLock) | ||
| { | ||
| Dictionary<TypeAsKey, PropertyInfo?> current = _propertyInfoCache; | ||
| // Re-check inside the lock in case another thread already added it | ||
| if (current.TryGetValue(type, out PropertyInfo? value)) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| // Copy-on-write: create a new dictionary so we don't mutate | ||
| // the one other threads may be reading without locks. | ||
| Dictionary<TypeAsKey, PropertyInfo?> propertyInfoCache = new(current); | ||
| PropertyInfo? propertyInfo = type.GetProperty("Result"); | ||
| propertyInfoCache[type] = propertyInfo; | ||
|
Comment on lines
+276
to
+278
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe do frozendictionary then? |
||
|
|
||
| // Publish the new cache instance atomically by swapping the reference. | ||
| // Readers grabbing _propertyInfoCache will now see the updated dictionary. | ||
| _propertyInfoCache = propertyInfoCache; | ||
|
|
||
| return propertyInfo; | ||
| } | ||
| } | ||
|
|
||
| private void LogRequest(string methodName, JsonElement providedParameters, ExpectedParameter[] expectedParameters) | ||
| { | ||
| if (_logger.IsTrace && !_methodsLoggingFiltering.Contains(methodName)) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.