Skip to content

chore: avoid the double evaluation of entityId in ClusterSharding#1304

Merged
Roiocam merged 4 commits intoapache:mainfrom
Roiocam:avoid-double-eval
Jun 5, 2024
Merged

chore: avoid the double evaluation of entityId in ClusterSharding#1304
Roiocam merged 4 commits intoapache:mainfrom
Roiocam:avoid-double-eval

Conversation

@Roiocam
Copy link
Member

@Roiocam Roiocam commented Apr 30, 2024

Found something we could optimized it.

@Roiocam Roiocam marked this pull request as draft April 30, 2024 17:08
@laglangyue
Copy link
Contributor

Seems like duplicate code, can it be abstracted

Copy link
Member

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

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

blocking - needs discussion on the dev list because of the ongoing release discussion

@pjfanning pjfanning added this to the 1.1.0-M2 milestone May 7, 2024
@Roiocam
Copy link
Member Author

Roiocam commented May 13, 2024

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")


@Roiocam Roiocam marked this pull request as ready for review May 13, 2024 05:38
@Roiocam Roiocam requested a review from He-Pin May 13, 2024 05:52
@pjfanning pjfanning dismissed their stale review May 15, 2024 11:47

1.1.0-M1 vote passed

@Roiocam Roiocam requested a review from pjfanning May 17, 2024 01:41
Copy link
Member

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

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

lgtm

@pjfanning
Copy link
Member

@Roiocam is it ok to get this merged now?

@Roiocam
Copy link
Member Author

Roiocam commented Jun 4, 2024

@Roiocam is it ok to get this merged now?

I have no objections to this, but it would be better if there were reviews from others.

Copy link
Contributor

@nvollmar nvollmar left a comment

Choose a reason for hiding this comment

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

lgtm

@Roiocam Roiocam merged commit b0e9886 into apache:main Jun 5, 2024
@Roiocam Roiocam deleted the avoid-double-eval branch June 5, 2024 15:23
@pjfanning pjfanning added the release notes Need to release note label Aug 14, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes Need to release note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants