Skip to content

Commit 6129a8c

Browse files
authored
[perf] Reduce allocations on some transport paths (#5865)
* Cache RequestData.Uri once created Lazily caches the Uri on RequestData since this property is accessed at least three times and each time was allocating a new Uri. This saves 192 bytes per request. * Ensure we reset the Uri if the Node changes
1 parent ba59625 commit 6129a8c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/Elasticsearch.Net/Transport/Pipeline/RequestData.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace Elasticsearch.Net
1313
{
1414
public class RequestData
1515
{
16+
private Uri _requestUri;
17+
private Node _node;
18+
1619
public const string OpaqueIdHeader = "X-Opaque-Id";
1720
public const string RunAsSecurityHeader = "es-security-runas-user";
1821

@@ -127,7 +130,19 @@ IMemoryStreamFactory memoryStreamFactory
127130

128131
public HttpMethod Method { get; }
129132

130-
public Node Node { get; set; }
133+
public Node Node
134+
{
135+
get
136+
{
137+
return _node;
138+
}
139+
set
140+
{
141+
_requestUri = null;
142+
_node = value;
143+
}
144+
}
145+
131146
public AuditEvent OnFailureAuditEvent => MadeItToResponse ? AuditEvent.BadResponse : AuditEvent.BadRequest;
132147
public PipelineFailure OnFailurePipelineFailure => MadeItToResponse ? PipelineFailure.BadResponse : PipelineFailure.BadRequest;
133148
public string PathAndQuery { get; }
@@ -149,7 +164,20 @@ IMemoryStreamFactory memoryStreamFactory
149164
public bool TcpStats { get; }
150165
public bool ThreadPoolStats { get; }
151166

152-
public Uri Uri => Node != null ? new Uri(Node.Uri, PathAndQuery) : null;
167+
/// <summary>
168+
/// The <see cref="Uri" /> for the request.
169+
/// </summary>
170+
public Uri Uri
171+
{
172+
get
173+
{
174+
if (_requestUri is not null) return _requestUri;
175+
176+
_requestUri = Node is not null ? new Uri(Node.Uri, PathAndQuery) : null;
177+
return _requestUri;
178+
}
179+
}
180+
153181
public TimeSpan DnsRefreshTimeout { get; }
154182

155183
public MetaHeaderProvider MetaHeaderProvider { get; }

0 commit comments

Comments
 (0)