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

Replace cfor with fastFor #1077

Merged
merged 70 commits into from
Oct 19, 2021
Merged

Replace cfor with fastFor #1077

merged 70 commits into from
Oct 19, 2021

Conversation

armanbilge
Copy link
Member

Based against #1067. Opening temporarily for CI before these get merged there.

cquiroz and others added 30 commits September 12, 2021 16:43
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <carlos.m.quiroz@gmail.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
w
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
cquiroz and others added 19 commits September 24, 2021 15:57
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
…check test

Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
Signed-off-by: Carlos Quiroz <3615303+cquiroz@users.noreply.github.com>
@armanbilge armanbilge marked this pull request as ready for review October 17, 2021 01:32
Comment on lines 280 to 290
/**
* The `fastFor` macro will replace the `cfor` macro in Scala 3.
* Note that `fastFor` has simpler semantics than `cfor` and in general is _not_ equivalent
* to inlining a while-loop, particularly with respect to closures.
* This change is unlikely to affect typical use-cases, however.
*
* The implementation of `fastFor` provided for Scala 2 is _not_ a macro but is a "reference" implementation
* with semantics matching the Scala 3 macro.
* If you are on Scala 2 and concerned about performance you should continue using `cfor`.
*/
def fastFor[A](init: A)(test: A => Boolean, next: A => A)(body: A => Unit): Unit =
Copy link
Member Author

Choose a reason for hiding this comment

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

Thoughts?

@armanbilge
Copy link
Member Author

Oh, I just realized, I didn't think this through. I just replaced all internal uses of cfor with fastFor which means there will be a performance hit for Scala 2.

@armanbilge
Copy link
Member Author

I did a quick benchmark ...

[info] Benchmark                                  (size)  Mode  Cnt        Score         Error  Units
[info] CForBenchmarks.doCForGcd                  1000000  avgt    3   893577.916 ± 1440594.362  us/op
[info] CForBenchmarks.doCForIntArrayMultiply     1000000  avgt    3     5372.725 ±   17842.561  us/op
[info] CForBenchmarks.doCForMin                  1000000  avgt    3     4580.879 ±   33360.156  us/op
[info] CForBenchmarks.doCForOr                   1000000  avgt    3     5618.975 ±   47778.820  us/op
[info] CForBenchmarks.doFastForGcd               1000000  avgt    3  3551141.142 ± 9913377.889  us/op
[info] CForBenchmarks.doFastForIntArrayMultiply  1000000  avgt    3    23034.833 ±   39622.191  us/op
[info] CForBenchmarks.doFastForMin               1000000  avgt    3   322733.005 ±  109521.168  us/op
[info] CForBenchmarks.doFastForOr                1000000  avgt    3   253956.971 ± 1861700.948  us/op

@armanbilge
Copy link
Member Author

Ok, I created spire-private shims for cfor in Scala 3 and reverted all internal uses of fastFor back to cfor. This way we preserve performance on Scala 2.

@cquiroz cquiroz merged commit efcd74f into typelevel:main Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants