Skip to content

[release/7.0-rc1] [wasm] Reduce allocation when options are passed to fetch #74230

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

Merged
merged 1 commit into from
Aug 22, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
int headerCount = request.Headers.Count + request.Content?.Headers.Count ?? 0;
List<string> headerNames = new List<string>(headerCount);
List<string> headerValues = new List<string>(headerCount);
List<string> optionNames = new List<string>();
List<object?> optionValues = new List<object?>();

JSObject abortController = BrowserHttpInterop.CreateAbortController();
CancellationTokenRegistration? abortRegistration = cancellationToken.Register(() =>
{
Expand All @@ -147,12 +144,27 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
});
try
{
optionNames.Add("method");
optionValues.Add(request.Method.Method);
if (request.RequestUri == null)
{
throw new ArgumentNullException(nameof(request.RequestUri));
}

string uri = request.RequestUri.IsAbsoluteUri ? request.RequestUri.AbsoluteUri : request.RequestUri.ToString();

bool hasFetchOptions = request.Options.TryGetValue(FetchOptions, out IDictionary<string, object>? fetchOptions);
int optionCount = 1 + (allowAutoRedirect.HasValue ? 1 : 0) + (hasFetchOptions && fetchOptions != null ? fetchOptions.Count : 0);
int optionIndex = 0;
string[] optionNames = new string[optionCount];
object?[] optionValues = new object?[optionCount];

optionNames[optionIndex] = "method";
optionValues[optionIndex] = request.Method.Method;
optionIndex++;
if (allowAutoRedirect.HasValue)
{
optionNames.Add("redirect");
optionValues.Add(allowAutoRedirect.Value ? "follow" : "manual");
optionNames[optionIndex] = "redirect";
optionValues[optionIndex] = allowAutoRedirect.Value ? "follow" : "manual";
optionIndex++;
}

foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
Expand All @@ -176,21 +188,16 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
}
}

if (request.Options.TryGetValue(FetchOptions, out IDictionary<string, object>? fetchOptions))
if (hasFetchOptions && fetchOptions != null)
{
foreach (KeyValuePair<string, object> item in fetchOptions)
{
optionNames.Add(item.Key);
optionValues.Add(item.Value);
optionNames[optionIndex] = item.Key;
optionValues[optionIndex] = item.Value;
optionIndex++;
}
}

if (request.RequestUri == null)
{
throw new ArgumentNullException(nameof(request.RequestUri));
}

string uri = request.RequestUri.IsAbsoluteUri ? request.RequestUri.AbsoluteUri : request.RequestUri.ToString();
Task<JSObject>? promise;
cancellationToken.ThrowIfCancellationRequested();
if (request.Content != null)
Expand All @@ -201,21 +208,22 @@ private static async Task<WasmFetchResponse> CallFetch(HttpRequestMessage reques
.ConfigureAwait(true);
cancellationToken.ThrowIfCancellationRequested();

promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController, body);
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController, body);
}
else
{
byte[] buffer = await request.Content.ReadAsByteArrayAsync(cancellationToken)
.ConfigureAwait(true);
cancellationToken.ThrowIfCancellationRequested();

promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController, buffer);
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController, buffer);
}
}
else
{
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames.ToArray(), optionValues.ToArray(), abortController);
promise = BrowserHttpInterop.Fetch(uri, headerNames.ToArray(), headerValues.ToArray(), optionNames, optionValues, abortController);
}

cancellationToken.ThrowIfCancellationRequested();
ValueTask<JSObject> wrappedTask = BrowserHttpInterop.CancelationHelper(promise, cancellationToken, abortController);
JSObject fetchResponse = await wrappedTask.ConfigureAwait(true);
Expand Down