Skip to content

Optimize PropsAdapter + add test coverage#2743

Open
pjfanning wants to merge 5 commits intoapache:mainfrom
pjfanning:copilot/port-akka-core-pull-31695
Open

Optimize PropsAdapter + add test coverage#2743
pjfanning wants to merge 5 commits intoapache:mainfrom
pjfanning:copilot/port-akka-core-pull-31695

Conversation

@pjfanning
Copy link
Member

Port of akka/akka-core#31695. Reduces heap allocations in PropsAdapter.apply (called on every typed actor spawn) and adds the accompanying test coverage.

part of #2730

PropsAdapter optimizations (PropsAdapter.scala)

  • DefaultTypedDeploy constant — pre-built Deploy (local scope + typed default mailbox) reused across calls instead of reconstructed each time
  • TypedCreatorFunctionConsumerClazz constant — cached class reference
  • Build Deploy directly — replaces chained .withDispatcher() / .withMailbox() / .withDeploy() calls (each producing a new Props copy) with direct .copy() on Deploy
  • Fast-path for Props.empty — skips all matching logic and reuses DefaultTypedDeploy directly for the common no-custom-props case
  • Direct Props constructor — uses new pekko.actor.Props(deploy, clazz, args) to bypass factory methods and avoid extra intermediate copies

Props caching (CachedProps.scala, Behavior.scala, ActorRefFactoryAdapter.scala)

  • CachedProps.scala (new) — internal case class holding a cached typed→classic Props mapping
  • Behavior.scala — adds _internalClassicPropsCache volatile field to Behavior[T] so each behavior instance can cache its last adapted classic Props
  • ActorRefFactoryAdapter.scala — adds classicPropsFor helper that checks the behavior's cache before adapting props, avoiding repeated work when spawning many actors of the same type with the same props

Additional optimizations

  • ActorContextAdapter.scala — lazy _self field: ActorRefAdapter is not allocated unless self is actually accessed
  • ActorAdapter.scalaval supervisorStrategyfinal override def supervisorStrategy to avoid per-instance field allocation

Test coverage (PropsAdapterSpec.scala)

Added tests covering all PropsAdapter code paths:

  • MailboxSelector.defaultMailbox()"pekko.actor.typed.default-mailbox"
  • withDispatcherFromConfig("some.path") → dispatcher "some.path"
  • withDispatcherSameAsParent → dispatcher ".."
  • MailboxSelector.fromConfig("some.path") → mailbox "some.path"
  • MailboxSelector.bounded(24) → mailbox "bounded-capacity:24"
  • ActorTags.create("my-tag")deploy.tags == Set("my-tag")

Copilot AI and others added 5 commits March 16, 2026 12:37
…-core#31695)

Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
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