Skip to content
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

Performance enhancements #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ronaldbarendse
Copy link
Contributor

I've spotted some other (major) performance enhancements with initializations, see commit comments.

Copy link
Contributor Author

@ronaldbarendse ronaldbarendse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments to the commits to explain the enhancements.

@@ -158,6 +160,7 @@ public LinkType Type
}
}

private UmbracoHelper Umbraco => _umbracoHelper ?? (_umbracoHelper = new UmbracoHelper(UmbracoContext.Current));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the UmbracoHelper was not provided in the constructor, create one here and store it in the field.

@@ -170,9 +173,22 @@ private void InitPublishedContent()
return;
}

if (Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
if (Udi.TryParse(_linkItem.Value<string>("udi"), out _udi) && _udi is GuidUdi guidUdi)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this code is copied from the ToPublishedContent() method, but now uses the same UmbracoHelper.

if (!newLink.Deleted)
{
_multiUrls.Add(new Link(item));
_multiUrls.Add(newLink);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add the link, no need to create a yet another one 😄

@@ -37,22 +37,24 @@ public override bool IsConverter(PublishedPropertyType propertyType)

public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (string.IsNullOrWhiteSpace(source?.ToString()))
var sourceString = source?.ToString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert the source to a string once and then use it everywhere, no need to do the same over and over again 😀

@@ -39,12 +40,15 @@ public MultiUrls(string propertyData)

private void Initialize(JArray data)
{
// Create UmbracoHelper here, because we (probably) need it more than once
var umbracoHelper = data.Count > 1 ? new UmbracoHelper(UmbracoContext.Current) : null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only need the UmbracoHelper if the link uses content or media, creating it here and not using it could actually decrease performance. We take that chance if we have more than 1 link...

@@ -39,12 +40,14 @@ public MultiUrls(string propertyData)

private void Initialize(JArray data)
{
var umbracoHelper = new Lazy<UmbracoHelper>(() => new UmbracoHelper(UmbracoContext.Current));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using Lazy<UmbracoHelper> and passing the same instance, it is only created when actually needed and only once (if multiple links are added).

Copy link
Contributor Author

@ronaldbarendse ronaldbarendse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now with even better performance 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant