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 <nathan@nathanw.com.au>
This commit is contained in:
Shannon Deminick
2022-07-19 10:58:21 +10:00
committed by GitHub
parent 41eb412403
commit c2c87a0d72

View File

@@ -16,6 +16,9 @@ namespace Umbraco.Cms.Infrastructure.Examine;
public class ContentValueSetBuilder : BaseValueSetBuilder<IContent>, 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<IContent>, IContentVal
{
{ "icon", c.ContentType.Icon?.Yield() ?? Enumerable.Empty<string>() },
{
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<IContent>, 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)
{