Skip to content

Commit

Permalink
Remove some double dictionary lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Aug 12, 2023
1 parent 77a5e3c commit 5371d12
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions source/ContractConfigurator/Parameter/VesselParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ protected virtual bool SetState(Vessel vessel, Contracts.ParameterState state)
/// <returns>The state for the vessel.</returns>
protected virtual Contracts.ParameterState GetStateForVessel(Vessel vessel)
{
if (!vesselInfo.ContainsKey(vessel.id))
if (!vesselInfo.TryGetValue(vessel.id, out var inf))
{
return ParameterState.Incomplete;
}

return vesselInfo[vessel.id].state;
return inf.state;
}

/// <summary>
Expand All @@ -236,9 +236,9 @@ public virtual void SetState(Vessel vessel)
{
LoggingUtil.LogVerbose(this, "SetState to that of vessel {0}", (vessel != null ? vessel.id.ToString() : "null"));

if (vessel != null && vesselInfo.ContainsKey(vessel.id))
if (vessel != null && vesselInfo.TryGetValue(vessel.id, out var inf))
{
this.state = vesselInfo[vessel.id].state;
this.state = inf.state;
}
else
{
Expand All @@ -253,9 +253,9 @@ public virtual void SetState(Vessel vessel)
/// <returns>Vessel state</returns>
public virtual Contracts.ParameterState GetState(Vessel vessel)
{
if (vesselInfo.ContainsKey(vessel.id))
if (vesselInfo.TryGetValue(vessel.id, out var inf))
{
return vesselInfo[vessel.id].state;
return inf.state;
}
else
{
Expand All @@ -271,9 +271,9 @@ public virtual Contracts.ParameterState GetState(Vessel vessel)
/// <returns>The time that the vessel completed the parameter</returns>
public virtual double GetCompletionTime(Vessel vessel)
{
if (vesselInfo.ContainsKey(vessel.id))
if (vesselInfo.TryGetValue(vessel.id, out var inf))
{
return vesselInfo[vessel.id].completionTime;
return inf.completionTime;
}
else
{
Expand Down Expand Up @@ -341,10 +341,9 @@ protected virtual void OnCrewTransferred(GameEvents.HostedFromToAction<ProtoCrew
protected void NewEVA(Vessel parent, Vessel eva)
{
// Check if there's anything for the parent
if (vesselInfo.ContainsKey(parent.id))
if (vesselInfo.TryGetValue(parent.id, out var vi))
{
// If there's a completion, transfer that to the EVA
VesselInfo vi = vesselInfo[parent.id];
if (vi.state == ParameterState.Complete && vi.strength != ParamStrength.WEAK)
{
VesselInfo viEVA = new VesselInfo(eva.id, eva);
Expand All @@ -359,17 +358,15 @@ protected void NewEVA(Vessel parent, Vessel eva)
protected void ReturnEVA(Vessel parent, Vessel eva)
{
// Check if the EVA is interesting...
if (vesselInfo.ContainsKey(eva.id))
if (vesselInfo.TryGetValue(eva.id, out var viEVA))
{
// Initialize parent
if (!vesselInfo.ContainsKey(parent.id))
if (!vesselInfo.TryGetValue(parent.id, out var vi))
{
vesselInfo[parent.id] = new VesselInfo(parent.id, parent);
vi = vesselInfo[parent.id] = new VesselInfo(parent.id, parent);
}

// Copy to parent vessel
VesselInfo vi = vesselInfo[parent.id];
VesselInfo viEVA = vesselInfo[eva.id];
if (viEVA.state == ParameterState.Complete && vi.state != ParameterState.Complete)
{
vi.state = viEVA.state;
Expand Down Expand Up @@ -407,15 +404,15 @@ protected virtual void OnVesselCreate(Vessel vessel)
KeyValuePair<ParamStrength, double>? dockedInfo = null;
foreach (uint hash in vessel.GetHashes())
{
if (dockedVesselInfo.ContainsKey(hash))
if (dockedVesselInfo.TryGetValue(hash, out var dvi))
{
if (dockedInfo == null)
{
dockedInfo = dockedVesselInfo[hash];
dockedInfo = dvi;
}
else
{
dockedInfo = dockedVesselInfo[hash].Key > dockedInfo.Value.Key ? dockedVesselInfo[hash] : dockedInfo;
dockedInfo = dvi.Key > dockedInfo.Value.Key ? dvi : dockedInfo;
}
}
}
Expand Down Expand Up @@ -490,9 +487,9 @@ protected virtual void OnPartAttach(GameEvents.HostTargetAction<Part, Part> e)
}

// Get the vesselInfo structs
VesselInfo v1 = vesselInfo.ContainsKey(e.host.vessel.id) ? vesselInfo[e.host.vessel.id] : null;
VesselInfo v2 = vesselInfo.ContainsKey(e.target.vessel.id) ? vesselInfo[e.target.vessel.id] : null;

vesselInfo.TryGetValue(e.host.vessel.id, out var v1);
vesselInfo.TryGetValue(e.target.vessel.id, out var v2);
// Handle cases of untracked vessels
if (v1 == null && v2 == null)
{
Expand Down Expand Up @@ -576,25 +573,25 @@ private void OnContractAccepted(Contract c)
private void CheckVesselInfoModifications(Part part)
{
// Check if we need to make modifications based on undocking
if (vesselInfo.ContainsKey(part.vessel.id) && vesselInfo[part.vessel.id].state == ParameterState.Complete)
if (vesselInfo.TryGetValue(part.vessel.id, out var inf) && inf.state == ParameterState.Complete)
{
ParamStrength strength = vesselInfo[part.vessel.id].strength;
ParamStrength strength = inf.strength;

// Medium strengths indicates that we may need to lower to weak if the "strong"
// part is lost
if (strength == ParamStrength.MEDIUM)
{
foreach (uint hash in part.vessel.GetHashes())
{
strength = !dockedVesselInfo.ContainsKey(hash) ? ParamStrength.WEAK : dockedVesselInfo[hash].Key > strength ? dockedVesselInfo[hash].Key : strength;
strength = !dockedVesselInfo.TryGetValue(hash, out var dockInf) ? ParamStrength.WEAK : dockInf.Key > strength ? dockInf.Key : strength;
}

vesselInfo[part.vessel.id].strength = strength == ParamStrength.STRONG ? ParamStrength.MEDIUM : ParamStrength.WEAK;
inf.strength = strength == ParamStrength.STRONG ? ParamStrength.MEDIUM : ParamStrength.WEAK;
}
else if (strength == ParamStrength.STRONG)
{
// Save the sub vessel info
SaveSubVesselInfo(part.vessel, ParamStrength.STRONG, vesselInfo[part.vessel.id].completionTime);
SaveSubVesselInfo(part.vessel, ParamStrength.STRONG, inf.completionTime);
}
}
}
Expand All @@ -610,8 +607,8 @@ private void SaveSubVesselInfo(Vessel vessel, ParamStrength strength, double com
{
foreach (uint hash in vessel.GetHashes())
{
if (!dockedVesselInfo.ContainsKey(hash) ||
dockedVesselInfo[hash].Key < strength)
if (!dockedVesselInfo.TryGetValue(hash, out var dockInf) ||
dockInf.Key < strength)
{
dockedVesselInfo[hash] = new KeyValuePair<ParamStrength, double>(strength, completionTime);
}
Expand Down

0 comments on commit 5371d12

Please sign in to comment.