Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(stdlib): optimized emptyCell() and emptySlice() #1696

Merged
merged 12 commits into from
Feb 6, 2025
8 changes: 4 additions & 4 deletions src/stdlib/stdlib/std/internal/cells.tact
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ inline extends fun asCell(self: Builder): Cell {
return self.endCell();
}

inline fun emptyCell(): Cell {
return beginCell().endCell();
asm fun emptyCell(): Cell {
<b b> PUSHREF // Pure fift) "<b" creates a builder, "b>" turns it into a cell (in compile time)
Shvandre marked this conversation as resolved.
Show resolved Hide resolved
}

inline fun emptySlice(): Slice {
return emptyCell().asSlice();
asm fun emptySlice(): Slice {
b{} PUSHSLICE
}

/// Struct for holding values computed by the `Cell.computeDataSize()` and `Slice.computeDataSize()` extension functions. Available since Tact 1.6.0.
Expand Down
12 changes: 8 additions & 4 deletions src/test/benchmarks/__snapshots__/benchmarks.spec.ts.snap
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`benchmarks benchmark functions: code size 1`] = `211`;
exports[`benchmarks benchmark cells creation: gas used emptyCell 1`] = `931n`;

exports[`benchmarks benchmark functions: gas used 1`] = `2769n`;
exports[`benchmarks benchmark cells creation: gas used emptySlice 1`] = `1035n`;

exports[`benchmarks benchmark readFwdFee: code size 1`] = `157`;
exports[`benchmarks benchmark functions: code size 1`] = `227`;

exports[`benchmarks benchmark readFwdFee: gas used 1`] = `2799n`;
exports[`benchmarks benchmark functions: gas used 1`] = `2869n`;

exports[`benchmarks benchmark readFwdFee: code size 1`] = `173`;

exports[`benchmarks benchmark readFwdFee: gas used 1`] = `2899n`;
19 changes: 19 additions & 0 deletions src/test/benchmarks/benchmarks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Sha256Big } from "./contracts/output/benchmark_sha256_big_Sha256Big";
import { Sha256AsSlice } from "./contracts/output/benchmark_sha256_as_slice_Sha256AsSlice";
import { Forward } from "./contracts/output/forward_Forward";
import "@ton/test-utils";
import { cellsCreation } from "./contracts/output/cells_cellsCreation";
import { getUsedGas } from "./util";

function measureGas(txs: BlockchainTransaction[]) {
Expand Down Expand Up @@ -164,4 +165,22 @@ describe("benchmarks", () => {
await hashStringAsSLice(sha256AsSlice, "hello world".repeat(10)),
).toEqual(2516n);
});
it("benchmark cells creation", async () => {
const testContract = blockchain.openContract(
await cellsCreation.fromInit(),
);
await testContract.send(
treasure.getSender(),
{ value: toNano(1) },
beginCell().asSlice(),
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
);
const gasUsed1 = (
await blockchain.runGetMethod(testContract.address, "getEmptyCell")
).gasUsed;
expect(gasUsed1).toMatchSnapshot("gas used emptyCell");
const gasUsed2 = (
await blockchain.runGetMethod(testContract.address, "getEmptySlice")
).gasUsed;
expect(gasUsed2).toMatchSnapshot("gas used emptySlice");
});
});
11 changes: 11 additions & 0 deletions src/test/benchmarks/contracts/cells.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract cellsCreation {
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
receive(msg: Slice) {

}
get fun getEmptyCell(): Cell {
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
return emptyCell();
}
get fun getEmptySlice(): Slice {
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
return emptySlice();
}
}
Loading