Skip to content

Commit

Permalink
Recalcalculate cache when new values are found.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Sep 20, 2023
1 parent fe1a22c commit b8a004a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Itinero.Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!--- Package information, Version -->
<PropertyGroup>
<PackageVersion>1.6.0-pre036</PackageVersion>
<PackageVersion>1.6.0-pre037</PackageVersion>
<NeutralLanguage>en</NeutralLanguage>
<Description>Itinero - Routing for .NET.</Description>
<Copyright>Itinero BV</Copyright>
Expand Down
51 changes: 50 additions & 1 deletion src/Itinero/Profiles/ProfileFactorAndSpeedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public bool ContainsAll(params IProfileInstance[] profileInstances)
{
for (var p = 0; p < profileInstances.Length; p++)
{
if (!_edgeProfileFactors.ContainsKey(profileInstances[p].Profile.FullName))
if (!_edgeProfileFactors.TryGetValue(profileInstances[p].Profile.FullName, out _))
{
return false;
}
Expand Down Expand Up @@ -124,6 +124,23 @@ public void CalculateFor(params Profile[] profiles)
}
}

private void CalculateFor(Profile profile)
{
// don't allow multiple threads to fill this cache at the same time.
var factors = new FactorAndSpeed[(int) _db.EdgeProfiles.Count];
lock (this)
{
// don't allow multiple threads to fill this cache at the same time.
for (uint edgeProfile = 0; edgeProfile < _db.EdgeProfiles.Count; edgeProfile++)
{
var edgeProfileTags = _db.EdgeProfiles.Get(edgeProfile);
factors[edgeProfile]
= profile.FactorAndSpeed(edgeProfileTags);
}
}
_edgeProfileFactors[profile.FullName] = factors;
}

/// <summary>
/// Returns an isacceptable function using the cached data.
/// </summary>
Expand All @@ -144,8 +161,16 @@ public Func<GeometricEdge, bool> GetIsAcceptable(bool verifyCanStopOn, params IP
ushort edgeProfileId;
Data.Edges.EdgeDataSerializer.Deserialize(edge.Data[0],
out distance, out edgeProfileId);
for (var i = 0; i < profileInstances.Length; i++)
{
if (edgeProfileId >= cachedFactors[i].Length)
{
this.CalculateFor(profileInstances[i].Profile);
if (!_edgeProfileFactors.TryGetValue(profileInstances[i].Profile.FullName, out var newCachedFactors)) throw new ArgumentException("Given profile not supported.");
cachedFactors[i] = newCachedFactors;
}
var cachedFactor = cachedFactors[i][edgeProfileId];
if (cachedFactor.Value <= 0)
{ // edge is not accessible to this profile.
Expand Down Expand Up @@ -180,6 +205,12 @@ public Func<ushort, Factor> GetGetFactor(IProfileInstance profileInstance)
{
return (p) =>
{
if (p >= cachedFactors.Length)
{
this.CalculateFor(profileInstance.Profile);
if (!_edgeProfileFactors.TryGetValue(profileInstance.Profile.FullName, out cachedFactors)) throw new ArgumentException("Given profile not supported.");
}
var cachedFactor = cachedFactors[p];
if (profileInstance.IsConstrained(cachedFactor.Constraints))
{
Expand All @@ -190,6 +221,12 @@ public Func<ushort, Factor> GetGetFactor(IProfileInstance profileInstance)
}
return (p) =>
{
if (p >= cachedFactors.Length)
{
this.CalculateFor(profileInstance.Profile);
if (!_edgeProfileFactors.TryGetValue(profileInstance.Profile.FullName, out cachedFactors)) throw new ArgumentException("Given profile not supported.");
}
return cachedFactors[p].ToFactor();
};
}
Expand All @@ -205,6 +242,12 @@ public Func<ushort, FactorAndSpeed> GetGetFactorAndSpeed(IProfileInstance profil
{
return (p) =>
{
if (p >= cachedFactors.Length)
{
this.CalculateFor(profileInstance.Profile);
if (!_edgeProfileFactors.TryGetValue(profileInstance.Profile.FullName, out cachedFactors)) throw new ArgumentException("Given profile not supported.");
}
var cachedFactor = cachedFactors[p];
if (profileInstance.IsConstrained(cachedFactor.Constraints))
{
Expand All @@ -215,6 +258,12 @@ public Func<ushort, FactorAndSpeed> GetGetFactorAndSpeed(IProfileInstance profil
}
return (p) =>
{
if (p >= cachedFactors.Length)
{
this.CalculateFor(profileInstance.Profile);
if (!_edgeProfileFactors.TryGetValue(profileInstance.Profile.FullName, out cachedFactors)) throw new ArgumentException("Given profile not supported.");
}
return cachedFactors[p];
};
}
Expand Down

0 comments on commit b8a004a

Please sign in to comment.