-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: master
Are you sure you want to change the base?
Performance enhancements #81
Conversation
There was a problem hiding this 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)); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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).
There was a problem hiding this 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 😄
I've spotted some other (major) performance enhancements with initializations, see commit comments.