Skip to content

Commit 9fe96f3

Browse files
authored
Use compound assignment in /interpreter and /runtime (PowerShell#17727)
1 parent 5f4ac67 commit 9fe96f3

File tree

11 files changed

+43
-152
lines changed

11 files changed

+43
-152
lines changed

src/System.Management.Automation/engine/interpreter/BranchLabel.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ internal void AddBranch(InstructionList instructions, int branchIndex)
110110

111111
if (_targetIndex == UnknownIndex)
112112
{
113-
if (_forwardBranchFixups == null)
114-
{
115-
_forwardBranchFixups = new List<int>();
116-
}
113+
_forwardBranchFixups ??= new List<int>();
117114

118115
_forwardBranchFixups.Add(branchIndex);
119116
}

src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ public override Instruction[] Cache
157157
{
158158
get
159159
{
160-
if (s_caches == null)
161-
{
162-
s_caches = new Instruction[2][][] { new Instruction[2][], new Instruction[2][] };
163-
}
160+
s_caches ??= new Instruction[2][][] { new Instruction[2][], new Instruction[2][] };
164161

165162
return s_caches[ConsumedStack][ProducedStack] ?? (s_caches[ConsumedStack][ProducedStack] = new Instruction[CacheSize]);
166163
}

src/System.Management.Automation/engine/interpreter/InstructionFactory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ protected internal override Instruction NewArrayInit(int elementCount)
119119
{
120120
if (elementCount < MaxArrayInitElementCountCache)
121121
{
122-
if (_newArrayInit == null)
123-
{
124-
_newArrayInit = new Instruction[MaxArrayInitElementCountCache];
125-
}
122+
_newArrayInit ??= new Instruction[MaxArrayInitElementCountCache];
126123

127124
return _newArrayInit[elementCount] ?? (_newArrayInit[elementCount] = new NewArrayInitInstruction<T>(elementCount));
128125
}

src/System.Management.Automation/engine/interpreter/InstructionList.cs

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ private void UpdateStackDepth(Instruction instruction)
233233
public void SetDebugCookie(object cookie)
234234
{
235235
#if DEBUG
236-
if (_debugCookies == null)
237-
{
238-
_debugCookies = new List<KeyValuePair<int, object>>();
239-
}
236+
_debugCookies ??= new List<KeyValuePair<int, object>>();
240237

241238
Debug.Assert(Count > 0);
242239
_debugCookies.Add(new KeyValuePair<int, object>(Count - 1, cookie));
@@ -372,10 +369,7 @@ public void EmitLoad(object value, Type type)
372369
int i = (int)value;
373370
if (i >= PushIntMinCachedValue && i <= PushIntMaxCachedValue)
374371
{
375-
if (s_ints == null)
376-
{
377-
s_ints = new Instruction[PushIntMaxCachedValue - PushIntMinCachedValue + 1];
378-
}
372+
s_ints ??= new Instruction[PushIntMaxCachedValue - PushIntMinCachedValue + 1];
379373

380374
i -= PushIntMinCachedValue;
381375
Emit(s_ints[i] ?? (s_ints[i] = new LoadObjectInstruction(value)));
@@ -387,10 +381,7 @@ public void EmitLoad(object value, Type type)
387381
if (_objects == null)
388382
{
389383
_objects = new List<object>();
390-
if (s_loadObjectCached == null)
391-
{
392-
s_loadObjectCached = new Instruction[CachedObjectCount];
393-
}
384+
s_loadObjectCached ??= new Instruction[CachedObjectCount];
394385
}
395386

396387
if (_objects.Count < s_loadObjectCached.Length)
@@ -451,10 +442,7 @@ internal void SwitchToBoxed(int index, int instructionIndex)
451442

452443
public void EmitLoadLocal(int index)
453444
{
454-
if (s_loadLocal == null)
455-
{
456-
s_loadLocal = new Instruction[LocalInstrCacheSize];
457-
}
445+
s_loadLocal ??= new Instruction[LocalInstrCacheSize];
458446

459447
if (index < s_loadLocal.Length)
460448
{
@@ -473,10 +461,7 @@ public void EmitLoadLocalBoxed(int index)
473461

474462
internal static Instruction LoadLocalBoxed(int index)
475463
{
476-
if (s_loadLocalBoxed == null)
477-
{
478-
s_loadLocalBoxed = new Instruction[LocalInstrCacheSize];
479-
}
464+
s_loadLocalBoxed ??= new Instruction[LocalInstrCacheSize];
480465

481466
if (index < s_loadLocalBoxed.Length)
482467
{
@@ -490,10 +475,7 @@ internal static Instruction LoadLocalBoxed(int index)
490475

491476
public void EmitLoadLocalFromClosure(int index)
492477
{
493-
if (s_loadLocalFromClosure == null)
494-
{
495-
s_loadLocalFromClosure = new Instruction[LocalInstrCacheSize];
496-
}
478+
s_loadLocalFromClosure ??= new Instruction[LocalInstrCacheSize];
497479

498480
if (index < s_loadLocalFromClosure.Length)
499481
{
@@ -507,10 +489,7 @@ public void EmitLoadLocalFromClosure(int index)
507489

508490
public void EmitLoadLocalFromClosureBoxed(int index)
509491
{
510-
if (s_loadLocalFromClosureBoxed == null)
511-
{
512-
s_loadLocalFromClosureBoxed = new Instruction[LocalInstrCacheSize];
513-
}
492+
s_loadLocalFromClosureBoxed ??= new Instruction[LocalInstrCacheSize];
514493

515494
if (index < s_loadLocalFromClosureBoxed.Length)
516495
{
@@ -524,10 +503,7 @@ public void EmitLoadLocalFromClosureBoxed(int index)
524503

525504
public void EmitAssignLocal(int index)
526505
{
527-
if (s_assignLocal == null)
528-
{
529-
s_assignLocal = new Instruction[LocalInstrCacheSize];
530-
}
506+
s_assignLocal ??= new Instruction[LocalInstrCacheSize];
531507

532508
if (index < s_assignLocal.Length)
533509
{
@@ -541,10 +517,7 @@ public void EmitAssignLocal(int index)
541517

542518
public void EmitStoreLocal(int index)
543519
{
544-
if (s_storeLocal == null)
545-
{
546-
s_storeLocal = new Instruction[LocalInstrCacheSize];
547-
}
520+
s_storeLocal ??= new Instruction[LocalInstrCacheSize];
548521

549522
if (index < s_storeLocal.Length)
550523
{
@@ -563,10 +536,7 @@ public void EmitAssignLocalBoxed(int index)
563536

564537
internal static Instruction AssignLocalBoxed(int index)
565538
{
566-
if (s_assignLocalBoxed == null)
567-
{
568-
s_assignLocalBoxed = new Instruction[LocalInstrCacheSize];
569-
}
539+
s_assignLocalBoxed ??= new Instruction[LocalInstrCacheSize];
570540

571541
if (index < s_assignLocalBoxed.Length)
572542
{
@@ -585,10 +555,7 @@ public void EmitStoreLocalBoxed(int index)
585555

586556
internal static Instruction StoreLocalBoxed(int index)
587557
{
588-
if (s_storeLocalBoxed == null)
589-
{
590-
s_storeLocalBoxed = new Instruction[LocalInstrCacheSize];
591-
}
558+
s_storeLocalBoxed ??= new Instruction[LocalInstrCacheSize];
592559

593560
if (index < s_storeLocalBoxed.Length)
594561
{
@@ -602,10 +569,7 @@ internal static Instruction StoreLocalBoxed(int index)
602569

603570
public void EmitAssignLocalToClosure(int index)
604571
{
605-
if (s_assignLocalToClosure == null)
606-
{
607-
s_assignLocalToClosure = new Instruction[LocalInstrCacheSize];
608-
}
572+
s_assignLocalToClosure ??= new Instruction[LocalInstrCacheSize];
609573

610574
if (index < s_assignLocalToClosure.Length)
611575
{
@@ -647,10 +611,7 @@ internal void EmitInitializeParameter(int index)
647611

648612
internal static Instruction Parameter(int index)
649613
{
650-
if (s_parameter == null)
651-
{
652-
s_parameter = new Instruction[LocalInstrCacheSize];
653-
}
614+
s_parameter ??= new Instruction[LocalInstrCacheSize];
654615

655616
if (index < s_parameter.Length)
656617
{
@@ -662,10 +623,7 @@ internal static Instruction Parameter(int index)
662623

663624
internal static Instruction ParameterBox(int index)
664625
{
665-
if (s_parameterBox == null)
666-
{
667-
s_parameterBox = new Instruction[LocalInstrCacheSize];
668-
}
626+
s_parameterBox ??= new Instruction[LocalInstrCacheSize];
669627

670628
if (index < s_parameterBox.Length)
671629
{
@@ -677,10 +635,7 @@ internal static Instruction ParameterBox(int index)
677635

678636
internal static Instruction InitReference(int index)
679637
{
680-
if (s_initReference == null)
681-
{
682-
s_initReference = new Instruction[LocalInstrCacheSize];
683-
}
638+
s_initReference ??= new Instruction[LocalInstrCacheSize];
684639

685640
if (index < s_initReference.Length)
686641
{
@@ -692,10 +647,7 @@ internal static Instruction InitReference(int index)
692647

693648
internal static Instruction InitImmutableRefBox(int index)
694649
{
695-
if (s_initImmutableRefBox == null)
696-
{
697-
s_initImmutableRefBox = new Instruction[LocalInstrCacheSize];
698-
}
650+
s_initImmutableRefBox ??= new Instruction[LocalInstrCacheSize];
699651

700652
if (index < s_initImmutableRefBox.Length)
701653
{
@@ -1127,10 +1079,7 @@ private RuntimeLabel[] BuildRuntimeLabels()
11271079

11281080
public BranchLabel MakeLabel()
11291081
{
1130-
if (_labels == null)
1131-
{
1132-
_labels = new List<BranchLabel>();
1133-
}
1082+
_labels ??= new List<BranchLabel>();
11341083

11351084
var label = new BranchLabel();
11361085
_labels.Add(label);

src/System.Management.Automation/engine/interpreter/LabelInfo.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ internal void ValidateFinish()
176176

177177
private void EnsureLabel(LightCompiler compiler)
178178
{
179-
if (_label == null)
180-
{
181-
_label = compiler.Instructions.MakeLabel();
182-
}
179+
_label ??= compiler.Instructions.MakeLabel();
183180
}
184181

185182
private bool DefinedIn(LabelScopeInfo scope)
@@ -359,10 +356,7 @@ internal void AddLabelInfo(LabelTarget target, LabelInfo info)
359356
{
360357
Debug.Assert(CanJumpInto);
361358

362-
if (_labels == null)
363-
{
364-
_labels = new HybridReferenceDictionary<LabelTarget, LabelInfo>();
365-
}
359+
_labels ??= new HybridReferenceDictionary<LabelTarget, LabelInfo>();
366360

367361
_labels[target] = info;
368362
}

src/System.Management.Automation/engine/interpreter/LightCompiler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,7 @@ private void CompileLabelExpression(Expression expr)
11321132
Debug.Assert(label != null);
11331133
}
11341134

1135-
if (label == null)
1136-
{
1137-
label = DefineLabel(node.Target);
1138-
}
1135+
label ??= DefineLabel(node.Target);
11391136

11401137
if (node.DefaultValue != null)
11411138
{

src/System.Management.Automation/engine/interpreter/LocalVariables.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ public LocalDefinition DefineLocal(ParameterExpression variable, int start)
163163
if (_variables.TryGetValue(variable, out existing))
164164
{
165165
newScope = new VariableScope(result, start, existing);
166-
if (existing.ChildScopes == null)
167-
{
168-
existing.ChildScopes = new List<VariableScope>();
169-
}
166+
existing.ChildScopes ??= new List<VariableScope>();
170167

171168
existing.ChildScopes.Add(newScope);
172169
}
@@ -296,10 +293,7 @@ internal Dictionary<ParameterExpression, LocalVariable> ClosureVariables
296293

297294
internal LocalVariable AddClosureVariable(ParameterExpression variable)
298295
{
299-
if (_closureVariables == null)
300-
{
301-
_closureVariables = new Dictionary<ParameterExpression, LocalVariable>();
302-
}
296+
_closureVariables ??= new Dictionary<ParameterExpression, LocalVariable>();
303297

304298
LocalVariable result = new LocalVariable(_closureVariables.Count, true, false);
305299
_closureVariables.Add(variable, result);

src/System.Management.Automation/engine/interpreter/LoopCompiler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,7 @@ private Expression VisitVariable(ParameterExpression node, ExpressionAccess acce
374374

375375
private ParameterExpression AddTemp(ParameterExpression variable)
376376
{
377-
if (_temps == null)
378-
{
379-
_temps = new List<ParameterExpression>();
380-
}
377+
_temps ??= new List<ParameterExpression>();
381378

382379
_temps.Add(variable);
383380
return variable;

src/System.Management.Automation/engine/runtime/Binding/Binders.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5343,13 +5343,9 @@ public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, Dy
53435343
if (!isGeneric || genericTypeArg != null)
53445344
{
53455345
var temp = Expression.Variable(typeof(object));
5346-
if (expr == null)
5347-
{
5348-
// If expr is not null, it's the fallback when no member exists. If it is null,
5349-
// the fallback is the result from PropertyDoesntExist.
5350-
5351-
expr = (errorSuggestion ?? PropertyDoesntExist(target, restrictions)).Expression;
5352-
}
5346+
// If expr is not null, it's the fallback when no member exists. If it is null,
5347+
// the fallback is the result from PropertyDoesntExist.
5348+
expr ??= (errorSuggestion ?? PropertyDoesntExist(target, restrictions)).Expression;
53535349

53545350
var method = isGeneric
53555351
? CachedReflectionInfo.PSGetMemberBinder_TryGetGenericDictionaryValue.MakeGenericMethod(genericTypeArg)
@@ -5691,10 +5687,7 @@ internal PSMemberInfo GetPSMemberInfo(DynamicMetaObject target,
56915687
if (alias != null)
56925688
{
56935689
aliasConversionType = alias.ConversionType;
5694-
if (aliasRestrictions == null)
5695-
{
5696-
aliasRestrictions = new List<BindingRestrictions>();
5697-
}
5690+
aliasRestrictions ??= new List<BindingRestrictions>();
56985691

56995692
memberInfo = ResolveAlias(alias, target, aliases, aliasRestrictions);
57005693
if (memberInfo == null)
@@ -5745,10 +5738,7 @@ internal PSMemberInfo GetPSMemberInfo(DynamicMetaObject target,
57455738
var methodInfo = member as MethodInfo;
57465739
if (methodInfo != null && (methodInfo.IsPublic || methodInfo.IsFamily))
57475740
{
5748-
if (candidateMethods == null)
5749-
{
5750-
candidateMethods = new List<MethodBase>();
5751-
}
5741+
candidateMethods ??= new List<MethodBase>();
57525742

57535743
candidateMethods.Add(methodInfo);
57545744
}
@@ -5841,10 +5831,7 @@ internal static object GetAdaptedValue(object obj, string member)
58415831
}
58425832

58435833
var adapterSet = PSObject.GetMappedAdapter(obj, context?.TypeTable);
5844-
if (memberInfo == null)
5845-
{
5846-
memberInfo = adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
5847-
}
5834+
memberInfo ??= adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
58485835

58495836
if (memberInfo == null && adapterSet.DotNetAdapter != null)
58505837
{
@@ -6445,10 +6432,7 @@ internal static object SetAdaptedValue(object obj, string member, object value)
64456432
}
64466433

64476434
var adapterSet = PSObject.GetMappedAdapter(obj, context?.TypeTable);
6448-
if (memberInfo == null)
6449-
{
6450-
memberInfo = adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
6451-
}
6435+
memberInfo ??= adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
64526436

64536437
if (memberInfo == null && adapterSet.DotNetAdapter != null)
64546438
{
@@ -7260,14 +7244,11 @@ private DynamicMetaObject InvokeMemberOnCollection(DynamicMetaObject targetEnume
72607244
private static DynamicMetaObject GetTargetAsEnumerable(DynamicMetaObject target)
72617245
{
72627246
var enumerableTarget = PSEnumerableBinder.IsEnumerable(target);
7263-
if (enumerableTarget == null)
7264-
{
7265-
// Wrap the target in an array.
7266-
enumerableTarget = PSEnumerableBinder.IsEnumerable(
7267-
new DynamicMetaObject(
7268-
Expression.NewArrayInit(typeof(object), target.Expression.Cast(typeof(object))),
7269-
target.GetSimpleTypeRestriction()));
7270-
}
7247+
// If null wrap the target in an array.
7248+
enumerableTarget ??= PSEnumerableBinder.IsEnumerable(
7249+
new DynamicMetaObject(
7250+
Expression.NewArrayInit(typeof(object), target.Expression.Cast(typeof(object))),
7251+
target.GetSimpleTypeRestriction()));
72717252

72727253
return enumerableTarget;
72737254
}

0 commit comments

Comments
 (0)