From c1ced1fb8b60394c57b06f31f3afd4be522c33ec Mon Sep 17 00:00:00 2001 From: Remy Date: Tue, 14 May 2024 17:07:06 +0200 Subject: [PATCH] [Speedy] remove some useless optTargetTemplateId (#19196) --- .../scala/com/daml/lf/speedy/Compiler.scala | 81 ++++++++----------- .../com/daml/lf/speedy/SBuiltinFun.scala | 39 ++++----- .../lf/speedy/SBuiltinInterfaceTest.scala | 2 +- 3 files changed, 50 insertions(+), 72 deletions(-) diff --git a/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/Compiler.scala b/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/Compiler.scala index 33eac69bd6..faa5d21fef 100644 --- a/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/Compiler.scala +++ b/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/Compiler.scala @@ -369,10 +369,8 @@ private[lf] final class Compiler( module.templates.foreach { case (tmplName, tmpl) => val tmplId = Identifier(pkgId, QualifiedName(module.name, tmplName)) - val targetTemplateId = Some(tmplId) - addDef(compileCreate(tmplId, tmpl)) - addDef(compileFetchTemplate(tmplId, targetTemplateId)) + addDef(compileFetchTemplate(tmplId)) addDef(compileTemplatePreCondition(tmplId, tmpl)) addDef(compileSignatories(tmplId, tmpl)) addDef(compileObservers(tmplId, tmpl)) @@ -388,17 +386,17 @@ private[lf] final class Compiler( } tmpl.choices.values.foreach { choice => - addDef(compileTemplateChoice(tmplId, tmpl, choice, targetTemplateId)) + addDef(compileTemplateChoice(tmplId, tmpl, choice)) addDef(compileChoiceController(tmplId, tmpl.param, choice)) addDef(compileChoiceObserver(tmplId, tmpl.param, choice)) } tmpl.key.foreach { tmplKey => addDef(compileContractKeyWithMaintainers(tmplId, tmpl, tmplKey)) - addDef(compileFetchByKey(tmplId, tmplKey, targetTemplateId)) - addDef(compileLookupByKey(tmplId, tmplKey, targetTemplateId)) + addDef(compileFetchByKey(tmplId, tmplKey)) + addDef(compileLookupByKey(tmplId, tmplKey)) tmpl.choices.values.foreach { x => - addDef(compileChoiceByKey(tmplId, tmpl, tmplKey, x, targetTemplateId)) + addDef(compileChoiceByKey(tmplId, tmpl, tmplKey, x)) } } } @@ -482,7 +480,6 @@ private[lf] final class Compiler( private[this] def translateChoiceBody( env: Env, tmplId: TypeConName, - optTargetTemplateId: Option[TypeConName], tmpl: Template, choice: TemplateChoice, )( @@ -495,7 +492,7 @@ private[lf] final class Compiler( env, SBCastAnyContract(tmplId)( env.toSEVar(cidPos), - SBFetchAny(optTargetTemplateId)( + SBFetchAny(Some(tmplId))( env.toSEVar(cidPos), mbKey.fold(s.SEValue.None: s.SExpr)(pos => SBSome(env.toSEVar(pos))), ), @@ -645,11 +642,10 @@ private[lf] final class Compiler( tmplId: TypeConName, tmpl: Template, choice: TemplateChoice, - optTargetTemplateId: Option[TypeConName], ): (t.SDefinitionRef, SDefinition) = topLevelFunction3(t.TemplateChoiceDefRef(tmplId, choice.name)) { (cidPos, choiceArgPos, tokenPos, env) => - translateChoiceBody(env, tmplId, optTargetTemplateId, tmpl, choice)( + translateChoiceBody(env, tmplId, tmpl, choice)( choiceArgPos, cidPos, None, @@ -701,7 +697,6 @@ private[lf] final class Compiler( tmpl: Template, tmplKey: TemplateKey, choice: TemplateChoice, - optTargetTemplateId: Option[TypeConName], ): (t.SDefinitionRef, SDefinition) = // Compiles a choice into: // ChoiceByKeyDefRef(SomeTemplate, SomeChoice) = \ -> @@ -715,14 +710,13 @@ private[lf] final class Compiler( topLevelFunction3(t.ChoiceByKeyDefRef(tmplId, choice.name)) { (keyPos, choiceArgPos, tokenPos, env) => let(env, translateKeyWithMaintainers(env, keyPos, tmplKey)) { (keyWithMPos, env) => - let(env, SBUFetchKey(tmplId, optTargetTemplateId)(env.toSEVar(keyWithMPos))) { - (cidPos, env) => - translateChoiceBody(env, tmplId, optTargetTemplateId, tmpl, choice)( - choiceArgPos, - cidPos, - Some(keyWithMPos), - tokenPos, - ) + let(env, SBUFetchKey(tmplId)(env.toSEVar(keyWithMPos))) { (cidPos, env) => + translateChoiceBody(env, tmplId, tmpl, choice)( + choiceArgPos, + cidPos, + Some(keyWithMPos), + tokenPos, + ) } } } @@ -731,7 +725,6 @@ private[lf] final class Compiler( private[this] def translateFetchTemplateBody( env: Env, tmplId: Identifier, - optTargetTemplateId: Option[TypeConName], )( cidPos: Position, mbKey: Option[Position], // defined for byKey operation @@ -739,7 +732,6 @@ private[lf] final class Compiler( ): s.SExpr = { SBUInsertFetchNode( tmplId, - optTargetTemplateId, byKey = mbKey.isDefined, )( env.toSEVar(cidPos), @@ -749,11 +741,10 @@ private[lf] final class Compiler( } private[this] def compileFetchTemplate( - tmplId: Identifier, - optTargetTemplateId: Option[TypeConName], + tmplId: Identifier ): (t.SDefinitionRef, SDefinition) = topLevelFunction2(t.FetchTemplateDefRef(tmplId)) { (cidPos, tokenPos, env) => - translateFetchTemplateBody(env, tmplId, optTargetTemplateId)( + translateFetchTemplateBody(env, tmplId)( cidPos, None, tokenPos, @@ -984,7 +975,6 @@ private[lf] final class Compiler( private[this] def compileLookupByKey( tmplId: Identifier, tmplKey: TemplateKey, - optTargetTemplateId: Option[TypeConName], ): (t.SDefinitionRef, SDefinition) = // compile a template with key into // LookupByKeyDefRef(tmplId) = \ -> @@ -994,14 +984,13 @@ private[lf] final class Compiler( // in topLevelFunction2(t.LookupByKeyDefRef(tmplId)) { (keyPos, _, env) => let(env, translateKeyWithMaintainers(env, keyPos, tmplKey)) { (keyWithMPos, env) => - let(env, SBULookupKey(tmplId, optTargetTemplateId)(env.toSEVar(keyWithMPos))) { - (maybeCidPos, env) => - let( - env, - SBUInsertLookupNode(tmplId)(env.toSEVar(keyWithMPos), env.toSEVar(maybeCidPos)), - ) { (_, env) => - env.toSEVar(maybeCidPos) - } + let(env, SBULookupKey(tmplId)(env.toSEVar(keyWithMPos))) { (maybeCidPos, env) => + let( + env, + SBUInsertLookupNode(tmplId)(env.toSEVar(keyWithMPos), env.toSEVar(maybeCidPos)), + ) { (_, env) => + env.toSEVar(maybeCidPos) + } } } } @@ -1013,7 +1002,6 @@ private[lf] final class Compiler( private[this] def compileFetchByKey( tmplId: TypeConName, tmplKey: TemplateKey, - optTargetTemplateId: Option[TypeConName], ): (t.SDefinitionRef, SDefinition) = // compile a template with key into // FetchByKeyDefRef(tmplId) = \ -> @@ -1024,18 +1012,17 @@ private[lf] final class Compiler( // in { contractId: ContractId Foo, contract: Foo } topLevelFunction2(t.FetchByKeyDefRef(tmplId)) { (keyPos, tokenPos, env) => let(env, translateKeyWithMaintainers(env, keyPos, tmplKey)) { (keyWithMPos, env) => - let(env, SBUFetchKey(tmplId, optTargetTemplateId)(env.toSEVar(keyWithMPos))) { - (cidPos, env) => - let( - env, - translateFetchTemplateBody(env, tmplId, optTargetTemplateId)( - cidPos, - Some(keyWithMPos), - tokenPos, - ), - ) { (contractPos, env) => - FetchByKeyResult(env.toSEVar(cidPos), env.toSEVar(contractPos)) - } + let(env, SBUFetchKey(tmplId)(env.toSEVar(keyWithMPos))) { (cidPos, env) => + let( + env, + translateFetchTemplateBody(env, tmplId)( + cidPos, + Some(keyWithMPos), + tokenPos, + ), + ) { (contractPos, env) => + FetchByKeyResult(env.toSEVar(cidPos), env.toSEVar(contractPos)) + } } } } diff --git a/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/SBuiltinFun.scala b/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/SBuiltinFun.scala index 13a2adac40..f5353fd80c 100644 --- a/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/SBuiltinFun.scala +++ b/sdk/daml-lf/interpreter/src/main/scala/com/daml/lf/speedy/SBuiltinFun.scala @@ -1258,11 +1258,9 @@ private[lf] object SBuiltinFun { args: util.ArrayList[SValue], machine: Machine[Q], ): Control.Expression = { - val optTargetTemplateId: Option[TypeConName] = None // no upgrading val e = SEBuiltinFun( SBUInsertFetchNode( getSAnyContract(args, 0)._1, - optTargetTemplateId, byKey = false, ) ) @@ -1429,7 +1427,6 @@ private[lf] object SBuiltinFun { */ final case class SBUInsertFetchNode( templateId: TypeConName, - optTargetTemplateId: Option[TypeConName], byKey: Boolean, ) extends UpdateBuiltin(2) { @@ -1439,7 +1436,7 @@ private[lf] object SBuiltinFun { ): Control[Question.Update] = { val coid = getSContractId(args, 0) val keyOpt: SValue = args.get(1) - fetchContract(machine, templateId, optTargetTemplateId, coid, keyOpt) { templateArg => + fetchContract(machine, templateId, coid, keyOpt) { templateArg => getContractInfo(machine, coid, templateId, templateArg, keyOpt) { contract => val version = machine.tmplId2TxVersion(templateId) machine.ptx.insertFetch( @@ -1540,8 +1537,7 @@ private[lf] object SBuiltinFun { } private[speedy] sealed abstract class SBUKeyBuiltin( - operation: KeyOperation, - optTargetTemplateId: Option[TypeConName], + operation: KeyOperation ) extends UpdateBuiltin(1) with Product { override protected def executeUpdate( @@ -1572,11 +1568,10 @@ private[lf] object SBuiltinFun { machine.ptx = machine.ptx.copy(contractState = next) keyMapping match { case ContractStateMachine.KeyActive(coid) => - fetchContract(machine, templateId, optTargetTemplateId, coid, keyOpt) { - templateArg => - getContractInfo(machine, coid, templateId, templateArg, keyOpt)(_ => - operation.handleKeyFound(coid) - ) + fetchContract(machine, templateId, coid, keyOpt) { templateArg => + getContractInfo(machine, coid, templateId, templateArg, keyOpt)(_ => + operation.handleKeyFound(coid) + ) } case ContractStateMachine.KeyInactive => @@ -1590,11 +1585,10 @@ private[lf] object SBuiltinFun { keyMapping match { case ContractStateMachine.KeyActive(coid) => val c = - fetchContract(machine, templateId, optTargetTemplateId, coid, keyOpt) { - templateArg => - getContractInfo(machine, coid, templateId, templateArg, keyOpt)(_ => - operation.handleKeyFound(coid) - ) + fetchContract(machine, templateId, coid, keyOpt) { templateArg => + getContractInfo(machine, coid, templateId, templateArg, keyOpt)(_ => + operation.handleKeyFound(coid) + ) } (c, true) case ContractStateMachine.KeyInactive => @@ -1623,18 +1617,16 @@ private[lf] object SBuiltinFun { * -> ContractId T */ final case class SBUFetchKey( - templateId: TypeConName, - optTargetTemplateId: Option[TypeConName] = None, - ) extends SBUKeyBuiltin(new KeyOperation.Fetch(templateId), optTargetTemplateId) + templateId: TypeConName + ) extends SBUKeyBuiltin(new KeyOperation.Fetch(templateId)) /** $lookupKey[T] * :: { key: key, maintainers: List Party } * -> Maybe (ContractId T) */ final case class SBULookupKey( - templateId: TypeConName, - optTargetTemplateId: Option[TypeConName] = None, - ) extends SBUKeyBuiltin(new KeyOperation.Lookup(templateId), optTargetTemplateId) + templateId: TypeConName + ) extends SBUKeyBuiltin(new KeyOperation.Lookup(templateId)) /** $getTime :: Token -> Timestamp */ final case object SBUGetTime extends UpdateBuiltin(1) { @@ -2159,11 +2151,10 @@ private[lf] object SBuiltinFun { private def fetchContract( machine: UpdateMachine, templateId: TypeConName, - optTargetTemplateId: Option[TypeConName], coid: V.ContractId, keyOpt: SValue, )(f: SValue => Control[Question.Update]): Control[Question.Update] = { - fetchAny(machine, optTargetTemplateId, coid, keyOpt) { fetched => + fetchAny(machine, Some(templateId), coid, keyOpt) { fetched => // The SBCastAnyContract check can never fail when the upgrading feature flag is enabled. // This is because the contract got up/down-graded when imported by importValue. diff --git a/sdk/daml-lf/interpreter/src/test/scala/com/daml/lf/speedy/SBuiltinInterfaceTest.scala b/sdk/daml-lf/interpreter/src/test/scala/com/daml/lf/speedy/SBuiltinInterfaceTest.scala index 1821bbbd01..fa3e1fff5e 100644 --- a/sdk/daml-lf/interpreter/src/test/scala/com/daml/lf/speedy/SBuiltinInterfaceTest.scala +++ b/sdk/daml-lf/interpreter/src/test/scala/com/daml/lf/speedy/SBuiltinInterfaceTest.scala @@ -172,7 +172,7 @@ final class SBuiltinInterfaceTestHelpers(majorLanguageVersion: LanguageMajorVers require(extraPkgId != basePkgId) val basePkgNameMap = - List(basePkg.name, extraPkgName).map(n => n -> basePkgId).toMap + Map(basePkg.name -> basePkgId, extraPkgName -> extraPkgId) lazy val extendedPkgs = {