From c2c87a0d723e2ceb84228510a110a4ef786dcb0c Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Tue, 19 Jul 2022 10:58:21 +1000 Subject: [PATCH] Don't allocate new object array instances when indexing (#12710) * Don't allocate new object array instances when indexing There's no reason to allocate new object[] array instances for each ValueSet created for the "y" or "n" values going into the index. This just creates a single object[] for each "y" or "n" value which will save on a ton of allocations when re indexing a bunch of content. There's an easy way to make further allocation reductions too (prob a separate PR) since there is no reason to create new object[] allocations for: CreatorId, WriterId, Level, TemplateId, Culture. Each of these can be returned from a concurrent dictionary since these values are finite and there won't be too many of each so we can just keep an internal dictionary of these object[] instances. * fixes mistake * Updates NoValue and YesValue field declarations - was blowing up Co-authored-by: Nathan Woulfe --- .../Examine/ContentValueSetBuilder.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Infrastructure/Examine/ContentValueSetBuilder.cs b/src/Umbraco.Infrastructure/Examine/ContentValueSetBuilder.cs index f92079513d..05274fc28e 100644 --- a/src/Umbraco.Infrastructure/Examine/ContentValueSetBuilder.cs +++ b/src/Umbraco.Infrastructure/Examine/ContentValueSetBuilder.cs @@ -16,6 +16,9 @@ namespace Umbraco.Cms.Infrastructure.Examine; public class ContentValueSetBuilder : BaseValueSetBuilder, IContentValueSetBuilder, IPublishedContentValueSetBuilder { + private static readonly object[] NoValue = new[] { "n" }; + private static readonly object[] YesValue = new[] { "y" }; + private readonly IScopeProvider _scopeProvider; private readonly IShortStringHelper _shortStringHelper; @@ -72,7 +75,7 @@ public class ContentValueSetBuilder : BaseValueSetBuilder, IContentVal { { "icon", c.ContentType.Icon?.Yield() ?? Enumerable.Empty() }, { - UmbracoExamineFieldNames.PublishedFieldName, new object[] { c.Published ? "y" : "n" } + UmbracoExamineFieldNames.PublishedFieldName, c.Published ? YesValue : NoValue }, // Always add invariant published value { "id", new object[] { c.Id } }, { UmbracoExamineFieldNames.NodeKeyFieldName, new object[] { c.Key } }, @@ -102,12 +105,12 @@ public class ContentValueSetBuilder : BaseValueSetBuilder, IContentVal }, { "writerID", new object[] { c.WriterId } }, { "templateID", new object[] { c.TemplateId ?? 0 } }, - { UmbracoExamineFieldNames.VariesByCultureFieldName, new object[] { "n" } }, + { UmbracoExamineFieldNames.VariesByCultureFieldName, NoValue }, }; if (isVariant) { - values[UmbracoExamineFieldNames.VariesByCultureFieldName] = new object[] { "y" }; + values[UmbracoExamineFieldNames.VariesByCultureFieldName] = YesValue; foreach (var culture in c.AvailableCultures) {