Skip to content

Commit 0a9747b

Browse files
committed
Respond to PR feedback.
1 parent 33cac0c commit 0a9747b

File tree

23 files changed

+63
-81
lines changed

23 files changed

+63
-81
lines changed

src/Microsoft.ML.Core/Data/MetadataUtils.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ public static void GetSlotNames(RoleMappedSchema schema, RoleMappedSchema.Column
319319
IReadOnlyList<ColumnInfo> list;
320320
if ((list = schema?.GetColumns(role)) == null || list.Count != 1 || !schema.Schema.HasSlotNames(list[0].Index, vectorSize))
321321
{
322-
slotNames = VBufferMutationContext.Create(ref slotNames, vectorSize, 0)
323-
.CreateBuffer();
322+
VBufferUtils.Resize(ref slotNames, vectorSize, 0);
324323
}
325324
else
326325
schema.Schema.GetMetadata(Kinds.SlotNames, list[0].Index, ref slotNames);

src/Microsoft.ML.Core/Data/VBufferMutationContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ internal VBufferMutationContext(int logicalLength,
132132
/// with a larger physical value count than was needed
133133
/// because the final value count was not known at creation time.
134134
/// </param>
135-
/// <returns></returns>
135+
/// <returns>
136+
/// The newly created <see cref="VBuffer{T}"/>.
137+
/// </returns>
136138
public VBuffer<T> CreateBuffer(int? physicalValuesCount = null)
137139
{
138140
int count = Values.Length;

src/Microsoft.ML.Core/Utilities/Utils.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,7 @@ public static int Count<TSource>(this ReadOnlySpan<TSource> source, Func<TSource
11141114
for (int i = 0; i < source.Length; i++)
11151115
{
11161116
if (predicate(source[i]))
1117-
{
11181117
result++;
1119-
}
11201118
}
11211119
return result;
11221120
}
@@ -1128,9 +1126,7 @@ public static bool All<TSource>(this ReadOnlySpan<TSource> source, Func<TSource,
11281126
for (int i = 0; i < source.Length; i++)
11291127
{
11301128
if (!predicate(source[i]))
1131-
{
11321129
return false;
1133-
}
11341130
}
11351131
return true;
11361132
}

src/Microsoft.ML.Core/Utilities/VBufferUtils.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,7 @@ public static void ForEachEitherDefined<T>(in VBuffer<T> a, in VBuffer<T> b, Act
314314
/// </summary>
315315
public static void Clear<T>(ref VBuffer<T> dst)
316316
{
317-
int dstValuesCount = dst.GetValues().Length;
318-
if (dstValuesCount == 0)
319-
return;
320-
var mutation = VBufferMutationContext.Create(ref dst, dst.Length, dstValuesCount);
317+
var mutation = VBufferMutationContext.CreateFromBuffer(ref dst);
321318
mutation.Values.Clear();
322319
}
323320

@@ -346,8 +343,7 @@ public static void Apply<T>(ref VBuffer<T> dst, SlotValueManipulator<T> manip)
346343
{
347344
Contracts.CheckValue(manip, nameof(manip));
348345

349-
int dstValuesCount = dst.GetValues().Length;
350-
var mutation = VBufferMutationContext.Create(ref dst, dst.Length, dstValuesCount);
346+
var mutation = VBufferMutationContext.CreateFromBuffer(ref dst);
351347
if (dst.IsDense)
352348
{
353349
for (int i = 0; i < mutation.Values.Length; i++)
@@ -381,8 +377,8 @@ public static void ApplyAt<T>(ref VBuffer<T> dst, int slot, SlotValueManipulator
381377
Contracts.CheckValue(manip, nameof(manip));
382378
Contracts.CheckValueOrNull(pred);
383379

384-
int dstValuesCount = dst.GetValues().Length;
385-
var mutation = VBufferMutationContext.Create(ref dst, dst.Length, dstValuesCount);
380+
var mutation = VBufferMutationContext.CreateFromBuffer(ref dst);
381+
int dstValuesCount = mutation.Values.Length;
386382
if (dst.IsDense)
387383
{
388384
// The vector is dense, so we can just do a direct access.
@@ -963,8 +959,7 @@ private static void ApplyWithCoreCopy<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuf
963959
{
964960
if (srcValues.Length == 0)
965961
{
966-
res = VBufferMutationContext.Create(ref res, length, 0)
967-
.CreateBuffer();
962+
Resize(ref res, length, 0);
968963
}
969964
else if (src.IsDense)
970965
{
@@ -1209,8 +1204,7 @@ public static void ApplyIntoEitherDefined<TSrc, TDst>(in VBuffer<TSrc> src, ref
12091204
// equal lengths, but I don't care here.
12101205
if (srcValues.Length == 0)
12111206
{
1212-
dst = VBufferMutationContext.Create(ref dst, src.Length, 0)
1213-
.CreateBuffer();
1207+
Resize(ref dst, src.Length, 0);
12141208
return;
12151209
}
12161210
var mutation = VBufferMutationContext.Create(ref dst,
@@ -1263,8 +1257,7 @@ public static void ApplyInto<TSrc1, TSrc2, TDst>(in VBuffer<TSrc1> a, in VBuffer
12631257
if (aValues.Length == 0 && bValues.Length == 0)
12641258
{
12651259
// Case 1. Output will be empty.
1266-
dst = VBufferMutationContext.Create(ref dst, a.Length, 0)
1267-
.CreateBuffer();
1260+
Resize(ref dst, a.Length, 0);
12681261
return;
12691262
}
12701263

@@ -1441,5 +1434,15 @@ public static void Copy<T>(List<T> src, ref VBuffer<T> dst, int length)
14411434
}
14421435
dst = mutation.CreateBuffer();
14431436
}
1437+
1438+
/// <summary>
1439+
/// Updates the logical length and number of physical values to be represented in
1440+
/// <paramref name="dst"/>, while preserving the underlying buffers.
1441+
/// </summary>
1442+
public static void Resize<T>(ref VBuffer<T> dst, int newLogicalLength, int? valuesCount = null)
1443+
{
1444+
dst = VBufferMutationContext.Create(ref dst, newLogicalLength, valuesCount)
1445+
.CreateBuffer();
1446+
}
14441447
}
14451448
}

src/Microsoft.ML.Data/Data/BufferBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ public void GetResult(ref VBuffer<T> buffer)
434434
{
435435
if (IsEmpty)
436436
{
437-
buffer = VBufferMutationContext.Create(ref buffer, _length, 0)
438-
.CreateBuffer();
437+
VBufferUtils.Resize(ref buffer, _length, 0);
439438
return;
440439
}
441440

src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ public void Get(ref VBuffer<TItem> dst)
403403

404404
if (_count == 0)
405405
{
406-
dst = VBufferMutationContext.Create(ref dst, _size, 0)
407-
.CreateBuffer();
406+
VBufferUtils.Resize(ref dst, _size, 0);
408407
return;
409408
}
410409

src/Microsoft.ML.Data/DataView/Transposer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,8 +1308,7 @@ private ValueGetter<VBuffer<T>> CreateGetter(int col)
13081308
int scount = slim - smin;
13091309
if (scount == 0)
13101310
{
1311-
value = VBufferMutationContext.Create(ref value, len, 0)
1312-
.CreateBuffer();
1311+
VBufferUtils.Resize(ref value, len, 0);
13131312
return;
13141313
}
13151314

src/Microsoft.ML.Data/Depricated/Vector/VBufferMathUtils.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ public static void ScaleBy(in VBuffer<Float> src, ref VBuffer<Float> dst, Float
108108
if (count == 0)
109109
{
110110
// dst is a zero vector.
111-
dst = VBufferMutationContext.Create(ref dst, length, 0)
112-
.CreateBuffer();
111+
VBufferUtils.Resize(ref dst, length, 0);
113112
return;
114113
}
115114

@@ -392,14 +391,13 @@ public static void ScaleInto(in VBuffer<Float> src, Float c, ref VBuffer<Float>
392391
{
393392
// Due to sparsity preservation from src, dst must be dense, in the same way.
394393
var mutation = VBufferMutationContext.Create(ref dst, src.Length);
395-
if (!mutation.CreatedNewValues) // We need to clear it
394+
if (!mutation.CreatedNewValues) // We need to clear it.
396395
mutation.Values.Clear();
397396
dst = mutation.CreateBuffer();
398397
}
399398
else
400399
{
401-
dst = VBufferMutationContext.Create(ref dst, src.Length, 0)
402-
.CreateBuffer();
400+
VBufferUtils.Resize(ref dst, src.Length, 0);
403401
}
404402
}
405403
else if (c == -1)

src/Microsoft.ML.Data/Transforms/DropSlotsTransform.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,7 @@ private ValueGetter<VBuffer<TDst>> MakeVecTrivialGetter<TDst>()
701701
// Delegates onto instance methods are more efficient than delegates onto static methods.
702702
private void VecTrivialGetter<TDst>(ref VBuffer<TDst> value)
703703
{
704-
value = VBufferMutationContext.Create(ref value, 1, 0)
705-
.CreateBuffer();
704+
VBufferUtils.Resize(ref value, 1, 0);
706705
}
707706

708707
private Delegate MakeVecGetter(IRow input, int iinfo)

src/Microsoft.ML.Data/Transforms/HashTransform.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,7 @@ private static ValueGetter<VBuffer<uint>> MakeVectorHashGetter<T, THash>(uint se
746746
var srcValues = src.GetValues();
747747
if (srcValues.Length == 0)
748748
{
749-
dst = VBufferMutationContext.Create(ref dst, src.Length, 0)
750-
.CreateBuffer();
749+
VBufferUtils.Resize(ref dst, src.Length, 0);
751750
return;
752751
}
753752
var mutation = VBufferMutationContext.Create(ref dst, src.Length, srcValues.Length);
@@ -809,8 +808,7 @@ private static ValueGetter<VBuffer<uint>> MakeVectorOrderedHashGetter<T, THash>(
809808
var srcValues = src.GetValues();
810809
if (srcValues.Length == 0)
811810
{
812-
dst = VBufferMutationContext.Create(ref dst, src.Length, 0)
813-
.CreateBuffer();
811+
VBufferUtils.Resize(ref dst, src.Length, 0);
814812
return;
815813
}
816814
var mutation = VBufferMutationContext.Create(ref dst, src.Length, srcValues.Length);

0 commit comments

Comments
 (0)