Skip to content

Commit dd3f687

Browse files
authored
manual: add gas-calculation page (#322)
Fixes #290
1 parent 92bf0f6 commit dd3f687

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This language server/extension provides support for the [Tact programming langua
1313
- On-the-fly inspections with quick fixes
1414
- Signature help inside calls, `initOf` and struct initialization
1515
- [Lenses] with implementation/reference counts
16+
- [Gas estimates] for assembly functions
1617
- Building projects with Blueprint and Tact Template
1718
- Integration with Tact compiler and Misti static analyzer
1819

@@ -24,6 +25,7 @@ This language server/extension provides support for the [Tact programming langua
2425
[definition]: https://github.com/tact-lang/tact-language-server/blob/master/docs/manual/features/navigation.md#go-to-definition
2526
[type definition]: https://github.com/tact-lang/tact-language-server/blob/master/docs/manual/features/navigation.md#go-to-type-definition
2627
[Lenses]: https://github.com/tact-lang/tact-language-server/blob/master/docs/manual/features/code-lenses.md
28+
[Gas estimates]: https://github.com/tact-lang/tact-language-server/blob/master/docs/manual/features/gas-calculation.md
2729

2830
## Quick start
2931

159 KB
Loading
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Gas Calculation
2+
3+
The Language Server provides gas consumption analysis for assembly functions.
4+
5+
Gas consumption information is displayed in two ways:
6+
7+
1. In the documentation when hovering over a function
8+
2. As inline hint after the opening bracket of the function body
9+
10+
![Gas calculation example](../assets/gas-calculation.png)
11+
12+
## Accuracy
13+
14+
The accuracy of gas calculations depends on the function complexity:
15+
16+
- **Simple functions**: Calculations are quite accurate
17+
- **Complex functions** (with branches and loops): Calculations are approximate
18+
19+
## Calculation Rules
20+
21+
The Language Server uses the following rules to estimate gas consumption:
22+
23+
### Branching (if-else)
24+
25+
- Take the branch with the highest gas consumption
26+
- This provides a "worst-case scenario" estimation
27+
28+
### Loops
29+
30+
- Applies to `while`, `until`, and `repeat` statements
31+
- Gas consumption is calculated as: `loop_body_gas * coefficient`
32+
- Default coefficient is 5
33+
- You can adjust this in VS Code settings:
34+
- Open **Settings**
35+
- Search for **"Tact: Editor › Gas Calculation"**
36+
- Modify **"Loop Gas Coefficient"**
37+
38+
### Unknown Instructions
39+
40+
- Instructions without known gas costs are counted as zero gas
41+
- This might lead to underestimation in some cases
42+
43+
## Tips
44+
45+
- Use these calculations as guidelines rather than exact values
46+
47+
## Disabling Gas Hints
48+
49+
If you don't need gas calculations, you can disable them:
50+
51+
1. Open VS Code **Settings**
52+
2. Search for **"Tact: Editor › Gas Hints"**
53+
3. Disable the relevant options:
54+
- **"Show Gas Consumption"**: For asm functions
55+
- **"Show Asm Instruction Gas"**: For individual instructions
56+
- **"Show Continuation Gas"**: For continuation blocks

server/src/inlays/collect.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {Node as SyntaxNode} from "web-tree-sitter"
1818
import {computeSeqGasConsumption, instructionPresentation} from "@server/asm/gas"
1919
import * as compiler from "@server/compiler/utils"
2020
import {FileDiff} from "@server/utils/FileDiff"
21-
import {InlayHintLabelPart, MarkupKind} from "vscode-languageserver"
21+
import {InlayHintLabelPart, MarkupContent, MarkupKind} from "vscode-languageserver"
2222
import {Location} from "vscode-languageclient"
2323
import {asLspRange} from "@server/utils/position"
2424
import {URI} from "vscode-uri"
@@ -124,6 +124,11 @@ export function collect(
124124

125125
const result: InlayHint[] = []
126126

127+
const gasHintTooltip: MarkupContent = {
128+
kind: "markdown",
129+
value: "Note that this value is approximate!\n\nLearn more about how LS calculates this: https://github.com/tact-lang/tact-language-server/blob/master/docs/manual/features/gas-calculation.md",
130+
}
131+
127132
RecursiveVisitor.visit(file.rootNode, (n): boolean => {
128133
const type = n.type
129134

@@ -352,6 +357,7 @@ export function collect(
352357
line: openBrace.endPosition.row,
353358
character: openBrace.endPosition.column,
354359
},
360+
tooltip: gasHintTooltip,
355361
paddingLeft: true,
356362
})
357363
}
@@ -400,6 +406,7 @@ export function collect(
400406
line: openBrace.endPosition.row,
401407
character: openBrace.endPosition.column,
402408
},
409+
tooltip: gasHintTooltip,
403410
})
404411
return true
405412
}

0 commit comments

Comments
 (0)