Skip to content

Commit 9557630

Browse files
[SignalR] Remove a few minor allocations (#13872)
1 parent be33a6f commit 9557630

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ private async Task StopAsyncCore(bool disposing)
539539
/// </returns>
540540
public IAsyncEnumerable<TResult> StreamAsyncCore<TResult>(string methodName, object[] args, CancellationToken cancellationToken = default)
541541
{
542-
var cts = cancellationToken.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken) : new CancellationTokenSource();
542+
var cts = cancellationToken.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, default) : new CancellationTokenSource();
543543
var stream = CastIAsyncEnumerable<TResult>(methodName, args, cts);
544544
var cancelableStream = AsyncEnumerableAdapters.MakeCancelableTypedAsyncEnumerable(stream, cts);
545545
return cancelableStream;

src/SignalR/server/Core/src/DefaultHubLifetimeManager.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ public override Task SendAllAsync(string methodName, object[] args, Cancellation
8585
return SendToAllConnections(methodName, args, null);
8686
}
8787

88-
private Task SendToAllConnections(string methodName, object[] args, Func<HubConnectionContext, bool> include)
88+
private Task SendToAllConnections(string methodName, object[] args, Func<HubConnectionContext, object, bool> include, object state = null)
8989
{
9090
List<Task> tasks = null;
9191
SerializedHubMessage message = null;
9292

9393
// foreach over HubConnectionStore avoids allocating an enumerator
9494
foreach (var connection in _connections)
9595
{
96-
if (include != null && !include(connection))
96+
if (include != null && !include(connection, state))
9797
{
9898
continue;
9999
}
@@ -127,12 +127,12 @@ private Task SendToAllConnections(string methodName, object[] args, Func<HubConn
127127

128128
// Tasks and message are passed by ref so they can be lazily created inside the method post-filtering,
129129
// while still being re-usable when sending to multiple groups
130-
private void SendToGroupConnections(string methodName, object[] args, ConcurrentDictionary<string, HubConnectionContext> connections, Func<HubConnectionContext, bool> include, ref List<Task> tasks, ref SerializedHubMessage message)
130+
private void SendToGroupConnections(string methodName, object[] args, ConcurrentDictionary<string, HubConnectionContext> connections, Func<HubConnectionContext, object, bool> include, object state, ref List<Task> tasks, ref SerializedHubMessage message)
131131
{
132132
// foreach over ConcurrentDictionary avoids allocating an enumerator
133133
foreach (var connection in connections)
134134
{
135-
if (include != null && !include(connection.Value))
135+
if (include != null && !include(connection.Value, state))
136136
{
137137
continue;
138138
}
@@ -193,7 +193,7 @@ public override Task SendGroupAsync(string groupName, string methodName, object[
193193
// group might be modified inbetween checking and sending
194194
List<Task> tasks = null;
195195
SerializedHubMessage message = null;
196-
SendToGroupConnections(methodName, args, group, null, ref tasks, ref message);
196+
SendToGroupConnections(methodName, args, group, null, null, ref tasks, ref message);
197197

198198
if (tasks != null)
199199
{
@@ -221,7 +221,7 @@ public override Task SendGroupsAsync(IReadOnlyList<string> groupNames, string me
221221
var group = _groups[groupName];
222222
if (group != null)
223223
{
224-
SendToGroupConnections(methodName, args, group, null, ref tasks, ref message);
224+
SendToGroupConnections(methodName, args, group, null, null, ref tasks, ref message);
225225
}
226226
}
227227

@@ -247,7 +247,7 @@ public override Task SendGroupExceptAsync(string groupName, string methodName, o
247247
List<Task> tasks = null;
248248
SerializedHubMessage message = null;
249249

250-
SendToGroupConnections(methodName, args, group, connection => !excludedConnectionIds.Contains(connection.ConnectionId), ref tasks, ref message);
250+
SendToGroupConnections(methodName, args, group, (connection, state) => !((IReadOnlyList<string>)state).Contains(connection.ConnectionId), excludedConnectionIds, ref tasks, ref message);
251251

252252
if (tasks != null)
253253
{
@@ -271,7 +271,7 @@ private HubMessage CreateInvocationMessage(string methodName, object[] args)
271271
/// <inheritdoc />
272272
public override Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default)
273273
{
274-
return SendToAllConnections(methodName, args, connection => string.Equals(connection.UserIdentifier, userId, StringComparison.Ordinal));
274+
return SendToAllConnections(methodName, args, (connection, state) => string.Equals(connection.UserIdentifier, (string)state, StringComparison.Ordinal), userId);
275275
}
276276

277277
/// <inheritdoc />
@@ -292,19 +292,19 @@ public override Task OnDisconnectedAsync(HubConnectionContext connection)
292292
/// <inheritdoc />
293293
public override Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList<string> excludedConnectionIds, CancellationToken cancellationToken = default)
294294
{
295-
return SendToAllConnections(methodName, args, connection => !excludedConnectionIds.Contains(connection.ConnectionId));
295+
return SendToAllConnections(methodName, args, (connection, state) => !((IReadOnlyList<string>)state).Contains(connection.ConnectionId), excludedConnectionIds);
296296
}
297297

298298
/// <inheritdoc />
299299
public override Task SendConnectionsAsync(IReadOnlyList<string> connectionIds, string methodName, object[] args, CancellationToken cancellationToken = default)
300300
{
301-
return SendToAllConnections(methodName, args, connection => connectionIds.Contains(connection.ConnectionId));
301+
return SendToAllConnections(methodName, args, (connection, state) => ((IReadOnlyList<string>)state).Contains(connection.ConnectionId), connectionIds);
302302
}
303303

304304
/// <inheritdoc />
305305
public override Task SendUsersAsync(IReadOnlyList<string> userIds, string methodName, object[] args, CancellationToken cancellationToken = default)
306306
{
307-
return SendToAllConnections(methodName, args, connection => userIds.Contains(connection.UserIdentifier));
307+
return SendToAllConnections(methodName, args, (connection, state) => ((IReadOnlyList<string>)state).Contains(connection.UserIdentifier), userIds);
308308
}
309309
}
310310
}

src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
275275
{
276276
if (descriptor.OriginalParameterTypes[parameterPointer] == typeof(CancellationToken))
277277
{
278-
cts = CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAborted);
278+
cts = CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAborted, default);
279279
arguments[parameterPointer] = cts.Token;
280280
}
281281
else if (isStreamCall && ReflectionHelper.IsStreamingType(descriptor.OriginalParameterTypes[parameterPointer], mustBeDirectType: true))
@@ -308,7 +308,7 @@ await SendInvocationError(hubMethodInvocationMessage.InvocationId, connection,
308308
return;
309309
}
310310

311-
cts = cts ?? CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAborted);
311+
cts = cts ?? CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAborted, default);
312312
connection.ActiveRequestCancellationSources.TryAdd(hubMethodInvocationMessage.InvocationId, cts);
313313
var enumerable = descriptor.FromReturnedStream(result, cts.Token);
314314

0 commit comments

Comments
 (0)