From 28f0759e9097a213b99aaede5b2f5245adabe9f7 Mon Sep 17 00:00:00 2001 From: George Vanburgh Date: Tue, 29 Oct 2024 21:35:57 -0400 Subject: [PATCH] Replace LINQ expression with for loop For code which repeatedly access columns by name, this LINQ expression can currently form part of the hot path. Replace the LINQ with the equivelent for loop. Some numbers from my machine | Method | Mean | Error | StdDev | Gen0 | Allocated | |------------------------ |---------:|----------:|----------:|-------:|----------:| | GetColumnByIndexLinq | 67.84 ns | 1.178 ns | 1.102 ns | 0.0107 | 136 B | | GetColumnByIndexForLoop | 9.428 ns | 0.1334 ns | 0.1114 ns | - | - | --- csharp/src/Apache.Arrow/Schema.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/csharp/src/Apache.Arrow/Schema.cs b/csharp/src/Apache.Arrow/Schema.cs index 4357e8b2ddd..32615e5d67b 100644 --- a/csharp/src/Apache.Arrow/Schema.cs +++ b/csharp/src/Apache.Arrow/Schema.cs @@ -82,7 +82,13 @@ public int GetFieldIndex(string name, IEqualityComparer comparer = defau { comparer ??= StringComparer.CurrentCulture; - return _fieldsList.IndexOf(_fieldsList.First(x => comparer.Equals(x.Name, name))); + for (int i = 0; i < _fieldsList.Count; i++) + { + if (comparer.Equals(_fieldsList[i].Name, name)) + return i; + } + + return -1; } public Schema RemoveField(int fieldIndex)