Skip to content
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

chore: Added suppression attributes to CA1822 #693

Merged
merged 3 commits into from
Oct 20, 2022

Conversation

nikcio
Copy link
Contributor

@nikcio nikcio commented Oct 18, 2022

Following #690 I've now added suppression attributes to the methods we decided not to make static.

Adds to #663

I also had another look at This issue and found that the reason this caused problems is because I forgot to change the this. to BasicModelBE. in the Score method. But I don't know if this is moving too far away from the original code?

The change would be:

public override sealed float Score(BasicStats stats, float tfn)
{
    double F = stats.TotalTermFreq + 1 + tfn;
    // approximation only holds true when F << N, so we use N += F
    double N = F + stats.NumberOfDocuments;
    return (float)(-SimilarityBase.Log2((N - 1) * Math.E) + BasicModelBE.F(N + F - 1, N + F - tfn - 2) - BasicModelBE.F(F, F - tfn));
}
/// <summary>
/// The <em>f</em> helper function defined for <em>B<sub>E</sub></em>. </summary>
private static double F(double n, double m) // LUCENENET: CA1822: Mark members as static
{
    return (m + 0.5) * SimilarityBase.Log2(n / m) + (n - m) * SimilarityBase.Log2(n);
}

@NightOwl888
Copy link
Contributor

NightOwl888 commented Oct 18, 2022

I also had another look at This issue and found that the reason this caused problems is because I forgot to change the this. to BasicModelBE. in the Score method. But I don't know if this is moving too far away from the original code?

The change would be:

public override sealed float Score(BasicStats stats, float tfn)
{
    double F = stats.TotalTermFreq + 1 + tfn;
    // approximation only holds true when F << N, so we use N += F
    double N = F + stats.NumberOfDocuments;
    return (float)(-SimilarityBase.Log2((N - 1) * Math.E) + BasicModelBE.F(N + F - 1, N + F - tfn - 2) - BasicModelBE.F(F, F - tfn));
}
/// <summary>
/// The <em>f</em> helper function defined for <em>B<sub>E</sub></em>. </summary>
private static double F(double n, double m) // LUCENENET: CA1822: Mark members as static
{
    return (m + 0.5) * SimilarityBase.Log2(n / m) + (n - m) * SimilarityBase.Log2(n);
}
 
  /** The <em>f</em> helper function defined for <em>B<sub>E</sub></em>. */
  private final double f(double n, double m) {
    return (m + 0.5) * log2(n / m) + (n - m) * log2(n);
  }

In the original code the function f was lowercase. I first thought that forced them to use uppercase F as a varible name. But, judging by the documentation and comments, the capital F variable name seems to be a math thing, and therefore intentional.

Let's revert F(double n, double m) to an instance method and add the SuppressMessage attribute to the F(double n, double m) method.

We should also add the [MethodImpl(MethodImplOptions.AggressiveInlining)] attribute to the F(double n, double m) method to prompt the compiler to inline this math in the Score function, which will give a similar benefit to making the method static.

I have also opened #694 which is related, but should be done in a separate PR since it goes beyond this one class.

Copy link
Contributor

@NightOwl888 NightOwl888 left a comment

Choose a reason for hiding this comment

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

@nikcio - Forgot to go through the review process when I left my last comment.

@nikcio
Copy link
Contributor Author

nikcio commented Oct 20, 2022

The PR has now been updated after the comment above

@NightOwl888 NightOwl888 merged commit 740144c into apache:master Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants