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

Add default constructor for MapRef #4139

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions std/shared/src/main/scala/cats/effect/std/MapRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] {

object MapRef extends MapRefCompanionPlatform {

/**
* the default Constructor for MapRef. If Async is available, it will use a ConcurrentHashMap,
* otherwise it will use a sharded immutable map.
*/
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
def apply[F[_]: Concurrent, K, V](): F[MapRef[F, K, Option[V]]] = {
yisraelU marked this conversation as resolved.
Show resolved Hide resolved

Concurrent[F] match {
case a: Async[_] =>
implicit val async: Async[F] = a
ofConcurrentHashMap()
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
case _ =>
ofShardedImmutableMap[F, K, V](Runtime.getRuntime.availableProcessors())
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
}

}

/**
* Creates a sharded map ref to reduce atomic contention on the Map, given an efficient and
* equally distributed hash, the contention should allow for interaction like a general
Expand Down
Loading