From 1142c7dfc0b058dfe38207ecd2f53b58d7ac8b39 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 1 Feb 2016 11:26:19 -0800 Subject: [PATCH] Be smarter about stringbuilder usage --- .../ModelBinding/JQueryFormValueProviderFactory.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/JQueryFormValueProviderFactory.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/JQueryFormValueProviderFactory.cs index 86799d632b..2a162f52e2 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/JQueryFormValueProviderFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/JQueryFormValueProviderFactory.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding { throw new ArgumentNullException(nameof(context)); } - + var request = context.HttpContext.Request; if (request.HasFormContentType) { @@ -47,10 +47,15 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding { var formCollection = await request.ReadFormAsync(); - var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + var builder = new StringBuilder(); + var dictionary = new Dictionary( + formCollection.Count, + StringComparer.OrdinalIgnoreCase); foreach (var entry in formCollection) { - var key = NormalizeJQueryToMvc(entry.Key); + var key = NormalizeJQueryToMvc(builder, entry.Key); + builder.Clear(); + dictionary[key] = entry.Value; } @@ -63,7 +68,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding // [] --> "" // x[12] --> x[12] // x[field] --> x.field, where field is not a number - private static string NormalizeJQueryToMvc(string key) + private static string NormalizeJQueryToMvc(StringBuilder builder, string key) { if (string.IsNullOrEmpty(key)) { @@ -79,7 +84,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding return key; } - var builder = new StringBuilder(); var position = 0; while (position < key.Length) {