From bff3438a5a9ea86158972968d383f12a8a9f95f8 Mon Sep 17 00:00:00 2001 From: Phil Scott Date: Sat, 27 Feb 2021 00:05:40 -0500 Subject: [PATCH] using loop instead of linq In both of these loops context is captured preventing caching of the lambda. this results in a pretty significant amount of allocations especially with progress bars that constantly are remeasuring --- src/Spectre.Console/Internal/Cell.cs | 8 +++++++- src/Spectre.Console/Rendering/Segment.cs | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Spectre.Console/Internal/Cell.cs b/src/Spectre.Console/Internal/Cell.cs index 1a8a0ce..c904757 100644 --- a/src/Spectre.Console/Internal/Cell.cs +++ b/src/Spectre.Console/Internal/Cell.cs @@ -8,7 +8,13 @@ namespace Spectre.Console { public static int GetCellLength(RenderContext context, string text) { - return text.Sum(rune => GetCellLength(context, rune)); + var sum = 0; + foreach (var rune in text) + { + sum += GetCellLength(context, rune); + } + + return sum; } public static int GetCellLength(RenderContext context, char rune) diff --git a/src/Spectre.Console/Rendering/Segment.cs b/src/Spectre.Console/Rendering/Segment.cs index 6b64837..ba6f5ff 100644 --- a/src/Spectre.Console/Rendering/Segment.cs +++ b/src/Spectre.Console/Rendering/Segment.cs @@ -135,7 +135,13 @@ namespace Spectre.Console.Rendering throw new ArgumentNullException(nameof(segments)); } - return segments.Sum(segment => segment.CellCount(context)); + var sum = 0; + foreach (var segment in segments) + { + sum += segment.CellCount(context); + } + + return sum; } ///