From c25152e8f2d4afbb05001507b91b5e1d325afbfa Mon Sep 17 00:00:00 2001 From: Gusarich Date: Sun, 1 Jun 2025 12:23:54 +0300 Subject: [PATCH 1/2] feat: optimize optional globals `__tact_randomized` and `__tact_child_contract_codes` --- dev-docs/CHANGELOG.md | 4 ++++ src/benchmarks/wallet-v5/gas.json | 10 ++++++++++ src/generator/writeProgram.ts | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dev-docs/CHANGELOG.md b/dev-docs/CHANGELOG.md index cee37573dc..eb427783f6 100644 --- a/dev-docs/CHANGELOG.md +++ b/dev-docs/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Code generation + +- Optimized optional global variables initialization: PR [#3326](https://github.com/tact-lang/tact/pull/3326) + ### Docs - Described off-chain calls and mention exit code 11 for getters: PR [#3314](https://github.com/tact-lang/tact/pull/3314) diff --git a/src/benchmarks/wallet-v5/gas.json b/src/benchmarks/wallet-v5/gas.json index 3cf3626264..4505275d65 100644 --- a/src/benchmarks/wallet-v5/gas.json +++ b/src/benchmarks/wallet-v5/gas.json @@ -149,6 +149,16 @@ "internalTransfer": "5724", "extensionTransfer": "4656" } + }, + { + "label": "1.6.13 with optimized optional globals", + "pr": "https://github.com/tact-lang/tact/pull/3326", + "gas": { + "externalTransfer": "4965", + "addExtension": "5628", + "internalTransfer": "5722", + "extensionTransfer": "4654" + } } ] } diff --git a/src/generator/writeProgram.ts b/src/generator/writeProgram.ts index b90c3fcb8c..d3b7b1d0f1 100644 --- a/src/generator/writeProgram.ts +++ b/src/generator/writeProgram.ts @@ -16,6 +16,7 @@ import { writeSerializer, } from "@/generator/writers/writeSerialization"; import { writeStdlib } from "@/generator/writers/writeStdlib"; +import { enabledOptimizedChildCode } from "@/config/features"; import { writeAccessors } from "@/generator/writers/writeAccessors"; import type { ContractABI } from "@ton/core"; import { writeFunction } from "@/generator/writers/writeFunction"; @@ -116,8 +117,18 @@ export async function writeProgram( globalVariables.push("global slice __tact_context_sender;"); } - globalVariables.push("global cell __tact_child_contract_codes;"); - globalVariables.push("global int __tact_randomized;"); + const needsChildCodes = + contract.dependsOn.length > 0 && !enabledOptimizedChildCode(ctx); + if (needsChildCodes) { + globalVariables.push("global cell __tact_child_contract_codes;"); + } + + const needsRandom = functions.some( + (f) => f.name === "__tact_prepare_random", + ); + if (needsRandom) { + globalVariables.push("global int __tact_randomized;"); + } if (contract.globalVariables.has("inMsg")) { globalVariables.push("global slice __tact_in_msg;"); From 10679004bb10e2d663f71fd687ddf248ee6e61fd Mon Sep 17 00:00:00 2001 From: Gusarich Date: Sun, 1 Jun 2025 12:26:00 +0300 Subject: [PATCH 2/2] fix: pr number --- dev-docs/CHANGELOG.md | 2 +- src/benchmarks/wallet-v5/gas.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-docs/CHANGELOG.md b/dev-docs/CHANGELOG.md index eb427783f6..ae79d88251 100644 --- a/dev-docs/CHANGELOG.md +++ b/dev-docs/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Code generation -- Optimized optional global variables initialization: PR [#3326](https://github.com/tact-lang/tact/pull/3326) +- Optimized optional global variables initialization: PR [#3327](https://github.com/tact-lang/tact/pull/3327) ### Docs diff --git a/src/benchmarks/wallet-v5/gas.json b/src/benchmarks/wallet-v5/gas.json index 4505275d65..d364f344bd 100644 --- a/src/benchmarks/wallet-v5/gas.json +++ b/src/benchmarks/wallet-v5/gas.json @@ -152,7 +152,7 @@ }, { "label": "1.6.13 with optimized optional globals", - "pr": "https://github.com/tact-lang/tact/pull/3326", + "pr": "https://github.com/tact-lang/tact/pull/3327", "gas": { "externalTransfer": "4965", "addExtension": "5628",