Skip to content

Use interned strings in Hub protocol implementations #41342

@davidfowl

Description

@davidfowl

Background and Motivation

Looking at profiles for one of my SignalR applications I see lots of string allocations showing up and most of them seem to come from the protocol implementation:

image

It's:

  • Hub method names
  • Invocation ids Not worth it unless we could optimize the number path and never allocate them.

We end up invoking these methods over and over and allocating the same strings for both invocation ids (numbers in our clients) and for hub method names (which are known at startup).

We should be using cached strings for these. Right now we don't have a clean extensibility model to inject this behavior. Maybe we can add something to the options.

Proposed API

namespace Microsoft.AspNetCore.SignalR;

public interface IInvocationBinder
{
+ #if NETCOREAPP
+    string? GetTarget(ReadOnlySpan<byte> utf8Bytes) => null;
+ #endif
}

Usage Examples

Called by SignalR protocols to get the cached hub name from the binder (if there exists any).

Alternative Designs

An alternative would be a new interface that can be optionally implemented on the IInvocationBinder:

public interface ITargetResolver
{
    string? GetTarget(ReadOnlySpan<byte> utf8Bytes);
}

Risks

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Perfapi-approvedAPI was approved in API review, it can be implementedarea-signalrIncludes: SignalR clients and serversold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions