Skip to content

[C#] Consider replacing LINQ expression when accessing columns by name #44575

@georgevanburgh

Description

@georgevanburgh

Describe the enhancement requested

We've recently been doing some profiling of code that uses the C# Arrow library, and which frequently accesses columns by name in a tight loop - and found the following LINQ expression makes a significant contribution to execution time.

public int GetFieldIndex(string name, IEqualityComparer<string> comparer = default)
{
comparer ??= StringComparer.CurrentCulture;
return _fieldsList.IndexOf(_fieldsList.First(x => comparer.Equals(x.Name, name)));
}

Obviously there are workarounds for this, such as creating our own cached mapping of column names to ordinals - but it would be nice if the built-in method was a little more efficient. In addition to the use of linq, we're also iterating through _fieldsList twice (once to find a match, and then another to find the index of the match).

We've also noticed that usage of StringComparer.CurrentCulture causes an allocation every time this method is called (assuming a value for comparer is not provided). I'm assuming the choice to use a culture-aware comparison by default is intentional?

Component(s)

C#

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions