8000 Replace occurrences of params T[] with params ReadOnlySpan<T> where possible by h3xds1nz · Pull Request #9481 · dotnet/wpf · GitHub
[go: up one dir, main page]

Skip to content

Replace occurrences of params T[] with params ReadOnlySpan<T> where possible #9481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -584,24 +584,20 @@ internal static object FireContentRendered(object arg)
/// Helper method which returns true when all the given visuals
/// are in the same presentation source.
/// </summary>
internal static bool UnderSamePresentationSource(params DependencyObject[] visuals)
internal static bool IsUnderSamePresentationSource(params ReadOnlySpan<DependencyObject> visuals)
{
if (visuals == null || visuals.Length == 0)
{
if (visuals.IsEmpty)
return true;
}

PresentationSource baseSource = CriticalFromVisual(visuals[0]);

int count = visuals.Length;
for (int i = 1; i < count; i++)
for (int i = 1; i < visuals.Length; i++)
{
PresentationSource currentSource = CriticalFromVisual(visuals[i]);
if (currentSource != baseSource)
if (baseSource != CriticalFromVisual(visuals[i]))
{
return false;
}
}

return true;
}

Expand Down
< 10000 td id="diff-59d8856130dd4f78a5d2bf81ec71c40f86d1c5382cc1280afda89a482b77a676L104" data-line-number="104" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class CommandHelpers

internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler)
{
PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, null);
PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null);
}

internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
Expand All @@ -39,7 +39,7 @@ internal static void RegisterCommandHandler(Type controlType, RoutedCommand comm
internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
CanExecuteRoutedEventHandler canExecuteRoutedEventHandler)
{
PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, null);
PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler);
}

internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
Expand Down Expand Up @@ -90,7 +90,7 @@ internal static void RegisterCommandHandler(Type controlType, RoutedCommand comm

// 'params' based method is private. Call sites that use this bloat unwittingly due to implicit construction of the params array that goes into IL.
private static void PrivateRegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params InputGesture[] inputGestures)
CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params ReadOnlySpan<InputGesture> inputGestures)
{
// Validate parameters
Debug.Assert(controlType != null);
Expand All @@ -102,12 +102,9 @@ private static void PrivateRegisterCommandHandler(Type controlType, RoutedComman
CommandManager.RegisterClassCommandBinding(controlType, new CommandBinding(command, executedRoutedEventHandler, canExecuteRoutedEventHandler));

// Create additional input binding for this command
if (inputGestures != null)
for (int i = 0; i < inputGestures.Length; i++)
{
for (int i = 0; i < inputGestures.Length; i++)
{
CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i]));
}
CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void OnAnyMouseLeftButtonDownThunk(object sender, MouseButtonEven
private void OnAnyMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
// Ignore actions if the button down arises from a different presentation source
if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ private bool ShouldManipulateScroll(ManipulationStartingEventArgs e, ScrollConte
{
// If the original source is not from the same PresentationSource as of ScrollViewer,
// then do not start the manipulation.
if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12117,7 +12117,7 @@ static void Flush()
}

// for use from VS Immediate window
static void Mark(params object[] args)
static void Mark(params ReadOnlySpan<object> args)
{
ScrollTraceRecord record = new ScrollTraceRecord(ScrollTraceOp.Mark, null, -1, 0, 0, BuildDetail(args));
lock (s_TargetToTraceListMap)
Expand Down Expand Up @@ -12229,7 +12229,7 @@ internal static bool IsTracing(VirtualizingStackPanel vsp)
return (sti != null && sti.ScrollTracer != null);
}

internal static void Trace(VirtualizingStackPanel vsp, ScrollTraceOp op, params object[] args)
internal static void Trace(VirtualizingStackPanel vsp, ScrollTraceOp op, params ReadOnlySpan<object> args)
{
ScrollTracingInfo sti = ScrollTracingInfoField.GetValue(vsp);
ScrollTracer tracer = sti.ScrollTracer;
Expand Down Expand Up @@ -12272,16 +12272,12 @@ private static string DisplayType(object o)
return sb.ToString();
}

private static string BuildDetail(object[] args)
private static string BuildDetail(ReadOnlySpan<object> args)
{
int length = (args != null) ? args.Length : 0;
if (length == 0)
return String.Empty;
else
return String.Format(CultureInfo.InvariantCulture, s_format[length], args);
return args.IsEmpty ? string.Empty : string.Format(CultureInfo.InvariantCulture, s_format[args.Length], args);
}

private static string[] s_format = new string[] {
private static readonly string[] s_format = new string[] {
"",
"{0}",
"{0} {1}",
Expand Down Expand Up @@ -12392,7 +12388,7 @@ private void IdentifyTrace(ItemsControl ic, VirtualizingStackPanel vsp)
}
}

private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracingInfo sti, params object[] args)
private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracingInfo sti, params ReadOnlySpan<object> args)
{
// the trace list contains references back into the VSP that can lead
// to memory leaks if the app removes the VSP. To avoid this, treat
Expand All @@ -12403,10 +12399,8 @@ private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracin
{
if (++_luCount > _luThreshold)
{
AddTrace(null, ScrollTraceOp.ID, _nullInfo,
"Inactive at", DateTime.Now);
ItemsControl ic;
if (_wrIC.TryGetTarget(out ic))
AddTrace(null, ScrollTraceOp.ID, _nullInfo, "Inactive at", DateTime.Now);
if (_wrIC.TryGetTarget(out ItemsControl ic))
{
ic.LayoutUpdated -= OnLayoutUpdated;
}
Expand All @@ -12421,10 +12415,8 @@ private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracin

if (luCount < 0)
{
AddTrace(null, ScrollTraceOp.ID, _nullInfo,
"Reactivate at", DateTime.Now);
ItemsControl ic;
if (_wrIC.TryGetTarget(out ic))
AddTrace(null, ScrollTraceOp.ID, _nullInfo, "Reactivate at", DateTime.Now);
if (_wrIC.TryGetTarget(out ItemsControl ic))
{
ic.LayoutUpdated += OnLayoutUpdated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace System.Windows.Controls
{
/// <summary>
/// Names and helpers for visual states in the controls.
/// <remarks>THIS IS A SHARED FILE. PresentationFramework.Design.dll must be rebuilt if changed.</remarks>
/// </summary>
/// <summary> Names and helpers for visual states in the controls. </summary>
internal static class VisualStates
{
#region CalendarDayButton
Expand Down Expand Up @@ -414,9 +411,9 @@ internal static class VisualStates
/// Ordered list of state names and fallback states to transition into.
/// Only the first state to be found will be used.
/// </param>
public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
public static void GoToState(Control control, bool useTransitions, params ReadOnlySpan<string> stateNames)
{
if (stateNames == null)
if (stateNames.IsEmpty)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4755,7 +4755,7 @@ internal static void Flush()
}

// for use from VS Immediate window
internal static void Mark(params object[] args)
internal static void Mark(params ReadOnlySpan<object> args)
{
IMECompositionTraceRecord record = new IMECompositionTraceRecord(IMECompositionTraceOp.Mark, BuildDetail(args));
lock (s_TargetToTraceListMap)
Expand Down Expand Up @@ -4807,7 +4807,7 @@ internal static bool IsTracing(TextStore textStore)
return (cti != null && cti.IMECompositionTracer != null);
}

internal static void Trace(TextStore textStore, IMECompositionTraceOp op, params object[] args)
internal static void Trace(TextStore textStore, IMECompositionTraceOp op, params ReadOnlySpan<object> args)
{
IMECompositionTracingInfo cti = IMECompositionTracingInfoField.GetValue(textStore.UiScope);
IMECompositionTracer tracer = cti.IMECompositionTracer;
Expand Down Expand Up @@ -4850,16 +4850,12 @@ private static string DisplayType(object o)
return sb.ToString();
}

private static string BuildDetail(object[] args)
private static string BuildDetail(ReadOnlySpan<object> args)
{
int length = (args != null) ? args.Length : 0;
if (length == 0)
return String.Empty;
else
return String.Format(CultureInfo.InvariantCulture, s_format[length], args);
return args.IsEmpty ? string.Empty : string.Format(CultureInfo.InvariantCulture, s_format[args.Length], args);
}

private static string[] s_format = new string[] {
private static readonly string[] s_format = new string[] {
"",
"{0}",
"{0} {1}",
Expand Down Expand Up @@ -4924,7 +4920,7 @@ private void IdentifyTrace(TextStore textStore)
AddTrace(textStore, IMECompositionTraceOp.ID, _nullInfo, DisplayType(uiScope));
}

private void AddTrace(TextStore textStore, IMECompositionTraceOp op, IMECompositionTracingInfo cti, params object[] args)
private void AddTrace(TextStore textStore, IMECompositionTraceOp op, IMECompositionTracingInfo cti, params ReadOnlySpan<object> args)
{
// pop a E* op from the stack
if (IMECompositionTraceOp.FirstEndOp <= op && _opStack.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public enum PowerLineStatus
/// </summary>
public static class SystemParameters
{
public static event System.ComponentModel.PropertyChangedEventHandler StaticPropertyChanged;
public static event PropertyChangedEventHandler StaticPropertyChanged;

private static void OnPropertiesChanged(params string[] propertyNames)
private static void OnPropertiesChanged(params ReadOnlySpan<string> propertyNames)
{
System.ComponentModel.PropertyChangedEventHandler handler = StaticPropertyChanged;
if (handler != null)
PropertyChangedEventHandler handler = StaticPropertyChanged;
if (handler is not null)
{
for (int i=0; i<propertyNames.Length; ++i)
foreach (string propertyName in propertyNames)
{
handler(null, new System.ComponentModel.PropertyChangedEventArgs(propertyNames[i]));
handler.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,38 @@ internal Collection<Storyboard> CurrentStoryboards
}
}

internal void StartNewThenStopOld(FrameworkElement element, params Storyboard[] newStoryboards)
internal void StartNewThenStopOld(FrameworkElement element, params ReadOnlySpan<Storyboard> newStoryboards)
{
// Remove the old Storyboards. Remove is delayed until the next TimeManager tick, so the
// handoff to the new storyboard is unaffected.
for (int index = 0; index < CurrentStoryboards.Count; ++index)
for (int i = 0; i < CurrentStoryboards.Count; i++)
{
if (CurrentStoryboards[index] == null)
{
Storyboard currentStoryboard = CurrentStoryboards[i];
if (currentStoryboard is null)
continue;
}

CurrentStoryboards[index].Remove(element);
currentStoryboard.Remove(element);
}

CurrentStoryboards.Clear();

// Start the new Storyboards
for (int index = 0; index < newStoryboards.Length; ++index)
{
if (newStoryboards[index] == null)
{
foreach (Storyboard newStoryboard in newStoryboards)
{
if (newStoryboard is null)
continue;
}

newStoryboards[index].Begin(element, HandoffBehavior.SnapshotAndReplace, true);
newStoryboard.Begin(element, HandoffBehavior.SnapshotAndReplace, true);

// Hold on to the running Storyboards
CurrentStoryboards.Add(newStoryboards[index]);
CurrentStoryboards.Add(newStoryboard);

// Silverlight had an issue where initially, a checked CheckBox would not show the check mark
// until the second frame. They chose to do a Seek(0) at this point, which this line
// is supposed to mimic. It does not seem to be equivalent, though, and WPF ends up
// with some odd animation behavior. I haven't seen the CheckBox issue on WPF, so
// commenting this out for now.
// newStoryboards[index].SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime);
// newStoryboard.SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7038,7 +7038,7 @@ protected override void OnManipulationBoundaryFeedback(ManipulationBoundaryFeedb

// If the original source is not from the same PresentationSource as of the Window,
// then do not act on the PanningFeedback.
if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this))
{
return;
}
Expand Down
Loading
0