Use compound assignment in /interpreter and /runtime (#17727)

This commit is contained in:
Dmitry Volodin
2022-07-25 16:55:56 -07:00
committed by GitHub
parent 5f4ac678e3
commit 9fe96f3c20
11 changed files with 43 additions and 152 deletions
@@ -110,10 +110,7 @@ namespace System.Management.Automation.Interpreter
if (_targetIndex == UnknownIndex)
{
if (_forwardBranchFixups == null)
{
_forwardBranchFixups = new List<int>();
}
_forwardBranchFixups ??= new List<int>();
_forwardBranchFixups.Add(branchIndex);
}
@@ -157,10 +157,7 @@ namespace System.Management.Automation.Interpreter
{
get
{
if (s_caches == null)
{
s_caches = new Instruction[2][][] { new Instruction[2][], new Instruction[2][] };
}
s_caches ??= new Instruction[2][][] { new Instruction[2][], new Instruction[2][] };
return s_caches[ConsumedStack][ProducedStack] ?? (s_caches[ConsumedStack][ProducedStack] = new Instruction[CacheSize]);
}
@@ -119,10 +119,7 @@ namespace System.Management.Automation.Interpreter
{
if (elementCount < MaxArrayInitElementCountCache)
{
if (_newArrayInit == null)
{
_newArrayInit = new Instruction[MaxArrayInitElementCountCache];
}
_newArrayInit ??= new Instruction[MaxArrayInitElementCountCache];
return _newArrayInit[elementCount] ?? (_newArrayInit[elementCount] = new NewArrayInitInstruction<T>(elementCount));
}
@@ -233,10 +233,7 @@ namespace System.Management.Automation.Interpreter
public void SetDebugCookie(object cookie)
{
#if DEBUG
if (_debugCookies == null)
{
_debugCookies = new List<KeyValuePair<int, object>>();
}
_debugCookies ??= new List<KeyValuePair<int, object>>();
Debug.Assert(Count > 0);
_debugCookies.Add(new KeyValuePair<int, object>(Count - 1, cookie));
@@ -372,10 +369,7 @@ namespace System.Management.Automation.Interpreter
int i = (int)value;
if (i >= PushIntMinCachedValue && i <= PushIntMaxCachedValue)
{
if (s_ints == null)
{
s_ints = new Instruction[PushIntMaxCachedValue - PushIntMinCachedValue + 1];
}
s_ints ??= new Instruction[PushIntMaxCachedValue - PushIntMinCachedValue + 1];
i -= PushIntMinCachedValue;
Emit(s_ints[i] ?? (s_ints[i] = new LoadObjectInstruction(value)));
@@ -387,10 +381,7 @@ namespace System.Management.Automation.Interpreter
if (_objects == null)
{
_objects = new List<object>();
if (s_loadObjectCached == null)
{
s_loadObjectCached = new Instruction[CachedObjectCount];
}
s_loadObjectCached ??= new Instruction[CachedObjectCount];
}
if (_objects.Count < s_loadObjectCached.Length)
@@ -451,10 +442,7 @@ namespace System.Management.Automation.Interpreter
public void EmitLoadLocal(int index)
{
if (s_loadLocal == null)
{
s_loadLocal = new Instruction[LocalInstrCacheSize];
}
s_loadLocal ??= new Instruction[LocalInstrCacheSize];
if (index < s_loadLocal.Length)
{
@@ -473,10 +461,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction LoadLocalBoxed(int index)
{
if (s_loadLocalBoxed == null)
{
s_loadLocalBoxed = new Instruction[LocalInstrCacheSize];
}
s_loadLocalBoxed ??= new Instruction[LocalInstrCacheSize];
if (index < s_loadLocalBoxed.Length)
{
@@ -490,10 +475,7 @@ namespace System.Management.Automation.Interpreter
public void EmitLoadLocalFromClosure(int index)
{
if (s_loadLocalFromClosure == null)
{
s_loadLocalFromClosure = new Instruction[LocalInstrCacheSize];
}
s_loadLocalFromClosure ??= new Instruction[LocalInstrCacheSize];
if (index < s_loadLocalFromClosure.Length)
{
@@ -507,10 +489,7 @@ namespace System.Management.Automation.Interpreter
public void EmitLoadLocalFromClosureBoxed(int index)
{
if (s_loadLocalFromClosureBoxed == null)
{
s_loadLocalFromClosureBoxed = new Instruction[LocalInstrCacheSize];
}
s_loadLocalFromClosureBoxed ??= new Instruction[LocalInstrCacheSize];
if (index < s_loadLocalFromClosureBoxed.Length)
{
@@ -524,10 +503,7 @@ namespace System.Management.Automation.Interpreter
public void EmitAssignLocal(int index)
{
if (s_assignLocal == null)
{
s_assignLocal = new Instruction[LocalInstrCacheSize];
}
s_assignLocal ??= new Instruction[LocalInstrCacheSize];
if (index < s_assignLocal.Length)
{
@@ -541,10 +517,7 @@ namespace System.Management.Automation.Interpreter
public void EmitStoreLocal(int index)
{
if (s_storeLocal == null)
{
s_storeLocal = new Instruction[LocalInstrCacheSize];
}
s_storeLocal ??= new Instruction[LocalInstrCacheSize];
if (index < s_storeLocal.Length)
{
@@ -563,10 +536,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction AssignLocalBoxed(int index)
{
if (s_assignLocalBoxed == null)
{
s_assignLocalBoxed = new Instruction[LocalInstrCacheSize];
}
s_assignLocalBoxed ??= new Instruction[LocalInstrCacheSize];
if (index < s_assignLocalBoxed.Length)
{
@@ -585,10 +555,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction StoreLocalBoxed(int index)
{
if (s_storeLocalBoxed == null)
{
s_storeLocalBoxed = new Instruction[LocalInstrCacheSize];
}
s_storeLocalBoxed ??= new Instruction[LocalInstrCacheSize];
if (index < s_storeLocalBoxed.Length)
{
@@ -602,10 +569,7 @@ namespace System.Management.Automation.Interpreter
public void EmitAssignLocalToClosure(int index)
{
if (s_assignLocalToClosure == null)
{
s_assignLocalToClosure = new Instruction[LocalInstrCacheSize];
}
s_assignLocalToClosure ??= new Instruction[LocalInstrCacheSize];
if (index < s_assignLocalToClosure.Length)
{
@@ -647,10 +611,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction Parameter(int index)
{
if (s_parameter == null)
{
s_parameter = new Instruction[LocalInstrCacheSize];
}
s_parameter ??= new Instruction[LocalInstrCacheSize];
if (index < s_parameter.Length)
{
@@ -662,10 +623,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction ParameterBox(int index)
{
if (s_parameterBox == null)
{
s_parameterBox = new Instruction[LocalInstrCacheSize];
}
s_parameterBox ??= new Instruction[LocalInstrCacheSize];
if (index < s_parameterBox.Length)
{
@@ -677,10 +635,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction InitReference(int index)
{
if (s_initReference == null)
{
s_initReference = new Instruction[LocalInstrCacheSize];
}
s_initReference ??= new Instruction[LocalInstrCacheSize];
if (index < s_initReference.Length)
{
@@ -692,10 +647,7 @@ namespace System.Management.Automation.Interpreter
internal static Instruction InitImmutableRefBox(int index)
{
if (s_initImmutableRefBox == null)
{
s_initImmutableRefBox = new Instruction[LocalInstrCacheSize];
}
s_initImmutableRefBox ??= new Instruction[LocalInstrCacheSize];
if (index < s_initImmutableRefBox.Length)
{
@@ -1127,10 +1079,7 @@ namespace System.Management.Automation.Interpreter
public BranchLabel MakeLabel()
{
if (_labels == null)
{
_labels = new List<BranchLabel>();
}
_labels ??= new List<BranchLabel>();
var label = new BranchLabel();
_labels.Add(label);
@@ -176,10 +176,7 @@ namespace System.Management.Automation.Interpreter
private void EnsureLabel(LightCompiler compiler)
{
if (_label == null)
{
_label = compiler.Instructions.MakeLabel();
}
_label ??= compiler.Instructions.MakeLabel();
}
private bool DefinedIn(LabelScopeInfo scope)
@@ -359,10 +356,7 @@ namespace System.Management.Automation.Interpreter
{
Debug.Assert(CanJumpInto);
if (_labels == null)
{
_labels = new HybridReferenceDictionary<LabelTarget, LabelInfo>();
}
_labels ??= new HybridReferenceDictionary<LabelTarget, LabelInfo>();
_labels[target] = info;
}
@@ -1132,10 +1132,7 @@ namespace System.Management.Automation.Interpreter
Debug.Assert(label != null);
}
if (label == null)
{
label = DefineLabel(node.Target);
}
label ??= DefineLabel(node.Target);
if (node.DefaultValue != null)
{
@@ -163,10 +163,7 @@ namespace System.Management.Automation.Interpreter
if (_variables.TryGetValue(variable, out existing))
{
newScope = new VariableScope(result, start, existing);
if (existing.ChildScopes == null)
{
existing.ChildScopes = new List<VariableScope>();
}
existing.ChildScopes ??= new List<VariableScope>();
existing.ChildScopes.Add(newScope);
}
@@ -296,10 +293,7 @@ namespace System.Management.Automation.Interpreter
internal LocalVariable AddClosureVariable(ParameterExpression variable)
{
if (_closureVariables == null)
{
_closureVariables = new Dictionary<ParameterExpression, LocalVariable>();
}
_closureVariables ??= new Dictionary<ParameterExpression, LocalVariable>();
LocalVariable result = new LocalVariable(_closureVariables.Count, true, false);
_closureVariables.Add(variable, result);
@@ -374,10 +374,7 @@ namespace System.Management.Automation.Interpreter
private ParameterExpression AddTemp(ParameterExpression variable)
{
if (_temps == null)
{
_temps = new List<ParameterExpression>();
}
_temps ??= new List<ParameterExpression>();
_temps.Add(variable);
return variable;
@@ -5343,13 +5343,9 @@ namespace System.Management.Automation.Language
if (!isGeneric || genericTypeArg != null)
{
var temp = Expression.Variable(typeof(object));
if (expr == null)
{
// If expr is not null, it's the fallback when no member exists. If it is null,
// the fallback is the result from PropertyDoesntExist.
expr = (errorSuggestion ?? PropertyDoesntExist(target, restrictions)).Expression;
}
// If expr is not null, it's the fallback when no member exists. If it is null,
// the fallback is the result from PropertyDoesntExist.
expr ??= (errorSuggestion ?? PropertyDoesntExist(target, restrictions)).Expression;
var method = isGeneric
? CachedReflectionInfo.PSGetMemberBinder_TryGetGenericDictionaryValue.MakeGenericMethod(genericTypeArg)
@@ -5691,10 +5687,7 @@ namespace System.Management.Automation.Language
if (alias != null)
{
aliasConversionType = alias.ConversionType;
if (aliasRestrictions == null)
{
aliasRestrictions = new List<BindingRestrictions>();
}
aliasRestrictions ??= new List<BindingRestrictions>();
memberInfo = ResolveAlias(alias, target, aliases, aliasRestrictions);
if (memberInfo == null)
@@ -5745,10 +5738,7 @@ namespace System.Management.Automation.Language
var methodInfo = member as MethodInfo;
if (methodInfo != null && (methodInfo.IsPublic || methodInfo.IsFamily))
{
if (candidateMethods == null)
{
candidateMethods = new List<MethodBase>();
}
candidateMethods ??= new List<MethodBase>();
candidateMethods.Add(methodInfo);
}
@@ -5841,10 +5831,7 @@ namespace System.Management.Automation.Language
}
var adapterSet = PSObject.GetMappedAdapter(obj, context?.TypeTable);
if (memberInfo == null)
{
memberInfo = adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
}
memberInfo ??= adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
if (memberInfo == null && adapterSet.DotNetAdapter != null)
{
@@ -6445,10 +6432,7 @@ namespace System.Management.Automation.Language
}
var adapterSet = PSObject.GetMappedAdapter(obj, context?.TypeTable);
if (memberInfo == null)
{
memberInfo = adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
}
memberInfo ??= adapterSet.OriginalAdapter.BaseGetMember<PSMemberInfo>(obj, member);
if (memberInfo == null && adapterSet.DotNetAdapter != null)
{
@@ -7260,14 +7244,11 @@ namespace System.Management.Automation.Language
private static DynamicMetaObject GetTargetAsEnumerable(DynamicMetaObject target)
{
var enumerableTarget = PSEnumerableBinder.IsEnumerable(target);
if (enumerableTarget == null)
{
// Wrap the target in an array.
enumerableTarget = PSEnumerableBinder.IsEnumerable(
new DynamicMetaObject(
Expression.NewArrayInit(typeof(object), target.Expression.Cast(typeof(object))),
target.GetSimpleTypeRestriction()));
}
// If null wrap the target in an array.
enumerableTarget ??= PSEnumerableBinder.IsEnumerable(
new DynamicMetaObject(
Expression.NewArrayInit(typeof(object), target.Expression.Cast(typeof(object))),
target.GetSimpleTypeRestriction()));
return enumerableTarget;
}
@@ -366,10 +366,7 @@ namespace System.Management.Automation
{
get
{
if (_isProductCode == null)
{
_isProductCode = SecuritySupport.IsProductBinary(((Ast)_ast).Extent.File);
}
_isProductCode ??= SecuritySupport.IsProductBinary(((Ast)_ast).Extent.File);
return _isProductCode.Value;
}
@@ -1015,10 +1012,7 @@ namespace System.Management.Automation
createLocalScope || variablesToDefine == null,
"When calling ScriptBlock.InvokeWithContext(), if 'variablesToDefine' != null then 'createLocalScope' must be true");
if (args == null)
{
args = Array.Empty<object>();
}
args ??= Array.Empty<object>();
bool runOptimized = context._debuggingMode <= 0 && createLocalScope;
var codeToInvoke = GetCodeToInvoke(ref runOptimized, clauseToInvoke);
@@ -1027,11 +1021,8 @@ namespace System.Management.Automation
return;
}
if (outputPipe == null)
{
// If we don't have a pipe to write to, we need to discard all results.
outputPipe = new Pipe { NullPipe = true };
}
// If we don't have a pipe to write to, we need to discard all results.
outputPipe ??= new Pipe { NullPipe = true };
var locals = MakeLocalsTuple(runOptimized);
@@ -230,10 +230,7 @@ namespace System.Management.Automation
{
ExecutionContext.CheckStackDepth();
if (args == null)
{
args = Array.Empty<object>();
}
args ??= Array.Empty<object>();
// Perform validations on the ScriptBlock. GetSimplePipeline can allow for more than one
// pipeline if the first parameter is true, but Invoke-Command doesn't yet support multiple