-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Mark VBuffer readonly and convert usages to use 'in' instead of 'ref` #1454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
60ecb06
3e62190
3ca8282
ae8f11e
7a096cd
c1f73d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -91,7 +91,7 @@ internal static IEnumerable<T> DenseValues<T>(T[] values, int[] indices, int len | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
public static bool HasNaNs(ref VBuffer<Single> buffer) | ||||||||||||
public static bool HasNaNs(in VBuffer<Single> buffer) | ||||||||||||
{ | ||||||||||||
for (int i = 0; i < buffer.Count; i++) | ||||||||||||
{ | ||||||||||||
|
@@ -101,7 +101,7 @@ public static bool HasNaNs(ref VBuffer<Single> buffer) | |||||||||||
return false; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public static bool HasNaNs(ref VBuffer<Double> buffer) | ||||||||||||
public static bool HasNaNs(in VBuffer<Double> buffer) | ||||||||||||
{ | ||||||||||||
for (int i = 0; i < buffer.Count; i++) | ||||||||||||
{ | ||||||||||||
|
@@ -111,7 +111,7 @@ public static bool HasNaNs(ref VBuffer<Double> buffer) | |||||||||||
return false; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public static bool HasNonFinite(ref VBuffer<Single> buffer) | ||||||||||||
public static bool HasNonFinite(in VBuffer<Single> buffer) | ||||||||||||
{ | ||||||||||||
for (int i = 0; i < buffer.Count; i++) | ||||||||||||
{ | ||||||||||||
|
@@ -121,7 +121,7 @@ public static bool HasNonFinite(ref VBuffer<Single> buffer) | |||||||||||
return false; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public static bool HasNonFinite(ref VBuffer<Double> buffer) | ||||||||||||
public static bool HasNonFinite(in VBuffer<Double> buffer) | ||||||||||||
{ | ||||||||||||
for (int i = 0; i < buffer.Count; i++) | ||||||||||||
{ | ||||||||||||
|
@@ -147,7 +147,7 @@ public static VBuffer<T> CreateDense<T>(int length) | |||||||||||
/// Applies <paramref name="visitor"/> to every explicitly defined element of the vector, | ||||||||||||
/// in order of index. | ||||||||||||
/// </summary> | ||||||||||||
public static void ForEachDefined<T>(ref VBuffer<T> a, Action<int, T> visitor) | ||||||||||||
public static void ForEachDefined<T>(in VBuffer<T> a, Action<int, T> visitor) | ||||||||||||
{ | ||||||||||||
Contracts.CheckValue(visitor, nameof(visitor)); | ||||||||||||
|
||||||||||||
|
@@ -175,7 +175,7 @@ public static void ForEachDefined<T>(ref VBuffer<T> a, Action<int, T> visitor) | |||||||||||
/// <param name="b">The second vector</param> | ||||||||||||
/// <param name="visitor">Delegate to apply to each pair of non-zero values. | ||||||||||||
/// This is passed the index, and two values</param> | ||||||||||||
public static void ForEachBothDefined<T>(ref VBuffer<T> a, ref VBuffer<T> b, Action<int, T, T> visitor) | ||||||||||||
public static void ForEachBothDefined<T>(in VBuffer<T> a, in VBuffer<T> b, Action<int, T, T> visitor) | ||||||||||||
{ | ||||||||||||
Contracts.Check(a.Length == b.Length, "Vectors must have the same dimensionality."); | ||||||||||||
Contracts.CheckValue(visitor, nameof(visitor)); | ||||||||||||
|
@@ -220,7 +220,7 @@ public static void ForEachBothDefined<T>(ref VBuffer<T> a, ref VBuffer<T> b, Act | |||||||||||
/// <param name="a">a vector</param> | ||||||||||||
/// <param name="b">another vector</param> | ||||||||||||
/// <param name="visitor">Function to apply to each pair of non-zero values - passed the index, and two values</param> | ||||||||||||
public static void ForEachEitherDefined<T>(ref VBuffer<T> a, ref VBuffer<T> b, Action<int, T, T> visitor) | ||||||||||||
public static void ForEachEitherDefined<T>(in VBuffer<T> a, in VBuffer<T> b, Action<int, T, T> visitor) | ||||||||||||
{ | ||||||||||||
Contracts.Check(a.Length == b.Length, "Vectors must have the same dimensionality."); | ||||||||||||
Contracts.CheckValue(visitor, nameof(visitor)); | ||||||||||||
|
@@ -492,7 +492,7 @@ public static void DensifyFirst<T>(ref VBuffer<T> dst, int denseCount) | |||||||||||
/// Creates a maybe sparse copy of a VBuffer. | ||||||||||||
/// Whether the created copy is sparse or not is determined by the proportion of non-default entries compared to the sparsity parameter. | ||||||||||||
/// </summary> | ||||||||||||
public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> dst, InPredicate<T> isDefaultPredicate, float sparsityThreshold = SparsityThreshold) | ||||||||||||
public static void CreateMaybeSparseCopy<T>(in VBuffer<T> src, ref VBuffer<T> dst, InPredicate<T> isDefaultPredicate, float sparsityThreshold = SparsityThreshold) | ||||||||||||
{ | ||||||||||||
Contracts.CheckParam(0 < sparsityThreshold && sparsityThreshold < 1, nameof(sparsityThreshold)); | ||||||||||||
if (!src.IsDense || src.Length < 20) | ||||||||||||
|
@@ -573,9 +573,9 @@ public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> d | |||||||||||
/// <param name="src">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, that could change</param> | ||||||||||||
/// <param name="manip">Function to apply to each pair of elements</param> | ||||||||||||
public static void ApplyWith<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip) | ||||||||||||
public static void ApplyWith<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip) | ||||||||||||
{ | ||||||||||||
ApplyWithCore(ref src, ref dst, manip, outer: false); | ||||||||||||
ApplyWithCore(in src, ref dst, manip, outer: false); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
|
@@ -589,12 +589,13 @@ public static void ApplyWith<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst | |||||||||||
/// there is any slot that is not explicitly represented in either vector. | ||||||||||||
/// </summary> | ||||||||||||
/// <param name="src">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, whose elements are read in most cases. But in some | ||||||||||||
/// cases <paramref name="dst"/> may be densified.</param> | ||||||||||||
/// <param name="res">Result vector</param> | ||||||||||||
/// <param name="manip">Function to apply to each pair of elements</param> | ||||||||||||
public static void ApplyWithCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip) | ||||||||||||
public static void ApplyWithCopy<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
/// Argument vector, whose elements are only read #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
{ | ||||||||||||
ApplyWithCoreCopy(ref src, ref dst, ref res, manip, outer: false); | ||||||||||||
ApplyWithCoreCopy(in src, ref dst, ref res, manip, outer: false); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
|
@@ -608,9 +609,9 @@ public static void ApplyWithCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer< | |||||||||||
/// <param name="src">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, that could change</param> | ||||||||||||
/// <param name="manip">Function to apply to each pair of elements</param> | ||||||||||||
public static void ApplyWithEitherDefined<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip) | ||||||||||||
public static void ApplyWithEitherDefined<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip) | ||||||||||||
{ | ||||||||||||
ApplyWithCore(ref src, ref dst, manip, outer: true); | ||||||||||||
ApplyWithCore(in src, ref dst, manip, outer: true); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
|
@@ -622,12 +623,13 @@ public static void ApplyWithEitherDefined<TSrc, TDst>(ref VBuffer<TSrc> src, ref | |||||||||||
/// there is any slot that is not explicitly represented in either vector. | ||||||||||||
/// </summary> | ||||||||||||
/// <param name="src">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, whose elements are only read</param> | ||||||||||||
/// <param name="dst">Argument vector, whose elements are read in most cases. But in some | ||||||||||||
/// cases <paramref name="dst"/> may be densified.</param> | ||||||||||||
/// <param name="res">Result vector</param> | ||||||||||||
/// <param name="manip">Function to apply to each pair of elements</param> | ||||||||||||
public static void ApplyWithEitherDefinedCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip) | ||||||||||||
public static void ApplyWithEitherDefinedCopy<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
/// Argument vector, whose elements are only read #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
{ | ||||||||||||
ApplyWithCoreCopy(ref src, ref dst, ref res, manip, outer: true); | ||||||||||||
ApplyWithCoreCopy(in src, ref dst, ref res, manip, outer: true); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// <summary> | ||||||||||||
|
@@ -636,7 +638,7 @@ public static void ApplyWithEitherDefinedCopy<TSrc, TDst>(ref VBuffer<TSrc> src, | |||||||||||
/// where necessary depending on whether this is an inner or outer join of the | ||||||||||||
/// indices of <paramref name="src"/> on <paramref name="dst"/>. | ||||||||||||
/// </summary> | ||||||||||||
private static void ApplyWithCore<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip, bool outer) | ||||||||||||
private static void ApplyWithCore<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, PairManipulator<TSrc, TDst> manip, bool outer) | ||||||||||||
{ | ||||||||||||
Contracts.Check(src.Length == dst.Length, "Vectors must have the same dimensionality."); | ||||||||||||
Contracts.CheckValue(manip, nameof(manip)); | ||||||||||||
|
@@ -773,7 +775,7 @@ private static void ApplyWithCore<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer | |||||||||||
// This is unnecessary -- falling through to the sparse code will | ||||||||||||
// actually handle this case just fine -- but it is more efficient. | ||||||||||||
Densify(ref dst); | ||||||||||||
ApplyWithCore(ref src, ref dst, manip, outer); | ||||||||||||
ApplyWithCore(in src, ref dst, manip, outer); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -908,7 +910,7 @@ private static void ApplyWithCore<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer | |||||||||||
/// where necessary depending on whether this is an inner or outer join of the | ||||||||||||
/// indices of <paramref name="src"/> on <paramref name="dst"/>. | ||||||||||||
/// </summary> | ||||||||||||
private static void ApplyWithCoreCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip, bool outer) | ||||||||||||
private static void ApplyWithCoreCopy<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, ref VBuffer<TDst> res, PairManipulatorCopy<TSrc, TDst> manip, bool outer) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can it be "in"? #ByDesign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This machinelearning/src/Microsoft.ML.Core/Utilities/VBufferUtils.cs Lines 1089 to 1093 in ef2bade
This method actually modifies In reply to: 229498975 [](ancestors = 229498975) |
||||||||||||
{ | ||||||||||||
Contracts.Check(src.Length == dst.Length, "Vectors must have the same dimensionality."); | ||||||||||||
Contracts.CheckValue(manip, nameof(manip)); | ||||||||||||
|
@@ -1090,7 +1092,7 @@ private static void ApplyWithCoreCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBu | |||||||||||
// This is unnecessary -- falling through to the sparse code will | ||||||||||||
// actually handle this case just fine -- but it is more efficient. | ||||||||||||
Densify(ref dst); | ||||||||||||
ApplyWithCoreCopy(ref src, ref dst, ref res, manip, outer); | ||||||||||||
ApplyWithCoreCopy(in src, ref dst, ref res, manip, outer); | ||||||||||||
} | ||||||||||||
else | ||||||||||||
{ | ||||||||||||
|
@@ -1152,7 +1154,7 @@ private static void ApplyWithCoreCopy<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBu | |||||||||||
/// </summary> | ||||||||||||
/// <seealso cref="ApplyWith{TSrc,TDst}"/> | ||||||||||||
/// <seealso cref="ApplyWithEitherDefined{TSrc,TDst}"/> | ||||||||||||
public static void ApplyIntoEitherDefined<TSrc, TDst>(ref VBuffer<TSrc> src, ref VBuffer<TDst> dst, Func<int, TSrc, TDst> func) | ||||||||||||
public static void ApplyIntoEitherDefined<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuffer<TDst> dst, Func<int, TSrc, TDst> func) | ||||||||||||
{ | ||||||||||||
Contracts.CheckValue(func, nameof(func)); | ||||||||||||
|
||||||||||||
|
@@ -1189,7 +1191,7 @@ public static void ApplyIntoEitherDefined<TSrc, TDst>(ref VBuffer<TSrc> src, ref | |||||||||||
/// necessarily be dense. Otherwise, if both are sparse, the output will be sparse iff | ||||||||||||
/// there is any slot that is not explicitly represented in either vector. | ||||||||||||
/// </summary> | ||||||||||||
public static void ApplyInto<TSrc1, TSrc2, TDst>(ref VBuffer<TSrc1> a, ref VBuffer<TSrc2> b, ref VBuffer<TDst> dst, Func<int, TSrc1, TSrc2, TDst> func) | ||||||||||||
public static void ApplyInto<TSrc1, TSrc2, TDst>(in VBuffer<TSrc1> a, in VBuffer<TSrc2> b, ref VBuffer<TDst> dst, Func<int, TSrc1, TSrc2, TDst> func) | ||||||||||||
{ | ||||||||||||
Contracts.Check(a.Length == b.Length, "Vectors must have the same dimensionality."); | ||||||||||||
Contracts.CheckValue(func, nameof(func)); | ||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: there was only 1 usage of this method, and it is just a wrapper over
src.CopyTo(ref dst)
, so I thought it was best to drop it in favor of only having a single way to copy from one buffer to another.Let me know if this was a bad idea. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also used once in internal repos, so I think this is a good change.
In reply to: 229444488 [](ancestors = 229444488)