Skip to content

Commit

Permalink
[Speedy] remove some useless optTargetTemplateId (#19196)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyhaemmerle-da authored May 14, 2024
1 parent 2d72a2a commit c1ced1f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
}
}
}
Expand Down Expand Up @@ -482,7 +480,6 @@ private[lf] final class Compiler(
private[this] def translateChoiceBody(
env: Env,
tmplId: TypeConName,
optTargetTemplateId: Option[TypeConName],
tmpl: Template,
choice: TemplateChoice,
)(
Expand All @@ -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))),
),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) = \ <actors> <key> <choiceArg> <token> ->
Expand All @@ -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,
)
}
}
}
Expand All @@ -731,15 +725,13 @@ 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
tokenPos: Position,
): s.SExpr = {
SBUInsertFetchNode(
tmplId,
optTargetTemplateId,
byKey = mbKey.isDefined,
)(
env.toSEVar(cidPos),
Expand All @@ -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,
Expand Down Expand Up @@ -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) = \ <key> <token> ->
Expand All @@ -994,14 +984,13 @@ private[lf] final class Compiler(
// in <mbCid>
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)
}
}
}
}
Expand All @@ -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) = \ <key> <token> ->
Expand All @@ -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))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down Expand Up @@ -1429,7 +1427,6 @@ private[lf] object SBuiltinFun {
*/
final case class SBUInsertFetchNode(
templateId: TypeConName,
optTargetTemplateId: Option[TypeConName],
byKey: Boolean,
) extends UpdateBuiltin(2) {

Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 =>
Expand All @@ -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 =>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {

Expand Down

0 comments on commit c1ced1f

Please sign in to comment.