interfaces: Improve fixed choice lookup (#11551)

* 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
This commit is contained in:
Sofia Faro 2021-11-04 18:44:48 +00:00 committed by GitHub
parent ba96bf784c
commit 4d4869f06e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -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] {

View File

@ -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]) {
@ -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))
)
}
}
)