Skip to content

Commit

Permalink
Use an index lookup for O(1) field index access
Browse files Browse the repository at this point in the history
  • Loading branch information
vthemelis committed Nov 4, 2024
1 parent 45b3697 commit 6b77dd7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion csharp/src/Apache.Arrow/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public partial class Schema : IRecordType
private readonly List<Field> _fieldsList;

public ILookup<string, Field> FieldsLookup { get; }
private ILookup<string, int> _fieldsIndexLookup { get; }

public IReadOnlyDictionary<string, string> Metadata { get; }

Expand All @@ -54,6 +55,10 @@ public Schema(
_fieldsDictionary = FieldsLookup.ToDictionary(g => g.Key, g => g.First());

Metadata = metadata?.ToDictionary(kv => kv.Key, kv => kv.Value);

_fieldsIndexLookup = _fieldsList
.Select((x, idx) => (Item: x, Index: idx))
.ToLookup(x => x.Item.Name, x => x.Index, StringComparer.CurrentCulture);
}

internal Schema(List<Field> fieldsList, IReadOnlyDictionary<string, string> metadata, bool copyCollections)
Expand All @@ -80,7 +85,10 @@ public int GetFieldIndex(string name, StringComparer comparer)

public int GetFieldIndex(string name, IEqualityComparer<string> comparer = default)
{
comparer ??= StringComparer.CurrentCulture;
if (comparer == null)
{
return _fieldsIndexLookup[name].First();
}

return _fieldsList.IndexOf(_fieldsList.First(x => comparer.Equals(x.Name, name)));
}
Expand Down

0 comments on commit 6b77dd7

Please sign in to comment.