Skip to content

Follow up from Extract IDataView feedback #2296

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

Merged
merged 2 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Optimizing volatile reads with Interlocked.CompareExchange to only ne…
…ed one read instead of two.
  • Loading branch information
eerhardt committed Jan 29, 2019
commit cdb42195af6224361750cb19aaa345045139f66f
96 changes: 48 additions & 48 deletions src/Microsoft.Data.DataView/ColumnType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static TextType Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new TextType(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new TextType(), null) ??
_instance;
}
}

Expand Down Expand Up @@ -116,9 +116,9 @@ public static NumberType I1
{
get
{
if (_instI1 == null)
Interlocked.CompareExchange(ref _instI1, new NumberType(typeof(sbyte), "I1"), null);
return _instI1;
return _instI1 ??
Interlocked.CompareExchange(ref _instI1, new NumberType(typeof(sbyte), "I1"), null) ??
_instI1;
}
}

Expand All @@ -127,9 +127,9 @@ public static NumberType U1
{
get
{
if (_instU1 == null)
Interlocked.CompareExchange(ref _instU1, new NumberType(typeof(byte), "U1"), null);
return _instU1;
return _instU1 ??
Interlocked.CompareExchange(ref _instU1, new NumberType(typeof(byte), "U1"), null) ??
_instU1;
}
}

Expand All @@ -138,9 +138,9 @@ public static NumberType I2
{
get
{
if (_instI2 == null)
Interlocked.CompareExchange(ref _instI2, new NumberType(typeof(short), "I2"), null);
return _instI2;
return _instI2 ??
Interlocked.CompareExchange(ref _instI2, new NumberType(typeof(short), "I2"), null) ??
_instI2;
}
}

Expand All @@ -149,9 +149,9 @@ public static NumberType U2
{
get
{
if (_instU2 == null)
Interlocked.CompareExchange(ref _instU2, new NumberType(typeof(ushort), "U2"), null);
return _instU2;
return _instU2 ??
Interlocked.CompareExchange(ref _instU2, new NumberType(typeof(ushort), "U2"), null) ??
_instU2;
}
}

Expand All @@ -160,9 +160,9 @@ public static NumberType I4
{
get
{
if (_instI4 == null)
Interlocked.CompareExchange(ref _instI4, new NumberType(typeof(int), "I4"), null);
return _instI4;
return _instI4 ??
Interlocked.CompareExchange(ref _instI4, new NumberType(typeof(int), "I4"), null) ??
_instI4;
}
}

Expand All @@ -171,9 +171,9 @@ public static NumberType U4
{
get
{
if (_instU4 == null)
Interlocked.CompareExchange(ref _instU4, new NumberType(typeof(uint), "U4"), null);
return _instU4;
return _instU4 ??
Interlocked.CompareExchange(ref _instU4, new NumberType(typeof(uint), "U4"), null) ??
_instU4;
}
}

Expand All @@ -182,9 +182,9 @@ public static NumberType I8
{
get
{
if (_instI8 == null)
Interlocked.CompareExchange(ref _instI8, new NumberType(typeof(long), "I8"), null);
return _instI8;
return _instI8 ??
Interlocked.CompareExchange(ref _instI8, new NumberType(typeof(long), "I8"), null) ??
_instI8;
}
}

Expand All @@ -193,9 +193,9 @@ public static NumberType U8
{
get
{
if (_instU8 == null)
Interlocked.CompareExchange(ref _instU8, new NumberType(typeof(ulong), "U8"), null);
return _instU8;
return _instU8 ??
Interlocked.CompareExchange(ref _instU8, new NumberType(typeof(ulong), "U8"), null) ??
_instU8;
}
}

Expand All @@ -204,9 +204,9 @@ public static NumberType UG
{
get
{
if (_instUG == null)
Interlocked.CompareExchange(ref _instUG, new NumberType(typeof(RowId), "UG"), null);
return _instUG;
return _instUG ??
Interlocked.CompareExchange(ref _instUG, new NumberType(typeof(RowId), "UG"), null) ??
_instUG;
}
}

Expand All @@ -215,9 +215,9 @@ public static NumberType R4
{
get
{
if (_instR4 == null)
Interlocked.CompareExchange(ref _instR4, new NumberType(typeof(float), "R4"), null);
return _instR4;
return _instR4 ??
Interlocked.CompareExchange(ref _instR4, new NumberType(typeof(float), "R4"), null) ??
_instR4;
}
}

Expand All @@ -226,9 +226,9 @@ public static NumberType R8
{
get
{
if (_instR8 == null)
Interlocked.CompareExchange(ref _instR8, new NumberType(typeof(double), "R8"), null);
return _instR8;
return _instR8 ??
Interlocked.CompareExchange(ref _instR8, new NumberType(typeof(double), "R8"), null) ??
_instR8;
}
}

