{"id":33278849,"url":"https://github.com/ton-blockchain/tolk-bench","last_synced_at":"2025-11-17T10:04:56.862Z","repository":{"id":315430228,"uuid":"1014392148","full_name":"ton-blockchain/tolk-bench","owner":"ton-blockchain","description":"\"Tolk vs FunC\" gas benchmarks — and at the same time, reference contracts in Tolk","archived":false,"fork":false,"pushed_at":"2025-11-13T20:08:56.000Z","size":595,"stargazers_count":27,"open_issues_count":1,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-13T22:10:12.644Z","etag":null,"topics":["func","tolk","ton"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ton-blockchain.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-07-05T16:24:29.000Z","updated_at":"2025-11-13T20:09:00.000Z","dependencies_parsed_at":"2025-09-18T15:59:23.709Z","dependency_job_id":"1b74e047-dbd7-401b-a43c-57375cb2c1da","html_url":"https://github.com/ton-blockchain/tolk-bench","commit_stats":null,"previous_names":["ton-blockchain/tolk-bench"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ton-blockchain/tolk-bench","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ton-blockchain%2Ftolk-bench","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ton-blockchain%2Ftolk-bench/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ton-blockchain%2Ftolk-bench/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ton-blockchain%2Ftolk-bench/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ton-blockchain","download_url":"https://codeload.github.com/ton-blockchain/tolk-bench/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ton-blockchain%2Ftolk-bench/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284861040,"owners_count":27075156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-17T02:00:06.431Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["func","tolk","ton"],"created_at":"2025-11-17T10:04:54.310Z","updated_at":"2025-11-17T10:04:56.854Z","avatar_url":"https://github.com/ton-blockchain.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \"Tolk vs FunC\" gas benchmarks\n\nThis repository demonstrates how real smart contracts look in **Tolk** — and how efficient they can be.\n\nI took several standard contracts from the TON ecosystem (Jetton, NFT, Wallet, etc.) and migrated them **from FunC to Tolk**:\n- preserving the original logic and behavior,\n- **passing the same test suites** as the FunC versions,\n- but written in **idiomatic, expressive Tolk style**,\n- with **significantly reduced gas costs**.\n\n**The goal is to show that Tolk can replace FunC** not just in theory — but in production, today.\n\n\n## 📊 Benchmarks!\n\nIn gas units, plus code side (bits / cells).\n\n### 01 — basic jetton\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| MINT jettons by admin           | 19278      | 11914      | **-38.20%**     |\n| TRANSFER with forward_amount    | 19420      | 12904      | **-33.55%**     |\n| TRANSFER no forward_amount      | 16964      | 10401      | **-38.69%**     |\n| BURN jettons                    | 12514      | 7952       | **-36.46%**     |\n| DISCOVER with include_address   | 7116       | 4456       | **-37.38%**     |\n| DISCOVER no include_address     | 6554       | 3894       | **-40.59%**     |\n| code size: minter               | 5046 / 14  | 4130 / 12  |                 |\n| code size: wallet               | 6081 / 18  | 4485 / 11  |                 |\n\n### 02 — basic nft\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| DEPLOY nft                      | 10469      | 5510       | **-47.37%**     |\n| BATCH deploy nft                | 1049281    | 458212     | **-56.33%**     |\n| TRANSFER nft                    | 7109       | 4478       | **-37.01%**     |\n| GET static data                 | 4535       | 2543       | **-43.93%**     |\n| code size: nft-item             | 3441 / 14  | 2581 / 7   |                 |\n| code size: nft-collection       | 3564 / 19  | 3939 / 14  |                 |\n\n### 03 — notcoin\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| MINT jettons by admin           | 17053      | 10664      | **-37.47%**     |\n| TRANSFER with forward_amount    | 19638      | 14024      | **-28.59%**     |\n| TRANSFER no forward_amount      | 16984      | 11655      | **-31.38%**     |\n| BURN jettons                    | 12732      | 8532       | **-32.99%**     |\n| DISCOVER with include_address   | 7107       | 4195       | **-40.97%**     |\n| DISCOVER no include_address     | 6545       | 3633       | **-44.49%**     |\n| code size: minter               | 8782 / 21  | 7119 / 16  |                 |\n| code size: wallet               | 6794 / 15  | 5314 / 12  |                 |\n\n### 04 — sharded jetton (tgBTC)\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| MINT jettons by admin           | 15019      | 10830      | **-27.89%**     |\n| TRANSFER with forward_amount    | 20021      | 14388      | **-28.14%**     |\n| TRANSFER no forward_amount      | 17489      | 12019      | **-31.28%**     |\n| BURN jettons                    | 13089      | 8634       | **-34.04%**     |\n| DISCOVER with include_address   | 7290       | 4429       | **-39.25%**     |\n| DISCOVER no include_address     | 6728       | 3867       | **-42.52%**     |\n| code size: minter               | 9454 / 21  | 7639 / 16  |                 |\n| code size: wallet               | 6941 / 13  | 5333 / 10  |                 |\n\n### 05 — wallet V5\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| EXTERNAL transfer               | 4939       | 3869       | **-21.66%**     |\n| INTERNAL transfer               | 5645       | 4351       | **-22.92%**     |\n| ADD extension                   | 6110       | 4951       | **-18.97%**     |\n| EXTENSION transfer              | 3854       | 2951       | **-23.43%**     |\n| code size: wallet_v5            | 4583 / 20  | 5775 / 20  |                 |\n\n### 06 — vesting wallet\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| ADD whitelist                   | 8000       | 4384       | **-45.20%**     |\n| UNLOCK internal partial         | 8365       | 5896       | **-29.52%**     |\n| INTERNAL to vesting sender      | 6543       | 5160       | **-21.14%**     |\n| INTERNAL to elector             | 11062      | 7623       | **-31.09%**     |\n| INTERNAL to single nominator    | 10338      | 6748       | **-34.73%**     |\n| INTERNAL with comment           | 10628      | 6836       | **-35.68%**     |\n| EXTERNAL transfer after unlock  | 6740       | 4509       | **-33.10%**     |\n| code size: vesting wallet       | 7413 / 28  | 7037 / 26  |                 |\n\n### 07 — telemint (telegram gifts)\n\n| Operation                       | FunC       | Tolk       | **Gas savings** |\n|---------------------------------|------------|------------|-----------------|\n| EXTERNAL to collection          | 2569       | 1161       | **-54.81%**     |\n| DEPLOY item by admin            | 23706      | 14205      | **-40.08%**     |\n| REJECT incorrect sender         | 24142      | 14903      | **-38.27%**     |\n| OVERRIDE restrictions           | 24069      | 14835      | **-38.36%**     |\n| INCREASE next bid               | 12640      | 7334       | **-41.98%**     |\n| AUCTION end on expire           | 11317      | 7932       | **-29.91%**     |\n| AUCTION end reached max bid     | 20497      | 12805      | **-37.53%**     |\n| AUCTION start by owner          | 7762       | 4118       | **-46.95%**     |\n| AUCTION cancel if no bids       | 6341       | 3189       | **-49.71%**     |\n| AUCTION first bid               | 10394      | 5477       | **-47.31%**     |\n| AUCTION next bid                | 12640      | 7334       | **-41.98%**     |\n| TRANSFER item                   | 9508       | 6619       | **-30.38%**     |\n| GET royalty params              | 4245       | 2906       | **-31.54%**     |\n| code size: NftCollection        | 4649 / 17  | 3695 / 15  |                 |\n| code size: NftItem              | 14785 / 37 | 13048 / 34 |                 |\n\n\u003cbr\u003e\n\n## How does Tolk achieve these numbers?\n\n#### 1. Language design and type system\n\nTolk code is closer to business logic — and still maps cleanly to the TVM's stack model.\n\nThis is not \"compiler magic\" — it's a result of language design. Just writing straightforward code is often more efficient than manual stack juggling in FunC.\n\nFor instance, universal `createMessage`, based on unions, is more lightweight than hand-crafted message cell composition. It also handles `StateInit` and deployment without creating extra cells.\n\n#### 2. The `lazy` keyword\n\nThe compiler decides when and where to load data from slices. It enables:\n- prefix-based lazy matching without creating unions on a stack,\n- loading only the fields you actually use,\n- skipping over unused fields or references,\n- computing immutable sub-slices for serializing back.\n\n#### 3. Optimizing compiler\n\nInlining, constant condition folding, grouping of sequential `storeInt`, peephole optimizations, stack reordering — all applied automatically.\n\n#### 4. TVM-11 and TVM-12 instructions\n\nTVM 11 allows accessing incoming message data without parsing `msg_cell`.\nTVM 12 has `BTOS` (\"builder-to-slice\" without intermediate cell creation).\nCombined, they contribute ~30% of the savings.\nMost of the gain comes from the language itself.\n\n#### 5. Fixing inefficiencies in original FunC code\n\nIn some cases, the FunC versions had suboptimal logic. The Tolk versions improve it — while preserving behavior.\n\n\n## Not just about gas — readability comes first\n\nTolk is built for **readability**. These contracts aren't \"just cleaner\" than their FunC equivalents — they're **elegant**.  \nNo magic. No stack tricks. Just clean, consistent logic — whether it's a Jetton or a Wallet.\n\nTake Jettons as an example. Compare these three files:\n\n- a standard jetton config: [01/jetton_utils.tolk](contracts_Tolk/01_jetton/jetton-utils.tolk)\n- Notcoin — supports masterchain: [03/jetton_utils.tolk](contracts_Tolk/03_notcoin/jetton-utils.tolk)\n- tgBTC — supports sharding: [04/jetton_utils.tolk](contracts_Tolk/04_sharded_tgbtc/jetton-utils.tolk)\n\nThey are **remarkably similar**.  \nStart with a simple Jetton. Want masterchain support? Add a line — and you have Notcoin.  \nWant sharding? Set the desired `SHARD_DEPTH` — and you get a sharded Jetton.  \nMessage sending and address composition are encapsulated cleanly and declaratively.\n\nAnd gas savings? They're a **consequence**.  \nI didn't micro-optimize. Each contract was rewritten in about a day — just focusing on clarity.  \nIf the code is readable, it's probably already efficient.  \nIf the logic is hard to follow — that's where the inefficiency hides.\n\nThe compiler and stdlib will keep improving.  \nBut the core principle remains: **if you write code the way the language encourages — gas will take care of itself.**\n\n\n## Correctness and test coverage\n\nAll Tolk contracts here **pass the same test suites** as their FunC originals.\n\nIn a few cases, tests were **slightly modified** — but only those that assert specific `exit codes`.\n\nThe reason: Tolk fails more gracefully on corrupted input. For example:\n- FunC might crash with `exit code 9` (\"cell underflow\"),\n- while Tolk returns `0xFFFF` (\"invalid opcode\").\n\nSo, I updated a few `expect(exit_code)` values — to match the actual (and now more meaningful) behavior.\n\n\n## How to run and verify\n\n```bash\nnpm run test:all\n```\n\nAll tests are executed on Tolk contracts, using the same inputs as the original FunC versions.\n\nThe `bench-snapshots/` folder contains gas snapshots for each contract at different stages of rewriting.\n\nYou can also follow the Git history to see how each contract evolved — from raw auto-conversion to clean, idiomatic Tolk.\n\n\n## Want to migrate your own contract from FunC to Tolk?\n\nStart with the [FunC-to-Tolk converter](https://github.com/ton-blockchain/convert-func-to-tolk).  \nIt's a syntax-level tool that preserves 1:1 semantics — giving you a working Tolk version in \"FunC-style,\" ready to be gradually modernized.\n\nThen check out the guide [Tolk vs FunC](https://docs.ton.org/v3/documentation/smart-contracts/tolk/tolk-vs-func/in-short).  \nIt focuses on syntax differences — but keep in mind: **Tolk is more than just new syntax**.\nThe language encourages a different mindset — one that puts data structures and types at the center, rather than imperative flow.\nThis philosophy isn't always spelled out in docs — but you'll feel it as you work with the code.\n\nUse the contracts in this repository as a reference — especially the ones you're already familiar with.\n\nFinally, Tolk is supported in blueprint. Run `npm create ton@latest`, and start experimenting!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fton-blockchain%2Ftolk-bench","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fton-blockchain%2Ftolk-bench","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fton-blockchain%2Ftolk-bench/lists"}