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

Small optimization in TaxonomyService #8775

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class TaxonomyService : ITaxonomyService {

private readonly HashSet<int> _processedTermPartIds = new HashSet<int>();

private Dictionary<string, TaxonomyPart> _taxonomiesByName;
private Dictionary<string, TaxonomyPart> _taxonomiesBySlug;

public TaxonomyService(
IRepository<TermContentItem> termContentItemRepository,
IContentManager contentManager,
Expand All @@ -54,13 +57,21 @@ public TaxonomyService(

Logger = NullLogger.Instance;
T = NullLocalizer.Instance;

// initialize memorization structures
_taxonomiesByName = new Dictionary<string, TaxonomyPart>();
_taxonomiesBySlug = new Dictionary<string, TaxonomyPart>();
}

public ILogger Logger { get; set; }
public Localizer T { get; set; }

private IEnumerable<TaxonomyPart> _taxonomies;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private IEnumerable<TaxonomyPart> _taxonomies;
private List<TaxonomyPart> _taxonomies;

Since the type is known and this is a private member

Copy link
Member

Choose a reason for hiding this comment

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

Unless it's not what the method returns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IContentQuery<T>.List() (which is what's assigned and eventually returned here) returns an IEnumerable<T>. Definitely not a List<T>.

public IEnumerable<TaxonomyPart> GetTaxonomies() {
return GetTaxonomiesQuery().List();
if (_taxonomies == null) {
_taxonomies = GetTaxonomiesQuery().List();
}
return _taxonomies;
}

public virtual TaxonomyPart GetTaxonomy(int id) {
Expand All @@ -71,26 +82,32 @@ public TaxonomyPart GetTaxonomyByName(string name) {
if (string.IsNullOrWhiteSpace(name)) {
throw new ArgumentNullException("name");
}

// include the record in the query to optimize the query plan
return GetTaxonomiesQuery()
.Join<TitlePartRecord>()
.Where(r => r.Title == name)
.List()
.FirstOrDefault();
if (!_taxonomiesByName.ContainsKey(name)) {
// include the record in the query to optimize the query plan
var t = GetTaxonomiesQuery()
.Join<TitlePartRecord>()
.Where(r => r.Title == name)
.List()
.FirstOrDefault();
_taxonomiesByName.Add(name, t);
}
return _taxonomiesByName[name];
}

public TaxonomyPart GetTaxonomyBySlug(string slug) {
if (string.IsNullOrWhiteSpace(slug)) {
throw new ArgumentNullException("slug");
}

return GetTaxonomiesQuery()
.Join<TitlePartRecord>()
.Join<AutoroutePartRecord>()
.Where(r => r.DisplayAlias == slug)
.List()
.FirstOrDefault();
if (!_taxonomiesBySlug.ContainsKey(slug)) {
var t = GetTaxonomiesQuery()
.Join<TitlePartRecord>()
.Join<AutoroutePartRecord>()
.Where(r => r.DisplayAlias == slug)
.List()
.FirstOrDefault();
_taxonomiesBySlug.Add(slug, t);
}
return _taxonomiesBySlug[slug];
}

public void CreateTermContentType(TaxonomyPart taxonomy) {
Expand Down