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

Skip to content

Conversation

@kyri-petrou
Copy link
Contributor

This PR (micro)-optimizes various methods related to usage of Scope in ZIO. The main optimizations come from reducing the number of flatMaps and unnecessary object creation / Long boxing when creating / closing a forked Scope (which happens a lot particularly in ZStream methods). It also optimizes Scope access (via ZIO.scope / ZIO.scopeWith) by adding a new accessor method to ZEnvironment for retrieving the Scope within it, bypassing the need to check the service tag.

Comment on lines 4654 to 4655
def scopeWith[R, E, A](f: Scope => ZIO[R, E, A])(implicit trace: Trace): ZIO[R with Scope, E, A] =
ZIO.serviceWithZIO[Scope](f)
FiberRef.currentEnvironment.getWith(env => f(env.unsafe.getScope(Unsafe)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is 'safe' because of with Scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It's not any different than what was happening previously. The only thing that is making things "safe" is that the Scope is that Scope needs to be erased from R. Unsafely casting the type to remove the Scope from the requirement will cause a runtime error (both previously and now)

# Conflicts:
#	core/shared/src/main/scala/zio/ZEnvironment.scala
@regiskuckaertz
Copy link
Member

I wonder if it improves the streaming benchmarks 😇

@kyri-petrou
Copy link
Contributor Author

kyri-petrou commented Dec 9, 2024

@regiskuckaertz there seems to be a 5-10% improvement in the StreamParBenchmarks suite across the board:

series/2.x

[info] Benchmark                                   (chunkCount)  (chunkSize)  (parChunkSize)   Mode  Cnt     Score     Error  Units
[info] StreamParBenchmark.zioFlatMapPar                   10000         5000              50  thrpt    5     0.351 ±   0.083  ops/s
[info] StreamParBenchmark.zioFlatMapParChunks             10000         5000              50  thrpt    5    11.220 ±   0.097  ops/s
[info] StreamParBenchmark.zioFlatMapParChunksFair         10000         5000              50  thrpt    5     2.930 ±   0.178  ops/s
[info] StreamParBenchmark.zioMapPar                       10000         5000              50  thrpt    5     0.449 ±   0.005  ops/s
[info] StreamParBenchmark.zioMapParUnordered              10000         5000              50  thrpt    5     0.440 ±   0.004  ops/s
[info] StreamParBenchmark.zioMerge                        10000         5000              50  thrpt    5  7444.306 ±  75.253  ops/s
[info] StreamParBenchmark.zioMerge2                       10000         5000              50  thrpt    5  7596.682 ± 140.174  ops/s
[info] StreamParBenchmark.zioMergeWithIdentity            10000         5000              50  thrpt    5  6601.866 ± 210.041  ops/s

PR:

[info] Benchmark                                   (chunkCount)  (chunkSize)  (parChunkSize)   Mode  Cnt     Score     Error  Units
[info] StreamParBenchmark.zioFlatMapPar                   10000         5000              50  thrpt    5     0.402 ±   0.005  ops/s
[info] StreamParBenchmark.zioFlatMapParChunks             10000         5000              50  thrpt    5    11.847 ±   0.172  ops/s
[info] StreamParBenchmark.zioFlatMapParChunksFair         10000         5000              50  thrpt    5     3.012 ±   0.094  ops/s
[info] StreamParBenchmark.zioMapPar                       10000         5000              50  thrpt    5     0.499 ±   0.005  ops/s
[info] StreamParBenchmark.zioMapParUnordered              10000         5000              50  thrpt    5     0.541 ±   0.053  ops/s
[info] StreamParBenchmark.zioMerge                        10000         5000              50  thrpt    5  7930.111 ± 102.962  ops/s
[info] StreamParBenchmark.zioMerge2                       10000         5000              50  thrpt    5  8040.346 ±  91.849  ops/s
[info] StreamParBenchmark.zioMergeWithIdentity            10000         5000              50  thrpt    5  6974.765 ±  85.280  ops/s

@kyri-petrou kyri-petrou merged commit ac74c77 into zio:series/2.x Dec 9, 2024
18 checks passed
@kyri-petrou kyri-petrou deleted the optimize-scope branch December 9, 2024 23:50
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.

3 participants