Skip to content

Remove exception due to supportsSparse:true in ReadFromTextLoader #2579

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 1 commit into from
Feb 19, 2019

Conversation

stephentoub
Copy link
Member

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.

Related to dotnet/machinelearning-samples#257
Related to #2576

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.
@codecov
Copy link

codecov bot commented Feb 15, 2019

Codecov Report

Merging #2579 into master will increase coverage by <.01%.
The diff coverage is 70%.

@@            Coverage Diff             @@
##           master    #2579      +/-   ##
==========================================
+ Coverage    71.5%   71.51%   +<.01%     
==========================================
  Files         801      801              
  Lines      142023   142024       +1     
  Branches    16147    16148       +1     
==========================================
+ Hits       101557   101563       +6     
+ Misses      35998    35993       -5     
  Partials     4468     4468
Flag Coverage Δ
#Debug 71.51% <70%> (ø) ⬆️
#production 67.79% <70%> (ø) ⬆️
#test 85.57% <ø> (ø) ⬆️

TryParseSigned(I4.MaxValue, in span, out long? res);
if (res.HasValue)
{
value = (I4)res.GetValueOrDefault();
Copy link
Member

@codemzs codemzs Feb 18, 2019

Choose a reason for hiding this comment

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

GetValueOrDefault [](start = 32, length = 17)

Why not just GetValue() since you have already checked it has value? Alternatively we can remove this if condition and do the below:

value = (I4)res.GetValueOrDefault();
return res.HasValue;

Copy link
Member Author

@stephentoub stephentoub Feb 19, 2019

Choose a reason for hiding this comment

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

Why not just GetValue() since you have already checked it has value?

Exactly because we already checked it has a value. GetValueOrDefault() just does return value; whereas .Value does something like if (!_hasValue) throw new Exception(); return _value;.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation. My understanding of these routines was the exact opposite of what they really do. Looks good!

Copy link
Contributor

@TomFinley TomFinley Feb 19, 2019

Choose a reason for hiding this comment

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

Hmmm. We may have some code to rewrite a number of places... opened #2612 to track this.

Copy link
Member

@codemzs codemzs left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Contributor

@TomFinley TomFinley left a comment

Choose a reason for hiding this comment

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

Thank you @stephentoub .

@TomFinley TomFinley merged commit d1a63ba into dotnet:master Feb 19, 2019
@stephentoub stephentoub deleted the removeparseexception branch February 19, 2019 14:04
@@ -1585,6 +1585,17 @@ public void Convert(in TX span, ref I4 value)
{
value = ParseI4(in span);
}
public bool TryConvert(in TX span, ref I4 value)
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the existing TryParse method?

/// <summary>
/// This produces zero for empty. It returns false if the text is not parsable or overflows.
/// On failure, it sets dst to the defualt value.
/// </summary>
public bool TryParse(in TX src, out I4 dst)
{
dst = default;
TryParseSigned(I4.MaxValue, in src, out long? res);
if (res == null)
{
dst = default;
return false;
}
Contracts.Assert(res.HasValue);
Contracts.Check((I4)res == res, "Overflow or underflow occured while converting value in text to int.");
dst = (I4)res;
return true;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I was trying to remain consistent with the existing code, which assigned a value, then passed it by ref, and then checked whether it remained what it was assigned. In practice it wouldn't matter because it would end up with the default value in either case, but it would incur additional writes in the failure case, and I didn't know whether it would matter. When I don't know a code base particularly well and what ramifications my changes might have, I try to maintain the existing code as much as possible.

Copy link
Member

Choose a reason for hiding this comment

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

I guess my concern is the consistency in the internal Conversion class.

  1. We now have TryParse and TryConvert. Which one should new callers choose?
  2. We have TryConvert for Int32, but no other types?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. Whichever signature they need. If they want an out, they choose TryParse, if they want ref, they choose TryConvert.
  2. Yes. You'd prefer we add a bunch of dead code?

If you feel strongly about this and want to change the call site to use TryParse, go for it. As I noted, my goal was to muck minimally with the call site logic.

eerhardt added a commit to eerhardt/machinelearning that referenced this pull request Feb 19, 2019
We don't need a TryConvert method, we already have a TryParse, which can be used instead.
eerhardt added a commit that referenced this pull request Feb 20, 2019
We don't need a TryConvert method, we already have a TryParse, which can be used instead.
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants