chore: avoid the double evaluation of entityId in ClusterSharding#1304
Merged
Roiocam merged 4 commits intoapache:mainfrom Jun 5, 2024
Merged
chore: avoid the double evaluation of entityId in ClusterSharding#1304Roiocam merged 4 commits intoapache:mainfrom
Roiocam merged 4 commits intoapache:mainfrom
Conversation
He-Pin
reviewed
Apr 30, 2024
cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/ClusterSharding.scala
Outdated
Show resolved
Hide resolved
He-Pin
reviewed
Apr 30, 2024
...ed/src/main/scala/org/apache/pekko/cluster/sharding/typed/internal/ClusterShardingImpl.scala
Outdated
Show resolved
Hide resolved
cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/ClusterSharding.scala
Outdated
Show resolved
Hide resolved
Contributor
|
Seems like duplicate code, can it be abstracted |
pjfanning
previously requested changes
May 2, 2024
Member
pjfanning
left a comment
There was a problem hiding this comment.
blocking - needs discussion on the dev list because of the ongoing release discussion
Member
Author
|
Use the cacheable partial function, and then verify it works. var calTime: Int = 0
def entityId(value: Any): String = {
calTime = calTime + 1
value match {
case "wrong" => null
case _ => String.valueOf(value)
}
}
def extractEntityIdFromExtractor: PartialFunction[Any, Any] =
new scala.runtime.AbstractPartialFunction[Any, (String, Any)] {
var cache: String = _
override def isDefinedAt(msg: Any): Boolean = {
cache = entityId(msg)
cache != null
}
override def apply(x: Any): (String, Any) = (cache, x)
}
lazy val xx = extractEntityIdFromExtractor
lazy val yy: PartialFunction[Any, Any] = {
case message if entityId(message) != null => (entityId(message), message)
}
assert(!xx.isDefinedAt("wrong"))
assert(!yy.isDefinedAt("wrong"))
calTime = 0
val health = "health"
println(xx.isDefinedAt(health))
println(xx(health))
println(s"caltime = $calTime")
println("=====")
calTime = 0
println(yy.isDefinedAt(health))
println(yy(health))
println(s"caltime = $calTime")
|
Member
|
@Roiocam is it ok to get this merged now? |
Member
Author
I have no objections to this, but it would be better if there were reviews from others. |
nvollmar
reviewed
Jun 5, 2024
...ed/src/main/scala/org/apache/pekko/cluster/sharding/typed/internal/ClusterShardingImpl.scala
Outdated
Show resolved
Hide resolved
Roiocam
added a commit
to Roiocam/pekko
that referenced
this pull request
Sep 11, 2024
…ache#1304) * chore: avoid the double evaluation of entityId in ClusterSharding * new cacheable partial function * optimized for review * fix the right type
Roiocam
added a commit
to Roiocam/pekko
that referenced
this pull request
Sep 11, 2024
…ache#1304) * chore: avoid the double evaluation of entityId in ClusterSharding * new cacheable partial function * optimized for review * fix the right type
pjfanning
added a commit
that referenced
this pull request
Sep 12, 2024
* add unit test protect ExtractEntityId can be shared safely Related with #1463 * chore: avoid the double evaluation of entityId in ClusterSharding (#1304) * chore: avoid the double evaluation of entityId in ClusterSharding * new cacheable partial function * optimized for review * fix the right type * Revert "chore: avoid the double evaluation of entityId in ClusterSharding (#1…" (#1464) This reverts commit b0e9886. * grammar fix * sort imports --------- Co-authored-by: PJ Fanning <pjfanning@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found something we could optimized it.