Skip to content

Commit e825750

Browse files
authored
Avoid Boxing/Unboxing on accessing elements of VBufferDataFrameColumn (fix merge issues) (dotnet#6867)
* Avoid Boxing/Unboxing on accessing elements of VBufferDataFrameColumn * Avoid boxing for vbuffer column
1 parent 766569b commit e825750

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/Microsoft.Data.Analysis/VBufferDataFrameColumn.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Microsoft.Data.Analysis
1818
/// </summary>
1919
public partial class VBufferDataFrameColumn<T> : DataFrameColumn, IEnumerable<VBuffer<T>>
2020
{
21-
2221
public static int MaxCapacity = ArrayUtility.ArrayMaxSize / Unsafe.SizeOf<VBuffer<T>>();
2322

2423
private readonly List<List<VBuffer<T>>> _vBuffers = new List<List<VBuffer<T>>>(); // To store more than intMax number of vbuffers
@@ -56,9 +55,7 @@ public VBufferDataFrameColumn(string name, IEnumerable<VBuffer<T>> values) : bas
5655
}
5756
}
5857

59-
private long _nullCount;
60-
61-
public override long NullCount => _nullCount;
58+
public override long NullCount => 0;
6259

6360
protected internal override void Resize(long length)
6461
{
@@ -94,6 +91,11 @@ private int GetBufferIndexContainingRowIndex(long rowIndex)
9491
}
9592

9693
protected override object GetValue(long rowIndex)
94+
{
95+
return GetTypedValue(rowIndex);
96+
}
97+
98+
protected VBuffer<T> GetTypedValue(long rowIndex)
9799
{
98100
int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);
99101
return _vBuffers[bufferIndex][(int)(rowIndex % MaxCapacity)];
@@ -118,30 +120,30 @@ protected override IReadOnlyList<object> GetValues(long startIndex, int length)
118120

119121
protected override void SetValue(long rowIndex, object value)
120122
{
121-
if (value == null || value is VBuffer<T>)
123+
if (value == null)
122124
{
123-
int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);
124-
int bufferOffset = (int)(rowIndex % MaxCapacity);
125-
var oldValue = _vBuffers[bufferIndex][bufferOffset];
126-
_vBuffers[bufferIndex][bufferOffset] = (VBuffer<T>)value;
127-
if (!oldValue.Equals((VBuffer<T>)value))
128-
{
129-
if (value == null)
130-
_nullCount++;
131-
if (oldValue.Length == 0 && _nullCount > 0)
132-
_nullCount--;
133-
}
125+
throw new NotSupportedException("Null values are not supported by VBufferDataFrameColumn");
126+
}
127+
else if (value is VBuffer<T> vbuffer)
128+
{
129+
SetTypedValue(rowIndex, vbuffer);
134130
}
135131
else
136132
{
137133
throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(VBuffer<T>)), nameof(value));
138134
}
139135
}
140136

137+
protected void SetTypedValue(long rowIndex, VBuffer<T> value)
138+
{
139+
int bufferIndex = GetBufferIndexContainingRowIndex(rowIndex);
140+
_vBuffers[bufferIndex][(int)(rowIndex % MaxCapacity)] = value;
141+
}
142+
141143
public new VBuffer<T> this[long rowIndex]
142144
{
143-
get => (VBuffer<T>)GetValue(rowIndex);
144-
set => SetValue(rowIndex, value);
145+
get => GetTypedValue(rowIndex);
146+
set => SetTypedValue(rowIndex, value);
145147
}
146148

147149
/// <summary>

0 commit comments

Comments
 (0)