From 4d4869f06ed5b6465549fc323839d96ab165670b Mon Sep 17 00:00:00 2001 From: Sofia Faro Date: Thu, 4 Nov 2021 18:44:48 +0000 Subject: [PATCH] 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 --- .../digitalasset/daml/lf/language/Ast.scala | 7 ++++++- .../daml/lf/language/PackageInterface.scala | 18 +++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/Ast.scala b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/Ast.scala index d16a6fdaca..5de2edf776 100644 --- a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/Ast.scala +++ b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/Ast.scala @@ -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] { diff --git a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/PackageInterface.scala b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/PackageInterface.scala index a1bd9d38ca..0fb1dbf503 100644 --- a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/PackageInterface.scala +++ b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/PackageInterface.scala @@ -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)) + ) + } } )