Optimize Guid comparisons

* Use Guid.Equals(Guid) instead of the == override
* Ban the usage of Guid.Equals(Object) to prevent accidental boxing
* Compare to default(Guid) instead of Guid.Empty
This commit is contained in:
Bond_009
2022-02-21 14:15:09 +01:00
parent bbac59c6d6
commit f50a250cd9
66 changed files with 355 additions and 326 deletions

View File

@@ -187,14 +187,14 @@ namespace MediaBrowser.Controller.Entities
/// <exception cref="ArgumentNullException">The id is empty.</exception>
public BaseItem FindVirtualChild(Guid id)
{
if (id.Equals(Guid.Empty))
if (id.Equals(default))
{
throw new ArgumentNullException(nameof(id));
}
foreach (var child in _virtualChildren)
{
if (child.Id == id)
if (child.Id.Equals(id))
{
return child;
}

View File

@@ -24,7 +24,7 @@ namespace MediaBrowser.Controller.Entities.Audio
public class MusicArtist : Folder, IItemByName, IHasMusicGenres, IHasDualAccess, IHasLookupInfo<ArtistInfo>
{
[JsonIgnore]
public bool IsAccessedByName => ParentId.Equals(Guid.Empty);
public bool IsAccessedByName => ParentId.Equals(default);
[JsonIgnore]
public override bool IsFolder => !IsAccessedByName;

View File

@@ -231,7 +231,7 @@ namespace MediaBrowser.Controller.Entities
{
get
{
if (!ChannelId.Equals(Guid.Empty))
if (!ChannelId.Equals(default))
{
return SourceType.Channel;
}
@@ -521,7 +521,7 @@ namespace MediaBrowser.Controller.Entities
get
{
var id = DisplayParentId;
if (id.Equals(Guid.Empty))
if (id.Equals(default))
{
return null;
}
@@ -737,7 +737,7 @@ namespace MediaBrowser.Controller.Entities
public virtual bool StopRefreshIfLocalMetadataFound => true;
[JsonIgnore]
protected virtual bool SupportsOwnedItems => !ParentId.Equals(Guid.Empty) && IsFileProtocol;
protected virtual bool SupportsOwnedItems => !ParentId.Equals(default) && IsFileProtocol;
[JsonIgnore]
public virtual bool SupportsPeople => false;
@@ -848,7 +848,7 @@ namespace MediaBrowser.Controller.Entities
public BaseItem GetOwner()
{
var ownerId = OwnerId;
return ownerId.Equals(Guid.Empty) ? null : LibraryManager.GetItemById(ownerId);
return ownerId.Equals(default) ? null : LibraryManager.GetItemById(ownerId);
}
public bool CanDelete(User user, List<Folder> allCollectionFolders)
@@ -984,12 +984,12 @@ namespace MediaBrowser.Controller.Entities
public BaseItem GetParent()
{
var parentId = ParentId;
if (!parentId.Equals(Guid.Empty))
if (parentId.Equals(default))
{
return LibraryManager.GetItemById(parentId);
return null;
}
return null;
return LibraryManager.GetItemById(parentId);
}
public IEnumerable<BaseItem> GetParents()
@@ -1397,7 +1397,7 @@ namespace MediaBrowser.Controller.Entities
var tasks = extras.Select(i =>
{
var subOptions = new MetadataRefreshOptions(options);
if (i.OwnerId != ownerId || i.ParentId != Guid.Empty)
if (!i.OwnerId.Equals(ownerId) || !i.ParentId.Equals(default))
{
i.OwnerId = ownerId;
i.ParentId = Guid.Empty;
@@ -1736,7 +1736,7 @@ namespace MediaBrowser.Controller.Entities
// First get using the cached Id
if (info.ItemId.HasValue)
{
if (info.ItemId.Value.Equals(Guid.Empty))
if (info.ItemId.Value.Equals(default))
{
return null;
}
@@ -2657,7 +2657,7 @@ namespace MediaBrowser.Controller.Entities
}
/// <inheritdoc />
public bool Equals(BaseItem other) => Id == other?.Id;
public bool Equals(BaseItem other) => other is not null && other.Id.Equals(Id);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(Id);

View File

@@ -213,7 +213,7 @@ namespace MediaBrowser.Controller.Entities
{
item.SetParent(this);
if (item.Id.Equals(Guid.Empty))
if (item.Id.Equals(default))
{
item.Id = LibraryManager.GetNewItemId(item.Path, item.GetType());
}
@@ -730,7 +730,9 @@ namespace MediaBrowser.Controller.Entities
return PostFilterAndSort(items, query, true);
}
if (this is not UserRootFolder && this is not AggregateFolder && query.ParentId == Guid.Empty)
if (this is not UserRootFolder
&& this is not AggregateFolder
&& query.ParentId.Equals(default))
{
query.Parent = this;
}
@@ -1492,7 +1494,7 @@ namespace MediaBrowser.Controller.Entities
{
if (i.ItemId.HasValue)
{
if (i.ItemId.Value == itemId)
if (i.ItemId.Value.Equals(itemId))
{
return true;
}
@@ -1502,7 +1504,7 @@ namespace MediaBrowser.Controller.Entities
var child = GetLinkedChild(i);
if (child != null && child.Id == itemId)
if (child != null && child.Id.Equals(itemId))
{
return true;
}

View File

@@ -74,12 +74,12 @@ namespace MediaBrowser.Controller.Entities.TV
get
{
var seriesId = SeriesId;
if (seriesId.Equals(Guid.Empty))
if (seriesId.Equals(default))
{
seriesId = FindSeriesId();
}
return !seriesId.Equals(Guid.Empty) ? (LibraryManager.GetItemById(seriesId) as Series) : null;
return seriesId.Equals(default) ? null : (LibraryManager.GetItemById(seriesId) as Series);
}
}
@@ -89,12 +89,12 @@ namespace MediaBrowser.Controller.Entities.TV
get
{
var seasonId = SeasonId;
if (seasonId.Equals(Guid.Empty))
if (seasonId.Equals(default))
{
seasonId = FindSeasonId();
}
return !seasonId.Equals(Guid.Empty) ? (LibraryManager.GetItemById(seasonId) as Season) : null;
return seasonId.Equals(default) ? null : (LibraryManager.GetItemById(seasonId) as Season);
}
}
@@ -271,7 +271,7 @@ namespace MediaBrowser.Controller.Entities.TV
var seasonId = SeasonId;
if (!seasonId.Equals(Guid.Empty) && !list.Contains(seasonId))
if (!seasonId.Equals(default) && !list.Contains(seasonId))
{
list.Add(seasonId);
}

View File

@@ -48,12 +48,12 @@ namespace MediaBrowser.Controller.Entities.TV
get
{
var seriesId = SeriesId;
if (seriesId == Guid.Empty)
if (seriesId.Equals(default))
{
seriesId = FindSeriesId();
}
return seriesId == Guid.Empty ? null : (LibraryManager.GetItemById(seriesId) as Series);
return seriesId.Equals(default) ? null : (LibraryManager.GetItemById(seriesId) as Series);
}
}

View File

@@ -69,11 +69,11 @@ namespace MediaBrowser.Controller.Entities
/// <inheritdoc />
public override IEnumerable<Guid> GetIdsForAncestorQuery()
{
if (!DisplayParentId.Equals(Guid.Empty))
if (!DisplayParentId.Equals(default))
{
yield return DisplayParentId;
}
else if (!ParentId.Equals(Guid.Empty))
else if (!ParentId.Equals(default))
{
yield return ParentId;
}
@@ -94,11 +94,11 @@ namespace MediaBrowser.Controller.Entities
{
var parent = this as Folder;
if (!DisplayParentId.Equals(Guid.Empty))
if (!DisplayParentId.Equals(default))
{
parent = LibraryManager.GetItemById(DisplayParentId) as Folder ?? parent;
}
else if (!ParentId.Equals(Guid.Empty))
else if (!ParentId.Equals(default))
{
parent = LibraryManager.GetItemById(ParentId) as Folder ?? parent;
}

View File

@@ -988,7 +988,7 @@ namespace MediaBrowser.Controller.Entities
public static IEnumerable<BaseItem> FilterForAdjacency(List<BaseItem> list, string adjacentToId)
{
var adjacentToIdGuid = new Guid(adjacentToId);
var adjacentToItem = list.FirstOrDefault(i => i.Id == adjacentToIdGuid);
var adjacentToItem = list.FirstOrDefault(i => i.Id.Equals(adjacentToIdGuid));
var index = list.IndexOf(adjacentToItem);
@@ -1005,7 +1005,7 @@ namespace MediaBrowser.Controller.Entities
nextId = list[index + 1].Id;
}
return list.Where(i => i.Id == previousId || i.Id == nextId || i.Id == adjacentToIdGuid);
return list.Where(i => i.Id.Equals(previousId) || i.Id.Equals(nextId) || i.Id.Equals(adjacentToIdGuid));
}
}
}

View File

@@ -455,7 +455,7 @@ namespace MediaBrowser.Controller.Entities
foreach (var child in LinkedAlternateVersions)
{
// Reset the cached value
if (child.ItemId.HasValue && child.ItemId.Value.Equals(Guid.Empty))
if (child.ItemId.HasValue && child.ItemId.Value.Equals(default))
{
child.ItemId = null;
}

View File

@@ -233,7 +233,7 @@ namespace MediaBrowser.Controller.Playlists
return base.IsVisible(user);
}
if (user.Id == OwnerUserId)
if (user.Id.Equals(OwnerUserId))
{
return true;
}
@@ -244,8 +244,8 @@ namespace MediaBrowser.Controller.Playlists
return base.IsVisible(user);
}
var userId = user.Id.ToString("N", CultureInfo.InvariantCulture);
return shares.Any(share => string.Equals(share.UserId, userId, StringComparison.OrdinalIgnoreCase));
var userId = user.Id;
return shares.Any(share => Guid.TryParse(share.UserId, out var id) && id.Equals(userId));
}
public override bool IsVisibleStandalone(User user)