Expand All @@ -255,9 +255,9 @@ public static BoolType Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new BoolType(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new BoolType(), null) ??
_instance;
}
}

Expand Down Expand Up @@ -287,9 +287,9 @@ public static DateTimeType Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new DateTimeType(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new DateTimeType(), null) ??
_instance;
}
}

Expand All @@ -316,9 +316,9 @@ public static DateTimeOffsetType Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new DateTimeOffsetType(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new DateTimeOffsetType(), null) ??
_instance;
}
}

Expand Down Expand Up @@ -348,9 +348,9 @@ public static TimeSpanType Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new TimeSpanType(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new TimeSpanType(), null) ??
_instance;
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/Microsoft.ML.Core/Data/MetadataUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,9 @@ internal static KeyType ScoreColumnSetIdType
{
get
{
if (_scoreColumnSetIdType == null)
{
var type = new KeyType(typeof(uint), int.MaxValue);
Interlocked.CompareExchange(ref _scoreColumnSetIdType, type, null);
}
return _scoreColumnSetIdType;
return _scoreColumnSetIdType ??
Interlocked.CompareExchange(ref _scoreColumnSetIdType, new KeyType(typeof(uint), int.MaxValue), null) ??
_scoreColumnSetIdType;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.ML.Core/Utilities/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ private static string DllDir
{
get
{
if (_dllDir == null)
string result = _dllDir;
if (result == null)
{
string path = typeof(Utils).Assembly.Location;
string directory = Path.GetDirectoryName(path);
Interlocked.CompareExchange(ref _dllDir, directory, null);
result = _dllDir;
}

return _dllDir;
return result;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static ResourceManagerUtils Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new ResourceManagerUtils(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new ResourceManagerUtils(), null) ??
_instance;
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/Microsoft.ML.Data/Data/Combiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static TextCombiner Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new TextCombiner(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new TextCombiner(), null) ??
_instance;
}
}

Expand All @@ -48,9 +48,9 @@ public static FloatAdder Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new FloatAdder(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new FloatAdder(), null) ??
_instance;
}
}

Expand Down Expand Up @@ -90,9 +90,9 @@ public static R8Adder Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new R8Adder(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new R8Adder(), null) ??
_instance;
}
}

Expand All @@ -112,9 +112,9 @@ public static U4Adder Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new U4Adder(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new U4Adder(), null) ??
_instance;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Data/Data/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public static Conversions Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new Conversions(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new Conversions(), null) ??
_instance;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public static ValueCreatorCache Instance
{
get
{
if (_instance == null)
Interlocked.CompareExchange(ref _instance, new ValueCreatorCache(), null);
return _instance;
return _instance ??
Interlocked.CompareExchange(ref _instance, new ValueCreatorCache(), null) ??
_instance;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.ML.Data/Evaluators/EvaluatorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static Dictionary<string, Func<IHostEnvironment, IMamlEvaluator>> Instanc
{
get
{
if (_knownEvaluatorFactories == null)
Dictionary<string, Func<IHostEnvironment, IMamlEvaluator>> result = _knownEvaluatorFactories;
if (result == null)
{
var tmp = new Dictionary<string, Func<IHostEnvironment, IMamlEvaluator>>
{
Expand All @@ -48,8 +49,9 @@ public static Dictionary<string, Func<IHostEnvironment, IMamlEvaluator>> Instanc
};
//tmp.Add(MetadataUtils.Const.ScoreColumnKind.SequenceClassification, "SequenceClassifierEvaluator");
Interlocked.CompareExchange(ref _knownEvaluatorFactories, tmp, null);
result = _knownEvaluatorFactories;
}
return _knownEvaluatorFactories;
return result;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.ML.Data/Evaluators/RankerEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -949,16 +949,18 @@ public static Double[] DiscountMap
{
get
{
if (_discountMap == null)
double[] result = _discountMap;
if (result == null)
{
var discountMap = new Double[100]; //Hard to believe anyone would set truncation Level higher than 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to believe anyone would set truncation Level higher than 100 [](start = 57, length = 65)

Comments like this raise my confidence in code a lot.

for (int i = 0; i < discountMap.Length; i++)
{
discountMap[i] = 1 / Math.Log(2 + i);
}
Interlocked.CompareExchange(ref _discountMap, discountMap, null);
result = _discountMap;
}
return _discountMap;
return result;
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/Microsoft.ML.Data/Scorers/MultiClassClassifierScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,10 @@ public ISchemaBindableMapper Bindable
{
get
{
if (_bindable == null)
{
return _bindable ??
Interlocked.CompareExchange(ref _bindable,
new LabelNameBindableMapper(_host, _mapper, _labelNameType, _labelNameGetter, _metadataKind, _canWrap), null);
}
Contracts.AssertValue(_bindable);
return _bindable;
new LabelNameBindableMapper(_host, _mapper, _labelNameType, _labelNameGetter, _metadataKind, _canWrap), null) ??
_bindable;
}
}

Expand Down
Loading