Skip to content

Move sloppySin into SloppyMath from GeoUtils #14516

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

jainankitk
Copy link
Contributor

Description

Wondering if we really need sloppySin anymore. For modern hardware with JDK 17 on recent processors:

  • The performance difference should be negligible
  • Modern JVMs heavily optimize Math.sin calls
  • The approximation error from sloppySin (~0.1%) is not worth probably insignificant performance benefit!

Copy link
Member

@rmuir rmuir left a comment

Choose a reason for hiding this comment

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

I don't agree with the such changes without supporting benchmarks, sorry. Especially it is bad news to just replace 3 functions (sin,cos,asin) all at once in YOLO fashion.

@jainankitk
Copy link
Contributor Author

I don't agree with the such changes without supporting benchmarks, sorry. Especially it is bad news to just replace 3 functions (sin,cos,asin) all at once in YOLO fashion.

Thanks for raising these concerns. The intention was to just remove custom sloppySin. Removing SloppyMath.cos, SloppyMath.asin was by mistake. Let me work on providing supporting benchmark. I am wondering if there is something in LuceneUtil benchmark that can be used for this purpose?

@rmuir
Copy link
Member

rmuir commented Apr 18, 2025

It will mostly impact geo sorting.

But replacing the fast SloppyMath calculation here with slower equivalents from OpenJDK won't buy you any improved error rate: precision is purposefully truncated to ensure stable sorts: https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java#L73-L74

@jainankitk

This comment was marked as outdated.

@jainankitk

This comment was marked as outdated.

@rmuir
Copy link
Member

rmuir commented Apr 19, 2025

But the benchmark is not useful, as it was written as a test. Tests don't run normally, in particular without java C2 compiler, so trying to make benchmark via test is not useful and biased in many ways.

If you want to make a microbenchmark, please use lucene/benchmark-jmh facility.

@rmuir
Copy link
Member

rmuir commented Apr 19, 2025

See guide here: https://github.com/apache/lucene/blob/main/help/jmh.txt

@jainankitk
Copy link
Contributor Author

JMH benchmark comparison for just the sloppySin / standardSin comparison:

JMH Benchmark Results Comparison
================================================================================
+---------------+----------------------+------------------------+---------+
|   Input Value | SloppySin (ops/ms)   | StandardSin (ops/ms)   | Ratio   |
+===============+======================+========================+=========+
|          0.1  | 155,079              | 77,701                 | 0.50x   |
+---------------+----------------------+------------------------+---------+
|          0.5  | 155,008              | 77,766                 | 0.50x   |
+---------------+----------------------+------------------------+---------+
|          1    | 155,577              | 77,495                 | 0.50x   |
+---------------+----------------------+------------------------+---------+
|          2    | 163,508              | 77,648                 | 0.47x   |
+---------------+----------------------+------------------------+---------+
|          3.14 | 162,770              | 78,211                 | 0.48x   |
+---------------+----------------------+------------------------+---------+

Average slowdown: 0.49x

@jainankitk
Copy link
Contributor Author

JMH benchmark comparison for just the FromPointDistanceSloppySin / FromPointDistanceStandardSin comparison:

