Skip to content

Commit a8bc268

Browse files
eerhardtTomFinley
authored andcommitted
Convert ValueMapper to use 'in' parameters (#1475)
* Convert ValueMapper`2 to use `in src` * Convert ValueMapper`3 to use 'in' parameter.
1 parent 71c9ff3 commit a8bc268

File tree

51 files changed

+321
-321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+321
-321
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Microsoft.ML.Runtime.Data
99
/// <summary>
1010
/// Delegate type to map/convert a value.
1111
/// </summary>
12-
public delegate void ValueMapper<TSrc, TDst>(ref TSrc src, ref TDst dst);
12+
public delegate void ValueMapper<TSrc, TDst>(in TSrc src, ref TDst dst);
1313

1414
/// <summary>
1515
/// Delegate type to map/convert among three values, for example, one input with two
1616
/// outputs, or two inputs with one output.
1717
/// </summary>
18-
public delegate void ValueMapper<TVal1, TVal2, TVal3>(ref TVal1 val1, ref TVal2 val2, ref TVal3 val3);
18+
public delegate void ValueMapper<TVal1, TVal2, TVal3>(in TVal1 val1, ref TVal2 val2, ref TVal3 val3);
1919

2020
/// <summary>
2121
/// Interface for mapping a single input value (of an indicated ColumnType) to

src/Microsoft.ML.Data/Commands/ShowSchemaCommand.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private static void ShowMetadataValue<T>(IndentingTextWriter itw, ISchema schema
239239
var value = default(T);
240240
var sb = default(StringBuilder);
241241
schema.GetMetadata(kind, col, ref value);
242-
conv(ref value, ref sb);
242+
conv(in value, ref sb);
243243

244244
itw.Write(": '{0}'", sb);
245245
}
@@ -292,7 +292,7 @@ private static void ShowMetadataValueVec<T>(IndentingTextWriter itw, ISchema sch
292292
else
293293
itw.Write(", ");
294294
var val = item.Value;
295-
conv(ref val, ref sb);
295+
conv(in val, ref sb);
296296
itw.Write("[{0}] '{1}'", item.Key, sb);
297297
count++;
298298
}

src/Microsoft.ML.Data/Data/Conversion.cs

+119-119
Large diffs are not rendered by default.

src/Microsoft.ML.Data/Data/DataViewUtils.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ public static ValueGetter<ReadOnlyMemory<char>> GetSingleValueGetter<T>(IRow cur
13421342
if (!Conversions.Instance.TryGetStringConversion<T>(colType, out conversion))
13431343
{
13441344
var error = $"Cannot display {colType}";
1345-
conversion = (ref T src, ref StringBuilder builder) =>
1345+
conversion = (in T src, ref StringBuilder builder) =>
13461346
{
13471347
if (builder == null)
13481348
builder = new StringBuilder();
@@ -1357,7 +1357,7 @@ public static ValueGetter<ReadOnlyMemory<char>> GetSingleValueGetter<T>(IRow cur
13571357
(ref ReadOnlyMemory<char> value) =>
13581358
{
13591359
floatGetter(ref v);
1360-
conversion(ref v, ref dst);
1360+
conversion(in v, ref dst);
13611361
string text = dst.ToString();
13621362
value = text.AsMemory();
13631363
};
@@ -1384,7 +1384,7 @@ public static ValueGetter<ReadOnlyMemory<char>> GetVectorFlatteningGetter<T>(IRo
13841384
x =>
13851385
{
13861386
var v = x.Value;
1387-
conversion(ref v, ref dst);
1387+
conversion(in v, ref dst);
13881388
return dst.ToString();
13891389
}));
13901390
value = string.Format("<{0}{1}>", stringRep, suffix).AsMemory();

src/Microsoft.ML.Data/Data/RowCursorUtils.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private static ValueGetter<TDst> GetGetterAsCore<TSrc, TDst>(ColumnType typeSrc,
9999
(ref TDst dst) =>
100100
{
101101
getter(ref src);
102-
conv(ref src, ref dst);
102+
conv(in src, ref dst);
103103
};
104104
}
105105

@@ -134,7 +134,7 @@ private static ValueGetter<StringBuilder> GetGetterAsStringBuilderCore<TSrc>(Col
134134
(ref StringBuilder dst) =>
135135
{
136136
getter(ref src);
137-
conv(ref src, ref dst);
137+
conv(in src, ref dst);
138138
};
139139
}
140140

@@ -278,7 +278,7 @@ private static ValueGetter<VBuffer<TDst>> GetVecGetterAsCore<TSrc, TDst>(VectorT
278278
// REVIEW: This would be faster if there were loops for each std conversion.
279279
// Consider adding those to the Conversions class.
280280
for (int i = 0; i < count; i++)
281-
conv(ref src.Values[i], ref values[i]);
281+
conv(in src.Values[i], ref values[i]);
282282

283283
if (!src.IsDense)
284284
{

src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ private TableOfContentsEntry CreateRowIndexEntry(string rowIndexName)
12281228
int count = _header.RowCount <= int.MaxValue ? (int)_header.RowCount : 0;
12291229
KeyType type = new KeyType(DataKind.U8, 0, count);
12301230
// We are mapping the row index as expressed as a long, into a key value, so we must increment by one.
1231-
ValueMapper<long, ulong> mapper = (ref long src, ref ulong dst) => dst = (ulong)(src + 1);
1231+
ValueMapper<long, ulong> mapper = (in long src, ref ulong dst) => dst = (ulong)(src + 1);
12321232
var entry = new TableOfContentsEntry(this, rowIndexName, type, mapper);
12331233
return entry;
12341234
}
@@ -1710,7 +1710,7 @@ private void Get(ref T value)
17101710
{
17111711
Ectx.Check(_curr != null, _badCursorState);
17121712
long src = _curr.RowIndexLim - _remaining - 1;
1713-
_mapper(ref src, ref value);
1713+
_mapper(in src, ref value);
17141714
}
17151715

17161716
public override Delegate GetGetter()

src/Microsoft.ML.Data/DataLoadSave/PartitionedFileLoader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ private ValueGetter<TValue> GetterDelegateCore<TValue>(int col, ColumnType type)
610610

611611
return (ref TValue value) =>
612612
{
613-
conv(ref _colValues[col], ref value);
613+
conv(in _colValues[col], ref value);
614614
};
615615
}
616616

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ public int GatherFields(ReadOnlyMemory<char> lineSpan, ReadOnlySpan<char> span,
10151015
int csrc = default;
10161016
try
10171017
{
1018-
Conversions.Instance.Convert(ref spanT, ref csrc);
1018+
Conversions.Instance.Convert(in spanT, ref csrc);
10191019
}
10201020
catch
10211021
{

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,28 @@ protected ValueWriterBase(PrimitiveType type, int source, char sep)
116116
Conv = Conversions.Instance.GetStringConversion<T>(type);
117117

118118
var d = default(T);
119-
Conv(ref d, ref Sb);
119+
Conv(in d, ref Sb);
120120
Default = Sb.ToString();
121121
}
122122

123-
protected void MapText(ref ReadOnlyMemory<char> src, ref StringBuilder sb)
123+
protected void MapText(in ReadOnlyMemory<char> src, ref StringBuilder sb)
124124
{
125125
TextSaverUtils.MapText(src.Span, ref sb, Sep);
126126
}
127127

128-
protected void MapTimeSpan(ref TimeSpan src, ref StringBuilder sb)
128+
protected void MapTimeSpan(in TimeSpan src, ref StringBuilder sb)
129129
{
130-
TextSaverUtils.MapTimeSpan(ref src, ref sb);
130+
TextSaverUtils.MapTimeSpan(in src, ref sb);
131131
}
132132

133-
protected void MapDateTime(ref DateTime src, ref StringBuilder sb)
133+
protected void MapDateTime(in DateTime src, ref StringBuilder sb)
134134
{
135-
TextSaverUtils.MapDateTime(ref src, ref sb);
135+
TextSaverUtils.MapDateTime(in src, ref sb);
136136
}
137137

138-
protected void MapDateTimeZone(ref DateTimeOffset src, ref StringBuilder sb)
138+
protected void MapDateTimeZone(in DateTimeOffset src, ref StringBuilder sb)
139139
{
140-
TextSaverUtils.MapDateTimeZone(ref src, ref sb);
140+
TextSaverUtils.MapDateTimeZone(in src, ref sb);
141141
}
142142
}
143143

@@ -170,15 +170,15 @@ public override void WriteData(Action<StringBuilder, int> appendItem, out int le
170170
{
171171
for (int i = 0; i < _src.Length; i++)
172172
{
173-
Conv(ref _src.Values[i], ref Sb);
173+
Conv(in _src.Values[i], ref Sb);
174174
appendItem(Sb, i);
175175
}
176176
}
177177
else
178178
{
179179
for (int i = 0; i < _src.Count; i++)
180180
{
181-
Conv(ref _src.Values[i], ref Sb);
181+
Conv(in _src.Values[i], ref Sb);
182182
appendItem(Sb, _src.Indices[i]);
183183
}
184184
}
@@ -195,7 +195,7 @@ public override void WriteHeader(Action<StringBuilder, int> appendItem, out int
195195
var name = _slotNames.Values[i];
196196
if (name.IsEmpty)
197197
continue;
198-
MapText(ref name, ref Sb);
198+
MapText(in name, ref Sb);
199199
int index = _slotNames.IsDense ? i : _slotNames.Indices[i];
200200
appendItem(Sb, index);
201201
}
@@ -218,15 +218,15 @@ public ValueWriter(IRowCursor cursor, PrimitiveType type, int source, char sep)
218218
public override void WriteData(Action<StringBuilder, int> appendItem, out int length)
219219
{
220220
_getSrc(ref _src);
221-
Conv(ref _src, ref Sb);
221+
Conv(in _src, ref Sb);
222222
appendItem(Sb, 0);
223223
length = 1;
224224
}
225225

226226
public override void WriteHeader(Action<StringBuilder, int> appendItem, out int length)
227227
{
228228
var span = _columnName.AsMemory();
229-
MapText(ref span, ref Sb);
229+
MapText(in span, ref Sb);
230230
appendItem(Sb, 0);
231231
length = 1;
232232
}
@@ -846,7 +846,7 @@ internal static void MapText(ReadOnlySpan<char> span, ref StringBuilder sb, char
846846
}
847847
}
848848

849-
internal static void MapTimeSpan(ref TimeSpan src, ref StringBuilder sb)
849+
internal static void MapTimeSpan(in TimeSpan src, ref StringBuilder sb)
850850
{
851851
if (sb == null)
852852
sb = new StringBuilder();
@@ -856,7 +856,7 @@ internal static void MapTimeSpan(ref TimeSpan src, ref StringBuilder sb)
856856
sb.AppendFormat("\"{0:c}\"", src);
857857
}
858858

859-
internal static void MapDateTime(ref DateTime src, ref StringBuilder sb)
859+
internal static void MapDateTime(in DateTime src, ref StringBuilder sb)
860860
{
861861
if (sb == null)
862862
sb = new StringBuilder();
@@ -866,7 +866,7 @@ internal static void MapDateTime(ref DateTime src, ref StringBuilder sb)
866866
sb.AppendFormat("\"{0:o}\"", src);
867867
}
868868

869-
internal static void MapDateTimeZone(ref DateTimeOffset src, ref StringBuilder sb)
869+
internal static void MapDateTimeZone(in DateTimeOffset src, ref StringBuilder sb)
870870
{
871871
if (sb == null)
872872
sb = new StringBuilder();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ protected override Delegate GetGetterCore(IChannel ch, IRow input, int iinfo, ou
165165
(ref T2 v2) =>
166166
{
167167
getSrc(ref v1);
168-
_map1(ref v1, ref v2);
168+
_map1(in v1, ref v2);
169169
};
170170
return getter;
171171
}
@@ -178,8 +178,8 @@ protected override Delegate GetGetterCore(IChannel ch, IRow input, int iinfo, ou
178178
(ref T3 v3) =>
179179
{
180180
getSrc(ref v1);
181-
_map1(ref v1, ref v2);
182-
_map2(ref v2, ref v3);
181+
_map1(in v1, ref v2);
182+
_map2(in v2, ref v3);
183183
};
184184
return getter;
185185
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public RowCursor(Impl<T1, T2> parent, IRowCursor input, bool[] active)
170170
_pred =
171171
(in T1 src) =>
172172
{
173-
conv(ref _src, ref val);
173+
conv(in _src, ref val);
174174
return pred(in val);
175175
};
176176
}

src/Microsoft.ML.Data/Evaluators/EvaluatorUtils.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ private static IDataView AddTextColumn<TSrc>(IHostEnvironment env, IDataView inp
372372
{
373373
Contracts.Check(typeSrc.RawType == typeof(TSrc));
374374
return LambdaColumnMapper.Create(env, registrationName, input, inputColName, outputColName, typeSrc, TextType.Instance,
375-
(ref TSrc src, ref ReadOnlyMemory<char> dst) => dst = value.AsMemory());
375+
(in TSrc src, ref ReadOnlyMemory<char> dst) => dst = value.AsMemory());
376376
}
377377

378378
/// <summary>
@@ -406,7 +406,7 @@ private static IDataView AddKeyColumn<TSrc>(IHostEnvironment env, IDataView inpu
406406
{
407407
Contracts.Check(typeSrc.RawType == typeof(TSrc));
408408
return LambdaColumnMapper.Create(env, registrationName, input, inputColName, outputColName, typeSrc,
409-
new KeyType(DataKind.U4, 0, keyCount), (ref TSrc src, ref uint dst) =>
409+
new KeyType(DataKind.U4, 0, keyCount), (in TSrc src, ref uint dst) =>
410410
{
411411
if (value < 0 || value > keyCount)
412412
dst = 0;
@@ -507,7 +507,7 @@ public static IDataView AddFoldIndex(IHostEnvironment env, IDataView input, int
507507
if (def.Equals(default(T)))
508508
{
509509
mapper =
510-
(ref VBuffer<T> src, ref VBuffer<T> dst) =>
510+
(in VBuffer<T> src, ref VBuffer<T> dst) =>
511511
{
512512
Contracts.Assert(src.Length == Utils.Size(map));
513513

@@ -533,7 +533,7 @@ public static IDataView AddFoldIndex(IHostEnvironment env, IDataView input, int
533533
naIndices.Add(j);
534534
}
535535
mapper =
536-
(ref VBuffer<T> src, ref VBuffer<T> dst) =>
536+
(in VBuffer<T> src, ref VBuffer<T> dst) =>
537537
{
538538
Contracts.Assert(src.Length == Utils.Size(map));
539539
var values = dst.Values;
@@ -622,7 +622,7 @@ public static void ReconcileKeyValues(IHostEnvironment env, IDataView[] views, s
622622
{
623623
var keyMapperCur = keyValueMappers[i];
624624
ValueMapper<uint, uint> mapper =
625-
(ref uint src, ref uint dst) =>
625+
(in uint src, ref uint dst) =>
626626
{
627627
if (src == 0 || src > keyMapperCur.Length)
628628
dst = 0;
@@ -653,7 +653,7 @@ public static void ReconcileKeyValuesWithNoNames(IHostEnvironment env, IDataView
653653
if (!views[i].Schema.TryGetColumnIndex(columnName, out var index))
654654
throw env.Except($"Data view {i} doesn't contain a column '{columnName}'");
655655
ValueMapper<uint, uint> mapper =
656-
(ref uint src, ref uint dst) =>
656+
(in uint src, ref uint dst) =>
657657
{
658658
if (src > keyCount)
659659
dst = 0;
@@ -689,7 +689,7 @@ public static void ReconcileVectorKeyValues(IHostEnvironment env, IDataView[] vi
689689
{
690690
var keyMapperCur = keyValueMappers[i];
691691
ValueMapper<VBuffer<uint>, VBuffer<uint>> mapper =
692-
(ref VBuffer<uint> src, ref VBuffer<uint> dst) =>
692+
(in VBuffer<uint> src, ref VBuffer<uint> dst) =>
693693
{
694694
var values = dst.Values;
695695
if (Utils.Size(values) < src.Count)
@@ -984,7 +984,7 @@ private static IDataView AddVarLengthColumn<TSrc>(IHostEnvironment env, IDataVie
984984
{
985985
return LambdaColumnMapper.Create(env, "ChangeToVarLength", idv, variableSizeVectorColumnName,
986986
variableSizeVectorColumnName + "_VarLength", typeSrc, new VectorType(typeSrc.ItemType.AsPrimitive),
987-
(ref VBuffer<TSrc> src, ref VBuffer<TSrc> dst) => src.CopyTo(ref dst));
987+
(in VBuffer<TSrc> src, ref VBuffer<TSrc> dst) => src.CopyTo(ref dst));
988988
}
989989

990990
private static List<string> GetMetricNames(IChannel ch, Schema schema, IRow row, Func<int, bool> ignoreCol,

src/Microsoft.ML.Data/Evaluators/MulticlassClassifierEvaluator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ protected override IDataView GetPerInstanceMetricsCore(IDataView perInst, RoleMa
11051105
{
11061106
perInst = LambdaColumnMapper.Create(Host, "ConvertToDouble", perInst, schema.Label.Name,
11071107
schema.Label.Name, perInst.Schema.GetColumnType(labelCol), NumberType.R8,
1108-
(ref uint src, ref double dst) => dst = src == 0 ? double.NaN : src - 1 + (double)labelType.AsKey.Min);
1108+
(in uint src, ref double dst) => dst = src == 0 ? double.NaN : src - 1 + (double)labelType.AsKey.Min);
11091109
}
11101110

11111111
var perInstSchema = perInst.Schema;

src/Microsoft.ML.Data/Evaluators/QuantileRegressionEvaluator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ private IDataView ExtractRelevantIndex(IDataView data)
509509
var name = data.Schema.GetColumnName(i);
510510
var index = _index ?? type.VectorSize / 2;
511511
output = LambdaColumnMapper.Create(Host, "Quantile Regression", output, name, name, type, NumberType.R8,
512-
(ref VBuffer<Double> src, ref Double dst) => dst = src.GetItemOrDefault(index));
512+
(in VBuffer<Double> src, ref Double dst) => dst = src.GetItemOrDefault(index));
513513
output = new ChooseColumnsByIndexTransform(Host,
514514
new ChooseColumnsByIndexTransform.Arguments() { Drop = true, Index = new[] { i } }, output);
515515
}

src/Microsoft.ML.Data/Prediction/Calibrator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ public ValueMapper<TIn, TOut, TDist> GetMapper<TIn, TOut, TDist>()
250250
Host.Check(typeof(TDist) == typeof(Float));
251251
var map = GetMapper<TIn, Float>();
252252
ValueMapper<TIn, Float, Float> del =
253-
(ref TIn src, ref Float score, ref Float prob) =>
253+
(in TIn src, ref Float score, ref Float prob) =>
254254
{
255-
map(ref src, ref score);
255+
map(in src, ref score);
256256
prob = Calibrator.PredictProbability(score);
257257
};
258258
return (ValueMapper<TIn, TOut, TDist>)(Delegate)del;

src/Microsoft.ML.Data/Scorers/SchemaBindablePredictorWrapper.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private ValueGetter<TDst> GetValueGetter<TSrc, TDst>(IRow input, int colSrc)
153153
(ref TDst dst) =>
154154
{
155155
featureGetter(ref features);
156-
map(ref features, ref dst);
156+
map(in features, ref dst);
157157
};
158158
}
159159

@@ -546,7 +546,7 @@ private static void EnsureCachedResultValueMapper(ValueMapper<VBuffer<Float>, Fl
546546
if (featureGetter != null)
547547
featureGetter(ref features);
548548

549-
mapper(ref features, ref score, ref prob);
549+
mapper(in features, ref score, ref prob);
550550
cachedPosition = input.Position;
551551
}
552552
}
@@ -667,7 +667,7 @@ protected override Delegate GetPredictionGetter(IRow input, int colSrc)
667667
{
668668
featureGetter(ref features);
669669
Contracts.Check(features.Length == featureCount || featureCount == 0);
670-
map(ref features, ref value);
670+
map(in features, ref value);
671671
};
672672
return del;
673673
}

0 commit comments

Comments
 (0)