Skip to content

Commit

Permalink
interfaces: Improve fixed choice lookup (#11551)
Browse files Browse the repository at this point in the history
* interfaces: Improve fixed choice lookup

This PR adds a lazy map to match fixed choice names to their
interface, to improve the worst case for choice lookup.

Part of #10810

changelog_begin
changelog_end

* scalafmt

* apply improvements from remy
  • Loading branch information
sofiafaro-da authored Nov 4, 2021
1 parent ba96bf7 commit 4d4869f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,12 @@ object Ast {
observers: E, // Observers of the contract.
key: Option[GenTemplateKey[E]],
implements: Map[TypeConName, GenTemplateImplements[E]],
)
) {
lazy val inheritedChoices: Map[ChoiceName, TypeConName] =
implements.flatMap { case (iface, impl) =>
impl.inheritedChoices.view.map(chName => (chName, iface))
}
}

final class GenTemplateCompanion[E] private[Ast] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package language

import com.daml.lf.data.Ref._
import com.daml.lf.language.Ast._
import scalaz._, std.list._, std.either._, syntax.traverse._

private[lf] class PackageInterface(signatures: PartialFunction[PackageId, PackageSignature]) {

Expand Down Expand Up @@ -202,14 +201,15 @@ private[lf] class PackageInterface(signatures: PartialFunction[PackageId, Packag
template.choices.get(chName) match {
case Some(choice) => Right(choice)
case None =>
// TODO https://github.com/digital-asset/daml/issues/10810
// Improve lookup for fixed choices
for {
ifaces <- template.implements.keys.toList.traverse(lookupInterface(_, context))
choices = ifaces.flatMap(_.fixedChoices.get(chName).toList)
choice <-
choices.headOption.toRight(LookupError(Reference.Choice(tmpName, chName), context))
} yield choice
template.inheritedChoices.get(chName) match {
case None => Left(LookupError(Reference.Choice(tmpName, chName), context))
case Some(ifaceName) =>
lookupInterface(ifaceName, context).flatMap(iface =>
iface.fixedChoices
.get(chName)
.toRight(LookupError(Reference.Choice(ifaceName, chName), context))
)
}
}
)

Expand Down

0 comments on commit 4d4869f

Please sign in to comment.