JMH Benchmark Results Comparison
================================================================================
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (lat,lon,radinmeters)   | FromPointDistanceSloppySin (ops/ms)   | FromPointDistanceStandardSin (ops/ms)   | Ratio   |
+=========================+=======================================+=========================================+=========+
| (0.0,0.0,100)           | 22,913                                | 17,211                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,0.0,1000)          | 22,812                                | 17,191                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,0.0,10000)         | 22,942                                | 17,147                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,0.0,100000)        | 22,967                                | 17,225                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,0.0,1000000)       | 22,859                                | 17,192                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,179.0,100)         | 22,922                                | 17,196                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,179.0,1000)        | 22,812                                | 17,200                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,179.0,10000)       | 22,876                                | 17,193                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,179.0,100000)      | 22,957                                | 17,229                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,179.0,1000000)     | 22,926                                | 17,159                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-179.0,100)        | 22,868                                | 17,191                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-179.0,1000)       | 22,949                                | 17,197                   | (0.0,-179.0,1000)       | 22,949                                | 17,197                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-179.0,10000)      | 22,832                                | 17,186                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-179.0,100000)     | 22,829                                | 17,236                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-179.0,1000000)    | 22,568                                | 17,039                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,90.0,100)          | 22,800                                | 17,236                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,90.0,1000)         | 22,862                                | 17,251                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,90.0,10000)        | 22,805                                | 17,269                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,90.0,100000)       | 22,837                                | 17,123                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,90.0,1000000)      | 22,965                                | 17,240                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-90.0,100)         | 22,843                                | 17,247                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-90.0,1000)        | 22,933                                | 17,212                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-90.0,10000)       | 22,858                                | 17,134                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-90.0,100000)      | 22,866                                | 17,246                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (0.0,-90.0,1000000)     | 22,945                                | 17,226                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,0.0,100)          | 22,763                                | 17,134                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,0.0,1000)         | 22,763                                | 17,097                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,0.0,10000)        | 22,825                                | 17,109                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,0.0,100000)       | 22,810                                | 17,090                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,0.0,1000000)      | 22,796                                | 17,087                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,179.0,100)        | 22,772                                | 17,149                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,179.0,1000)       | 22,750                                | 17,110                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,179.0,10000)      | 22,857                                | 17,118                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,179.0,100000)     | 22,644                                | 17,105                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,179.0,1000000)    | 22,670                                | 16,918                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-179.0,100)       | 22,749                                | 17,138                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-179.0,1000)      | 22,740                                | 17,117                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-179.0,10000)     | 22,695                                | 17,113                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-179.0,100000)    | 22,513                                | 16,942                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-179.0,1000000)   | 22,421                                | 16,947                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,90.0,100)         | 22,714                                | 17,148                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,90.0,1000)        | 22,641                                | 17,120                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,90.0,10000)       | 22,766                                | 17,108                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,90.0,100000)      | 22,816                                | 17,105                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,90.0,1000000)     | 22,826                                | 17,128                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-90.0,100)        | 22,828                                | 17,115                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-90.0,1000)       | 22,778                                | 17,129                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-90.0,10000)      | 22,723                                | 17,102                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-90.0,100000)     | 22,776                                | 17,098                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (45.0,-90.0,1000000)    | 22,878                                | 17,149                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,0.0,100)          | 22,789                                | 17,130                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,0.0,1000)         | 22,748                                | 17,110                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,0.0,10000)        | 22,783                                | 17,119                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,0.0,100000)       | 22,856                                | 17,105                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,0.0,1000000)      | 91,486                                | 91,286                                  | 1.00x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,179.0,100)        | 22,729                                | 17,126                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,179.0,1000)       | 22,761                                | 17,149                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,179.0,10000)      | 22,805                                | 17,109                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,179.0,100000)     | 22,626                                | 17,114                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,179.0,1000000)    | 91,999                                | 91,454                                  | 0.99x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-179.0,100)       | 22,835                                | 17,155                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-179.0,1000)      | 22,838                                | 17,155                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-179.0,10000)     | 22,378                                | 16,943                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-179.0,100000)    | 22,455                                | 16,836                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-179.0,1000000)   | 90,194                                | 91,280                                  | 1.01x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,90.0,100)         | 22,683                                | 17,134                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,90.0,1000)        | 22,769                                | 17,139                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,90.0,10000)       | 22,714                                | 17,110                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,90.0,100000)      | 22,876                                | 17,139                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,90.0,1000000)     | 91,628                                | 91,051                                  | 0.99x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-90.0,100)        | 22,771                                | 17,140                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-90.0,1000)       | 22,764                                | 17,130                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-90.0,10000)      | 22,723                                | 17,098                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-90.0,100000)     | 22,745                                | 17,131                                  | 0.75x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (89.0,-90.0,1000000)    | 91,140                                | 86,797                                  | 0.95x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,0.0,100)         | 21,870                                | 16,791                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,0.0,1000)        | 21,891                                | 16,820                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,0.0,10000)       | 21,941                                | 16,806                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,0.0,100000)      | 21,867                                | 16,611                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,0.0,1000000)     | 21,947                                | 16,685                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,179.0,100)       | 21,931                                | 16,859                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,179.0,1000)      | 21,936                                | 16,793                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,179.0,10000)     | 21,850                                | 16,852                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,179.0,100000)    | 21,522                                | 16,827                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,179.0,1000000)   | 21,744                                | 16,829                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-179.0,100)      | 21,921                                | 16,799                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-179.0,1000)     | 21,573                                | 16,832                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-179.0,10000)    | 22,036                                | 16,832                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-179.0,100000)   | 21,475                                | 16,717                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-179.0,1000000)  | 21,621                                | 16,708                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,90.0,100)        | 21,959                                | 16,838                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,90.0,1000)       | 21,976                                | 16,868                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,90.0,10000)      | 21,754                                | 16,841                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,90.0,100000)     | 21,825                                | 16,834                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,90.0,1000000)    | 21,876                                | 16,822                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-90.0,100)       | 21,918                                | 16,736                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-90.0,1000)      | 21,912                                | 16,728                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-90.0,10000)     | 21,865                                | 16,764                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-90.0,100000)    | 21,926                                | 16,804                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-45.0,-90.0,1000000)   | 21,937                                | 16,845                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,0.0,100)         | 21,878                                | 16,866                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,0.0,1000)        | 21,973                                | 16,823                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,0.0,10000)       | 22,017                                | 16,871                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,0.0,100000)      | 21,952                                | 16,842                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,0.0,1000000)     | 96,439                                | 95,950                                  | 0.99x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,179.0,100)       | 21,903                                | 16,831                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,179.0,1000)      | 21,939                                | 16,833                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,179.0,10000)     | 21,649                                | 16,827                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,179.0,100000)    | 21,680                                | 16,714                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,179.0,1000000)   | 96,683                                | 96,579                                  | 1.00x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-179.0,100)      | 21,934                                | 16,839                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-179.0,1000)     | 21,930                                | 16,811                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-179.0,10000)    | 21,488                                | 16,679                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-179.0,100000)   | 21,479                                | 16,663                                  | 0.78x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-179.0,1000000)  | 95,235                                | 96,168                                  | 1.01x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,90.0,100)        | 21,954                                | 16,819                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,90.0,1000)       | 21,875                                | 16,863                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,90.0,10000)      | 21,874                                | 16,844                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,90.0,100000)     | 21,988                                | 16,829                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,90.0,1000000)    | 96,311                                | 95,527                                  | 0.99x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-90.0,100)       | 21,882                                | 16,785                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-90.0,1000)      | 21,980                                | 16,835                                  | 0.77x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-90.0,10000)     | 22,039                                | 16,669                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-90.0,100000)    | 22,011                                | 16,637                                  | 0.76x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+
| (-89.0,-90.0,1000000)   | 96,675                                | 94,549                                  | 0.98x   |
+-------------------------+---------------------------------------+-----------------------------------------+---------+

