Skip to content

Commit 0aeb6ea

Browse files
#136 Insufficient performance 5
1 parent e8ad645 commit 0aeb6ea

4 files changed

Lines changed: 247 additions & 116 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Pure.DI.Core.Code;
2+
3+
interface IVarStateTracker
4+
{
5+
void OnStateChanging(int bindingId);
6+
}

src/Pure.DI.Core/Core/Code/Var.cs

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ record Var(
1818
private string? _baseName;
1919
private string? _rootBaseName;
2020
private readonly IDependencyNode _abstractNode = Declaration.Node;
21+
private bool _isCreated = IsCreatedDefault(Declaration.Node);
22+
private bool _isLocalFunctionCalled;
23+
private IVarStateTracker? _stateTracker;
2124

2225
/// <summary>
2326
/// Gets the type of the instance.
@@ -67,7 +70,16 @@ public string Name
6770
public string CodeExpression
6871
{
6972
get => _codeExpression ?? Name;
70-
set => _codeExpression = value;
73+
set
74+
{
75+
if (string.Equals(_codeExpression, value, StringComparison.Ordinal))
76+
{
77+
return;
78+
}
79+
80+
_stateTracker?.OnStateChanging(_abstractNode.BindingId);
81+
_codeExpression = value;
82+
}
7183
}
7284

7385
internal string? RawCodeExpression => _codeExpression;
@@ -93,12 +105,38 @@ public string CodeExpression
93105
/// <summary>
94106
/// Gets or sets a value indicating whether the variable has been created.
95107
/// </summary>
96-
public bool IsCreated { get; set; } = IsCreatedDefault(Declaration.Node);
108+
public bool IsCreated
109+
{
110+
get => _isCreated;
111+
set
112+
{
113+
if (_isCreated == value)
114+
{
115+
return;
116+
}
117+
118+
_stateTracker?.OnStateChanging(_abstractNode.BindingId);
119+
_isCreated = value;
120+
}
121+
}
97122

98123
/// <summary>
99124
/// Gets or sets a value indicating whether the local function was called in the current path.
100125
/// </summary>
101-
public bool IsLocalFunctionCalled { get; set; }
126+
public bool IsLocalFunctionCalled
127+
{
128+
get => _isLocalFunctionCalled;
129+
set
130+
{
131+
if (_isLocalFunctionCalled == value)
132+
{
133+
return;
134+
}
135+
136+
_stateTracker?.OnStateChanging(_abstractNode.BindingId);
137+
_isLocalFunctionCalled = value;
138+
}
139+
}
102140

103141
/// <summary>
104142
/// Resets the variable to its default state, including generated code.
@@ -109,9 +147,9 @@ public bool ResetToDefaults()
109147
{
110148
var changed = false;
111149
var defaultCreated = IsCreatedDefault(_abstractNode);
112-
if (defaultCreated != IsCreated)
150+
if (defaultCreated != _isCreated)
113151
{
114-
IsCreated = defaultCreated;
152+
_isCreated = defaultCreated;
115153
changed = true;
116154
}
117155

@@ -133,9 +171,9 @@ public bool ResetToDefaults()
133171
changed = true;
134172
}
135173

136-
if (IsLocalFunctionCalled)
174+
if (_isLocalFunctionCalled)
137175
{
138-
IsLocalFunctionCalled = false;
176+
_isLocalFunctionCalled = false;
139177
changed = true;
140178
}
141179

@@ -157,9 +195,9 @@ public bool ResetStateToDefaults(bool resetLocalFunctionCalled)
157195
{
158196
var changed = false;
159197
var defaultCreated = IsCreatedDefault(_abstractNode);
160-
if (defaultCreated != IsCreated)
198+
if (defaultCreated != _isCreated)
161199
{
162-
IsCreated = defaultCreated;
200+
_isCreated = defaultCreated;
163201
changed = true;
164202
}
165203

@@ -171,15 +209,28 @@ public bool ResetStateToDefaults(bool resetLocalFunctionCalled)
171209
}
172210

173211
// ReSharper disable once InvertIf
174-
if (resetLocalFunctionCalled && IsLocalFunctionCalled)
212+
if (resetLocalFunctionCalled && _isLocalFunctionCalled)
175213
{
176-
IsLocalFunctionCalled = false;
214+
_isLocalFunctionCalled = false;
177215
changed = true;
178216
}
179217

180218
return changed;
181219
}
182220

221+
internal void AttachStateTracker(IVarStateTracker stateTracker) => _stateTracker = stateTracker;
222+
223+
internal void RestorePathState(bool isCreated, bool isLocalFunctionCalled, string? codeExpression, bool restoreLocalFunctionCalled)
224+
{
225+
_isCreated = isCreated;
226+
if (restoreLocalFunctionCalled)
227+
{
228+
_isLocalFunctionCalled = isLocalFunctionCalled;
229+
}
230+
231+
_codeExpression = codeExpression;
232+
}
233+
183234
public override string ToString() => $"{InstanceType} {Name}";
184235

185236
private static bool IsCreatedDefault(IDependencyNode node) =>

src/Pure.DI.Core/Core/Code/VarDeclaration.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ record VarDeclaration(
1212
IDependencyNode Node)
1313
{
1414
private readonly Lazy<string> _name = new(() => NameProvider.GetVariableName(Node, PerLifetimeId));
15+
private bool _isDeclared = IsDeclaredDefault(Node);
16+
private IVarStateTracker? _stateTracker;
1517

1618
/// <summary>
1719
/// Gets or sets a value indicating whether the variable has been declared.
1820
/// </summary>
19-
public bool IsDeclared { get; set; } = IsDeclaredDefault(Node) ;
21+
public bool IsDeclared
22+
{
23+
get => _isDeclared;
24+
set
25+
{
26+
if (_isDeclared == value)
27+
{
28+
return;
29+
}
30+
31+
_stateTracker?.OnStateChanging(Node.BindingId);
32+
_isDeclared = value;
33+
}
34+
}
2035

2136
/// <summary>
2237
/// Gets the type of the instance.
@@ -35,12 +50,12 @@ record VarDeclaration(
3550
public bool ResetToDefaults()
3651
{
3752
var declaredDefault = IsDeclaredDefault(Node);
38-
if (declaredDefault == IsDeclared)
53+
if (declaredDefault == _isDeclared)
3954
{
4055
return false;
4156
}
4257

43-
IsDeclared = declaredDefault;
58+
_isDeclared = declaredDefault;
4459
return true;
4560
}
4661

@@ -50,8 +65,12 @@ public bool ResetToDefaults()
5065
/// <returns>True if the state has changed.</returns>
5166
public bool ResetStateToDefaults() => ResetToDefaults();
5267

68+
internal void AttachStateTracker(IVarStateTracker stateTracker) => _stateTracker = stateTracker;
69+
70+
internal void RestoreDeclaredState(bool isDeclared) => _isDeclared = isDeclared;
71+
5372
public override string ToString() => $"{InstanceType} {Name}";
5473

5574
private static bool IsDeclaredDefault(IDependencyNode node) =>
5675
node.ActualLifetime is Lifetime.Singleton or Lifetime.Scoped or Lifetime.PerResolve || node.Arg is not null;
57-
}
76+
}

0 commit comments

Comments
 (0)