-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dotnet] [bidi] Revisit some core functionality to deserialize without intermediate JsonElement allocation
#15575
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
[dotnet] [bidi] Revisit some core functionality to deserialize without intermediate JsonElement allocation
#15575
Conversation
PR Reviewer Guide 🔍(Review updated until commit b560945)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to b560945
Previous suggestions✅ Suggestions up to commit 1a4773e
|
||||||||||||||||||||||||||||||
|
@RenderMichael finally ready for review, actually it simplifies the core functionality. Previously I tried to make remote end Please review changes in |
|
Going forward, truly internal stuff. I need it to make improvements further. |
|
CI fails not related to this PR, merging. |
| Utf8JsonReader reader = new(new ReadOnlySpan<byte>(data)); | ||
| reader.Read(); | ||
|
|
||
| reader.Read(); // "{" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.Assert(reader.TokenType == JsonTokenType.StartObject);, to protect against future refactorings?
Or better yet, maybe we should throw in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving fast for now, revisit later as not important for end users.
| case "message": | ||
| message = reader.GetString(); | ||
| break; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default: throw new BiDiException($"Unexpected BiDi response: {Encoding.UTF8.GetString(data)}")?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, we will introduce handling of "unknown polymorphic types"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please post an issue to not forget, for now just moving fast to introduce good pattern for BiDi namespace
| public abstract class Command | ||
| { | ||
| protected Command(string method) | ||
| protected Command(string method, Type resultType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR: what do you think of renaming this type BiDiCommand? It is more clear and easier for namespacing (we already have a Command type)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't like any BiDi* type in BiDi namespace. Namespaces are especially created to resolve collisions.
| public static CommandParameters Empty { get; } = new CommandParameters(); | ||
| } | ||
|
|
||
| public record EmptyResult; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe BaseResult or BiDiResult? Since it is derived by results with values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #15593
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answered there, I prefer just Result
|
|
||
| internal class CloseCommand(CloseCommandParameters @params) | ||
| : Command<CloseCommandParameters>(@params, "browsingContext.close"); | ||
| : Command<CloseCommandParameters, EmptyResult>(@params, "browsingContext.close"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty result is used it mean two things in two different places: base result and void result. The same type serving two different purposes. I propose making this two different types. It will have a real advantage: today, EmptyResult can be assigned a real results value. If we have a separate "no results" type, then it will be guaranteed safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will not have "no results" at all. Any command will return results, read it as public Task<Result> DoSomething().
User description
Core aspects:
MessageSuccess<T>which will handleExtensiblepayload (looking to the future)It is still in "dirty" state, but conceptually it becomes better.
🔗 Related Issues
💥 What does this PR do?
Improve memory allocation. Locating all
divnodes on google.com: before404KB, after317KB.🔧 Implementation Notes
💡 Additional Considerations
🔄 Types of changes
PR Type
Enhancement
Description
Refactored BiDi commands to use strongly-typed results, improving deserialization efficiency.
Introduced
EmptyResultas a base class for command results, simplifying result handling.Enhanced event handling with a new
_eventTypesMapfor dynamic event type resolution.Removed intermediate
JsonElementallocations, optimizing memory usage.Replaced
intwithlongfor command IDs, ensuring compatibility with larger ranges.Changes walkthrough 📝
18 files
Refactored Broker to handle typed command results and eventsAdded result type to Command class for typed resultsUpdated serializer context for new result typesUpdated Message records for typed resultsUpdated CloseCommand to use EmptyResultUpdated CreateUserContextCommand to use typed resultUpdated GetClientWindowsCommand to use typed resultUpdated GetUserContextsCommand to use typed resultUpdated RemoveUserContextCommand to use EmptyResultUpdated ActivateCommand to use EmptyResultUpdated CaptureScreenshotCommand to use typed resultUpdated CloseCommand to use EmptyResultUpdated CreateCommand to use typed resultUpdated GetTreeCommand to use typed resultUpdated HandleUserPromptCommand to use EmptyResultUpdated LocateNodesCommand to use typed resultUpdated NavigateCommand to use typed resultUpdated PrintCommand to use typed result2 files
Removed unused MessageConverter classFixed typo in exception message29 files