Average slowdown: 0.78x

Signed-off-by: Ankit Jain <[email protected]>
@jainankitk
Copy link
Contributor Author

But the benchmark is not useful, as it was written as a test. Tests don't run normally, in particular without java C2 compiler, so trying to make benchmark via test is not useful and biased in many ways.

If you want to make a microbenchmark, please use lucene/benchmark-jmh facility.

Thanks @rmuir for the suggestion. I have added couple of jmh benchmarks for this, and they clearly show, probably not the right change. While the slowdown for fromPointDistanceStandardSin / fromPointDistanceSloppySin is about 0.78x compared to .49x in sloppySin vs standardSin, and it is probable we don't even observe this micro-level slowdown in practical applications, there is no good reason for merging this change as is.

I have just moved sloppySin from GeoUtils to SloppyMath for keeping all the sloppy stuff in single place. Also added clarifying comment about possible regression pointing to the benchmarks. Please let me know if you have any concerns with that.

@jainankitk jainankitk changed the title Remove sloppySin calculations Move sloppySin into SloppyMath from GeoUtils Apr 21, 2025
* @return the sine of the argument.
* @see Math#sin(double)
*/
public static double sin(double a) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to promote this from an experimental API in GeoUtils to a non-experimental public API here?

If we really want to do this, let's keep in line with the existing methods in the file, and provide accuracy bounds and tests?

Personally I think an easier solution would be to "demote" it from GeoUtils to a package-private static method: seems like it is only used by Rectangle and it doesn't need to be public. This would be easier than "properly supporting it" IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally I think an easier solution would be to "demote" it from GeoUtils to a package-private static method: seems like it is only used by Rectangle and it doesn't need to be public. This would be easier than "properly supporting it" IMO.

Thanks @rmuir for the suggestions. While I agree it is easier this way, but IMO:

  • It is better to have related stuff in single place, even if we are using only once currently
  • The benchmarks will be unable to use if it is private, and I feel they do add some value, in case some else like myself wonders if we really need sloppySin!?

Do we really need to promote this from an experimental API in GeoUtils to a non-experimental public API here?
If we really want to do this, let's keep in line with the existing methods in the file, and provide accuracy bounds and tests?

Had not realized myself that this promotes to non-experimental API. I have been bit sloppy with this SloppyMath stuff! :( Thanks for pointing and patiently reviewing so far.

That being said, I don't see any real harm with sloppySin getting promoted to SloppyMath. I have added accuracy bounds and tests. Verified that the test ran without failure for 1000 iterations

> Task :lucene:core:test
:lucene:core:test (SUCCESS): 1000 test(s)

> Task :lucene:core:wipeTaskTemp
The slowest suites (exceeding 1s) during this run:
   9.46s TestSloppyMath (:lucene:core)

BUILD SUCCESSFUL in 25s
246 actionable tasks: 175 executed, 71 up-to-date

Copy link
Member

Choose a reason for hiding this comment

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

I agree it is better to have the benchmark. I'm not familiar with what the Rectangle code is doing here: I don't tend to think about sin() being associated with Rectanges...

But the background is that the cos() and asin() are needed for this use-case: https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/document/LatLonPointDistanceComparator.java#L37-L43

We try hard to avoid calling these functions at all, but in some cases such as the sorting, many calls are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with what the Rectangle code is doing here: I don't tend to think about sin() being associated with Rectanges...

sin() is used for calculating and adjusting the longitude range of a circle's bounding Rectangle based on the latitude

Signed-off-by: Ankit Jain <[email protected]>
Copy link
Member

@rmuir rmuir left a comment

Choose a reason for hiding this comment

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

thanks for adding tests and benchies

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