Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@LGLO
Copy link
Contributor

@LGLO LGLO commented Mar 9, 2019

I open this to have version of bracket suitable for hot-path in performance sensitive applications.

@LGLO LGLO requested a review from jdegoes March 9, 2019 17:27
jdegoes
jdegoes previously approved these changes Mar 10, 2019
Copy link
Member

@jdegoes jdegoes left a comment

Choose a reason for hiding this comment

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

Looks great to me! You preserved all the code from the old versions, right? Just moved them over?

Also, would be nice to have a little code that shows the uncurried ones infer (somewhat).

@jdegoes
Copy link
Member

jdegoes commented Mar 10, 2019

@LGLO Thanks a lot for doing this! Let's merge after the build is passing.

@LGLO
Copy link
Contributor Author

LGLO commented Mar 10, 2019

👍 I'll try to retain scaladoc link, although it's tricky.
When I do scaladoc fix, then I'll also add specs for inference (to see that besides compilation there are no cast exceptions).

@jdegoes
Copy link
Member

jdegoes commented Mar 13, 2019

@LGLO Would love to get this in as soon as the build passes!

@LGLO
Copy link
Contributor Author

LGLO commented Mar 15, 2019

@jdegoes I've run this benchmark:

  @Benchmark
  def curriedBracket: UIO[Unit] = {
      ZIO.bracket(ZIO.unit)(_ => ZIO.unit)(_ => ZIO.unit)
  }
  @Benchmark
  def tupledBracket: UIO[Unit] = {
    ZIO.bracket(ZIO.unit, (_: Unit) => ZIO.unit, (_: Unit) => ZIO.unit)
  }

5 iterations, 30 seconds each (to let GC work) and observed no difference:

[info] Benchmark                                                                        Mode  Cnt     Score     Error   Units
[info] BracketBenchmark.curriedBracket                                                  avgt    5    28.820 ±   3.596   ns/op
[info] BracketBenchmark.curriedBracket:·gc.alloc.rate                                   avgt    5  4429.676 ± 552.787  MB/sec
[info] BracketBenchmark.curriedBracket:·gc.alloc.rate.norm                              avgt    5   136.002 ±   0.014    B/op
[info] BracketBenchmark.curriedBracket:·gc.churn.CodeHeap_'non-profiled_nmethods'       avgt    5     0.008 ±   0.003  MB/sec
[info] BracketBenchmark.curriedBracket:·gc.churn.CodeHeap_'non-profiled_nmethods'.norm  avgt    5    ≈ 10⁻⁴              B/op
[info] BracketBenchmark.curriedBracket:·gc.churn.G1_Old_Gen                             avgt    5  4437.325 ± 550.946  MB/sec
[info] BracketBenchmark.curriedBracket:·gc.churn.G1_Old_Gen.norm                        avgt    5   136.237 ±   0.566    B/op
[info] BracketBenchmark.curriedBracket:·gc.count                                        avgt    5  1434.000            counts
[info] BracketBenchmark.curriedBracket:·gc.time                                         avgt    5  1306.000                ms
[info] BracketBenchmark.tupledBracket                                                   avgt    5    28.790 ±   3.233   ns/op
[info] BracketBenchmark.tupledBracket:·gc.alloc.rate                                    avgt    5  4433.757 ± 496.258  MB/sec
[info] BracketBenchmark.tupledBracket:·gc.alloc.rate.norm                               avgt    5   136.000 ±   0.001    B/op
[info] BracketBenchmark.tupledBracket:·gc.churn.CodeHeap_'non-profiled_nmethods'        avgt    5     0.009 ±   0.003  MB/sec
[info] BracketBenchmark.tupledBracket:·gc.churn.CodeHeap_'non-profiled_nmethods'.norm   avgt    5    ≈ 10⁻⁴              B/op
[info] BracketBenchmark.tupledBracket:·gc.churn.G1_Old_Gen                              avgt    5  4443.315 ± 493.961  MB/sec
[info] BracketBenchmark.tupledBracket:·gc.churn.G1_Old_Gen.norm                         avgt    5   136.294 ±   0.773    B/op
[info] BracketBenchmark.tupledBracket:·gc.count                                         avgt    5  1558.000            counts
[info] BracketBenchmark.tupledBracket:·gc.time                                          avgt    5  1356.000                ms

I cannot prove this PR helps in anything.
I guess it should be closed.

@jdegoes
Copy link
Member

jdegoes commented Mar 16, 2019

@LGLO I am ok merging, actually, because in the best case the JIT can eliminate short-lived allocations, but that depends a lot on the code being run, version of JRE, etc. At least with these versions, we have a guarantee of minimal allocation. What do you think?

@LGLO LGLO force-pushed the bracket-uncurried-for-performance branch from 27ba045 to 4c0342a Compare March 17, 2019 22:25
} yield (result must_=== 0L) and (released must_=== true)
}

object UncurriedBracketCompilesRegardlessOrderOfEAndRTypes {
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 don't really know where to place these if-compiles-works tests.

* Finalizers offer very powerful guarantees, but they are low-level, and
* should generally not be used for releasing resources. For higher-level
* logic built on `ensuring`, see [[ZIO#bracket]].
* logic built on `ensuring`, see `ZIO#bracket`.
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 sorry for this one, but scaladoc finds two bracket methods now and I cannot find method when signature contains type bounded by type LowerR declared inside ZIO.

@LGLO
Copy link
Contributor Author

LGLO commented Mar 17, 2019

Okey!
I've fixed doc and added test.

Copy link
Member

@jdegoes jdegoes left a comment

Choose a reason for hiding this comment

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

Looks great!

@jdegoes jdegoes merged commit 10a63c1 into zio:master Mar 18, 2019
@LGLO LGLO deleted the bracket-uncurried-for-performance branch May 3, 2019 19:37
ghostdogpr pushed a commit to ghostdogpr/scalaz-zio that referenced this pull request May 5, 2019
* Uncurried versions of 'bracket' and 'bracketExit'

* Fixed doc and added specs
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