Skip to content

Commit

Permalink
Issue19: Parallelise ApplicantForRole construction (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlang42 authored Nov 30, 2023
1 parent 13e0b15 commit 667dfc5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions CastingEngine/FunctionCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,7 +10,7 @@ namespace Carmen.CastingEngine
public class FunctionCache<T, U>
where T : notnull
{
readonly Dictionary<T, U> cache = new();
readonly ConcurrentDictionary<T, U> cache = new();

public Func<T, U>? Function { get; set; }

Expand All @@ -24,7 +25,7 @@ public U Get(T input, Func<T, U>? function_override = null)
{
var function = Function ?? function_override ?? throw new ArgumentException("Function not set");
value = function(input);
cache.Add(input, value);
cache[input] = value;
}
return value;
}
Expand All @@ -34,7 +35,7 @@ public U Get(T input, Func<T, U>? function_override = null)

public class FunctionCache<T1, T2, U>
{
readonly Dictionary<(T1, T2), U> cache = new();
readonly ConcurrentDictionary<(T1, T2), U> cache = new();

public Func<T1, T2, U>? Function { get; set; }

Expand All @@ -49,7 +50,7 @@ public U Get(T1 input1, T2 input2, Func<T1, T2, U>? function_override = null)
{
var function = Function ?? function_override ?? throw new ArgumentException("Function not set");
value = function(input1, input2);
cache.Add((input1, input2), value);
cache[(input1, input2)] = value;
}
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion Desktop/ViewModels/EditableRoleWithApplicantsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public EditableRoleWithApplicantsView(IAllocationEngine engine, Role role, CastG
: base(role, cast_groups_by_cast)
{
requiredCastGroups = role.CountByGroups.Where(cbg => cbg.Count != 0).Select(cbg => cbg.CastGroup).ToHashSet();
Applicants = applicants.Select(a =>
Applicants = applicants.AsParallel().Select(a =>
{
var av = new ApplicantForRole(engine, a, role, primary_criterias);
av.PropertyChanged += ApplicantForRole_PropertyChanged;
Expand Down

0 comments on commit 667dfc5

Please sign in to comment.