Skip to content

Commit d1a63ba

Browse files
stephentoubTomFinley
authored andcommitted
Remove exception due to supportsSparse:true in ReadFromTextLoader (#2579)
In the GitHubLabeler sample, for example, there are ~450 InvalidOperationExceptions getting thrown and caught internally, as the implementation tries to parse some text as an int and fails. There's no need for an exception here; the implementation is already using a TryParse method, which it then turns into an exception and then immediately catches and ignores... we can just cut out the middle man and not throw.
1 parent ba75a47 commit d1a63ba

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,17 @@ public void Convert(in TX span, ref I4 value)
15851585
{
15861586
value = ParseI4(in span);
15871587
}
1588+
public bool TryConvert(in TX span, ref I4 value)
1589+
{
1590+
TryParseSigned(I4.MaxValue, in span, out long? res);
1591+
if (res.HasValue)
1592+
{
1593+
value = (I4)res.GetValueOrDefault();
1594+
return true;
1595+
}
1596+
1597+
return false;
1598+
}
15881599
public void Convert(in TX span, ref U4 value)
15891600
{
15901601
value = ParseU4(in span);

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,18 +1016,8 @@ public int GatherFields(ReadOnlyMemory<char> lineSpan, ReadOnlySpan<char> span,
10161016
}
10171017
var spanT = Fields.Spans[Fields.Count - 1];
10181018

1019-
// Note that Convert throws exception the text is unparsable.
1020-
int csrc = default;
1021-
try
1022-
{
1023-
Conversions.Instance.Convert(in spanT, ref csrc);
1024-
}
1025-
catch
1026-
{
1027-
Contracts.Assert(csrc == default);
1028-
}
1029-
1030-
if (csrc <= 0)
1019+
int csrc = 0;
1020+
if (!Conversions.Instance.TryConvert(in spanT, ref csrc) || csrc <= 0)
10311021
{
10321022
_stats.LogBadFmt(ref scan, "Bad dimensionality or ambiguous sparse item. Use sparse=- for non-sparse file, and/or quote the value.");
10331023
break;

0 commit comments

Comments
 (0)