rasmusjp/umbraco-multi-url-picker

Performance issues

Opened this issue · 4 comments

I've narrowed down a performance issue we're having with the url picker. When we request a Link, it's taking about 7ms to return the data.
content.Link.FirstOrDefault(l => l.Url != null)

I presume the delay is either the result of the internal call to Link.InitPublishedContent or the deserialisation. 7ms in isolation might not seem much, but in our footer this code is called 30 times. In our header it's called 20 times. That's 350ms delay just from accessing content cached data. Can anything be done to speed this up?

When accessing the Url property of a Link, it indeed first tries to get the IPublishedContent, but that should not be an expansive lookup if the Id and Udi are empty. It does however initialize a new UmbracoHelper for every lookup, even if it doesn't need it:

private void InitPublishedContent()
{
if (!_publishedContentInitialized)
{
_publishedContentInitialized = true;
if (UmbracoContext.Current == null)
{
return;
}
if (Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
{
_content = _udi.ToPublishedContent();
_id = _content?.Id;
}
else
{
var helper = new UmbracoHelper(UmbracoContext.Current);
// there were no Udi so let's try the legacy way
_id = _linkItem.Value<int?>("id");
if (_id.HasValue)
{
bool isMedia = _linkItem.Value<bool>("isMedia");
if (_linkItem.Value<bool>("isMedia"))
{
_content = helper.TypedMedia(_id.Value);
}
else
{
_content = helper.TypedContent(_id.Value);
}
SetUdi();
}
}
}
}

Moving this within the if (_id.HasValue) statement should be a big improvement, especially if you're using mostly external URLs.

The _udi.ToPublishedContent(); has the same flaw BTW and is depreciated (probably because of this performance issue): https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/Extensions/UdiExtensions.cs

I see a fix was merged into master recently -- will this be in a release soon?

I've added some more performance improvements in PR #81. Those could also be merged in before creating a new release (although some additional testing should be done first, just to be sure).