Skip to content
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

Use byte array in message send to avoid some unnecessary encoding #2749

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp.Tests/PollerInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace PuppeteerSharp.Tests
{
public sealed class PollerInterceptor(IConnectionTransport connectionTransport) : IConnectionTransport
{
public event EventHandler<string> MessageSent;
public event EventHandler<byte[]> MessageSent;

public Task SendAsync(string message)
public Task SendAsync(byte[] message)
{
var task = connectionTransport.SendAsync(message);
MessageSent?.Invoke(connectionTransport, message);
Expand Down Expand Up @@ -41,7 +41,7 @@ public Task<bool> WaitForStartPollingAsync()
// We intercept the poller.start() call to prevent tests from continuing before the polling has started.
MessageSent += (_, message) =>
{
if (message.Contains("poller.start()"))
if (message.AsSpan().IndexOf("poller.start()"u8) != -1)
{
startedPolling.SetResult(true);
}
Expand Down
12 changes: 8 additions & 4 deletions lib/PuppeteerSharp/Cdp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ internal static async Task<Connection> Create(string url, IConnectionOptions con

internal int GetMessageId() => Interlocked.Increment(ref _lastId);

internal Task RawSendAsync(string message, CommandOptions options = null)
internal Task RawSendAsync(byte[] message, CommandOptions options = null)
{
_logger.LogTrace("Send ► {Message}", message);
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("Send ► {Message}", Encoding.UTF8.GetString(message));
}

return Transport.SendAsync(message);
}

internal string GetMessage(int id, string method, object args, string sessionId = null)
=> JsonSerializer.Serialize(
internal byte[] GetMessage(int id, string method, object args, string sessionId = null)
=> JsonSerializer.SerializeToUtf8Bytes(
new ConnectionRequest { Id = id, Method = method, Params = args, SessionId = sessionId },
JsonHelper.DefaultJsonSerializerSettings.Value);

Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/MessageTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PuppeteerSharp
{
internal class MessageTask
{
internal string Message { get; set; }
internal byte[] Message { get; set; }

internal TaskCompletionSource<JsonElement?> TaskWrapper { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Transport/IConnectionTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public interface IConnectionTransport : IDisposable
/// </summary>
/// <param name="message">Message to send.</param>
/// <returns>The task.</returns>
Task SendAsync(string message);
Task SendAsync(byte[] message);
}
}
6 changes: 2 additions & 4 deletions lib/PuppeteerSharp/Transport/WebSocketTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ private WebSocketTransport(WebSocket client, TransportTaskScheduler scheduler, b
public bool IsClosed { get; private set; }

/// <inheritdoc />
public Task SendAsync(string message)
public Task SendAsync(byte[] message)
{
var encoded = Encoding.UTF8.GetBytes(message);
var buffer = new ArraySegment<byte>(encoded, 0, encoded.Length);
Task SendCoreAsync() => _client.SendAsync(buffer, WebSocketMessageType.Text, true, default);
Task SendCoreAsync() => _client.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Text, true, default);

return _queueRequests ? _socketQueue.Enqueue(SendCoreAsync) : SendCoreAsync();
}
Expand Down
Loading