Skip to content

[dotnet] [bidi] Optimize processing of incoming messages#17206

Merged
nvborisenko merged 5 commits intoSeleniumHQ:trunkfrom
nvborisenko:bidi-process-message
Mar 11, 2026
Merged

[dotnet] [bidi] Optimize processing of incoming messages#17206
nvborisenko merged 5 commits intoSeleniumHQ:trunkfrom
nvborisenko:bidi-process-message

Conversation

@nvborisenko
Copy link
Member

Refactors the ProcessReceivedMessage method in Broker.cs to improve the parsing logic for JSON properties and to fix type inconsistencies relating to parameter indices. The changes streamline property handling and ensure correct indexing for extracting parameter data.

🔄 Types of changes

  • Cleanup (formatting, renaming)
  • Bug fix (backwards compatible)

Copilot AI review requested due to automatic review settings March 11, 2026 13:35
@qodo-code-review
Copy link
Contributor

Review Summary by Qodo

Optimize BiDi message processing with improved property parsing

🐞 Bug fix ✨ Enhancement

Grey Divider

Walkthroughs

Description
• Refactored property parsing from switch statement to if-else chain using ValueTextEquals()
• Changed paramsStartIndex and paramsEndIndex from long to int for type consistency
• Optimized JSON reader initialization by removing unnecessary ReadOnlySpan<byte> wrapper
• Fixed parameter index calculation to correctly capture token positions

Grey Divider

File Changes

1. dotnet/src/webdriver/BiDi/Broker.cs 🐞 Bug fix +41/-39

Refactor message parsing with optimized property matching

• Replaced switch statement with if-else chain using ValueTextEquals() for UTF-8 property name
 comparison
• Changed paramsStartIndex and paramsEndIndex from long to int type for consistency with
 TokenStartIndex and BytesConsumed
• Simplified Utf8JsonReader initialization by passing data directly instead of wrapping in
 ReadOnlySpan<byte>
• Refactored parameter index tracking logic to correctly capture start and end positions using
 isParams flag
• Removed unnecessary casts in ReadOnlyMemory<byte> constructor call

dotnet/src/webdriver/BiDi/Broker.cs


Grey Divider

Qodo Logo

@selenium-ci selenium-ci added the C-dotnet .NET Bindings label Mar 11, 2026
@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Mar 11, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Remediation recommended

1. ProcessReceivedMessage changes lack tests 📘 Rule violation ⛯ Reliability
Description
The PR modifies BiDi message parsing and parameter slicing logic without adding/updating tests to
cover the new behavior. This increases the risk of regressions in event/result parsing and parameter
extraction.
Code

dotnet/src/webdriver/BiDi/Broker.cs[R127-182]

+        int paramsStartIndex = 0;
+        int paramsEndIndex = 0;
-        Utf8JsonReader reader = new(new ReadOnlySpan<byte>(data));
+        Utf8JsonReader reader = new(data);
   reader.Read();
   reader.Read(); // "{"
   while (reader.TokenType == JsonTokenType.PropertyName)
   {
-            string? propertyName = reader.GetString();
-            reader.Read();
+            bool isParams = false;
-            switch (propertyName)
+            if (reader.ValueTextEquals("id"u8))
       {
-                case "id":
-                    id = reader.GetInt64();
-                    break;
-
-                case "type":
-                    type = reader.GetString();
-                    break;
-
-                case "method":
-                    method = reader.GetString();
-                    break;
-
-                case "result":
-                    resultReader = reader; // snapshot
-                    break;
-
-                case "params":
-                    paramsStartIndex = reader.TokenStartIndex;
-                    break;
-
-                case "error":
-                    error = reader.GetString();
-                    break;
-
-                case "message":
-                    message = reader.GetString();
-                    break;
+                reader.Read();
+                id = reader.GetInt64();
       }
-
-            if (propertyName == "params")
+            else if (reader.ValueTextEquals("type"u8))
+            {
+                reader.Read();
+                type = reader.GetString();
+            }
+            else if (reader.ValueTextEquals("method"u8))
       {
-                reader.Skip();
-                paramsEndIndex = reader.BytesConsumed;
+                reader.Read();
+                method = reader.GetString();
+            }
+            else if (reader.ValueTextEquals("result"u8))
+            {
+                reader.Read();
+                resultReader = reader; // snapshot
+            }
+            else if (reader.ValueTextEquals("params"u8))
+            {
+                reader.Read();
+                paramsStartIndex = (int)reader.TokenStartIndex;
+                isParams = true;
+            }
+            else if (reader.ValueTextEquals("error"u8))
+            {
+                reader.Read();
+                error = reader.GetString();
+            }
+            else if (reader.ValueTextEquals("message"u8))
+            {
+                reader.Read();
+                message = reader.GetString();
       }
       else
       {
-                reader.Skip();
+                reader.Read();
       }
+
+            reader.Skip();
+            if (isParams) paramsEndIndex = (int)reader.BytesConsumed;
       reader.Read();
Evidence
The compliance checklist requires behavior changes to be covered by appropriate tests. The PR
description and updated ProcessReceivedMessage implementation show parsing/indexing logic changes,
but no corresponding test updates are included in the provided diff.

AGENTS.md
dotnet/src/webdriver/BiDi/Broker.cs[119-183]
dotnet/src/webdriver/BiDi/Broker.cs[218-222]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`ProcessReceivedMessage` parsing/indexing logic was changed, but there are no accompanying tests ensuring the updated property handling and `params` byte slicing remain correct.
## Issue Context
This PR refactors JSON property parsing (`ValueTextEquals` branching and unconditional `Skip()` usage) and changes `paramsStartIndex`/`paramsEndIndex` handling used to create `ReadOnlyMemory&amp;amp;amp;amp;amp;lt;byte&amp;amp;amp;amp;amp;gt;` slices for `params`. These behaviors are easy to regress without targeted tests.
## Fix Focus Areas
- dotnet/src/webdriver/BiDi/Broker.cs[119-183]
- dotnet/src/webdriver/BiDi/Broker.cs[218-222]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors Broker.ProcessReceivedMessage in the .NET BiDi implementation to reduce allocations and simplify JSON property handling while ensuring parameter byte indices are handled consistently for slicing the raw params payload.

Changes:

  • Replaced string-based property name parsing with Utf8JsonReader.ValueTextEquals(...) using UTF-8 literals.
  • Standardized params start/end indices to int and simplified ReadOnlyMemory<byte> slicing for event params.

Copilot AI review requested due to automatic review settings March 11, 2026 13:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

@nvborisenko nvborisenko merged commit ec5d115 into SeleniumHQ:trunk Mar 11, 2026
18 of 19 checks passed
@nvborisenko nvborisenko deleted the bidi-process-message branch March 11, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-dotnet .NET Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants