From 36100d3e45365a692601f6d6594557c458317e1b Mon Sep 17 00:00:00 2001 From: skywardboundd Date: Wed, 28 May 2025 14:57:23 +0300 Subject: [PATCH 1/3] refactor(benches-nft): use structs as possible --- src/benchmarks/nft/gas.json | 10 ++++ src/benchmarks/nft/size.json | 10 ++++ src/benchmarks/nft/tact/collection.tact | 31 +++++++---- src/benchmarks/nft/tact/item.tact | 73 +++++++++++-------------- src/benchmarks/nft/tact/messages.tact | 16 ++++++ 5 files changed, 87 insertions(+), 53 deletions(-) diff --git a/src/benchmarks/nft/gas.json b/src/benchmarks/nft/gas.json index f079f2c6bf..2d5807b3e3 100644 --- a/src/benchmarks/nft/gas.json +++ b/src/benchmarks/nft/gas.json @@ -189,6 +189,16 @@ "deploy nft": "10335", "batch deploy nft": "654266" } + }, + { + "label": "1.6.12 with using structs as possible", + "pr": "https://github.com/tact-lang/tact/pull/3299", + "gas": { + "transfer": "6839", + "get static data": "4336", + "deploy nft": "10335", + "batch deploy nft": "654266" + } } ] } diff --git a/src/benchmarks/nft/size.json b/src/benchmarks/nft/size.json index 7a19fcca05..1e75084ee7 100644 --- a/src/benchmarks/nft/size.json +++ b/src/benchmarks/nft/size.json @@ -199,6 +199,16 @@ "item cells": "10", "item bits": "5217" } + }, + { + "label": "1.6.12 with using structs as possible", + "pr": "https://github.com/tact-lang/tact/pull/3299", + "size": { + "collection cells": "39", + "collection bits": "19204", + "item cells": "10", + "item bits": "5017" + } } ] } diff --git a/src/benchmarks/nft/tact/collection.tact b/src/benchmarks/nft/tact/collection.tact index 57ef972d79..75441ffaf3 100644 --- a/src/benchmarks/nft/tact/collection.tact +++ b/src/benchmarks/nft/tact/collection.tact @@ -2,6 +2,17 @@ import "./constants"; import "./messages"; import "./item"; +struct Content { + collectionContent: Cell; + nftContent: Cell; +} + +struct NFTContent { + tag: Int as uint8; + commonContent: Slice; + individualNFTContent: Cell; +} + contract NFTCollection { owner: Address; nextItemIndex: Int as uint64; // IndexSizeBits = 64 @@ -80,10 +91,9 @@ contract NFTCollection { } get fun get_collection_data(): CollectionData { - let cs: Slice = self.content.beginParse(); return CollectionData { nextItemIndex: self.nextItemIndex, - collectionContent: cs.loadRef(), + collectionContent: Content.fromCell(self.content).collectionContent, owner: self.owner, }; } @@ -97,15 +107,14 @@ contract NFTCollection { return self.royaltyParams; } - get fun get_nft_content(index: Int, individualNFTContent: Cell): Cell { - let cs = self.content.beginParse(); - cs.skipRef(); - let commonContent = cs.loadRef().beginParse(); - return beginCell() - .storeUint(1, 8) // off-chain tag - .storeSlice(commonContent) - .storeRef(individualNFTContent) - .endCell(); + get fun get_nft_content(index: Int, individualNFTContent: Cell): NFTContent { + let commonContent = Content.fromCell(self.content).nftContent.asSlice(); + + return NFTContent { + tag: 1, + commonContent, + individualNFTContent, + }; } } diff --git a/src/benchmarks/nft/tact/item.tact b/src/benchmarks/nft/tact/item.tact index 33fc9eab2e..8930490576 100644 --- a/src/benchmarks/nft/tact/item.tact +++ b/src/benchmarks/nft/tact/item.tact @@ -17,16 +17,17 @@ contract NFTItem( receive(msg: GetStaticData) { throwUnless(NotInit, self.owner != null); - sendMsg( - sender(), - 0, - ReportStaticData, - msg.queryId, - beginCell() - .storeUint(self.itemIndex, 256) - .storeAddress(self.collectionAddress), - SendRemainingValue, - ); // implementation detail + message(MessageParameters { + bounce: false, + to: sender(), + value: 0, + body: ReportStaticData { + queryId: msg.queryId, + itemIndex: self.itemIndex, + collectionAddress: self.collectionAddress, + }.toCell(), + mode: SendRemainingValue, + }); } receive(msg: Slice) { @@ -67,28 +68,30 @@ contract NFTItem( throwUnless(InvalidFees, restAmount >= 0); if (msg.forwardAmount > 0) { - sendMsg( - msg.newOwner, - msg.forwardAmount, - OwnershipAssigned, - msg.queryId, - beginCell() - .storeAddress(self.owner!!) - .storeSlice(msg.forwardPayload), - SendPayFwdFeesSeparately, - ); + message(MessageParameters { + bounce: false, + to: msg.newOwner, + value: msg.forwardAmount, + body: OwnershipAssigned { + queryId: msg.queryId, + newOwner: self.owner!!, + forwardPayload: msg.forwardPayload, + }.toCell(), + mode: SendPayFwdFeesSeparately, + }); } if (needResponse) { forceBasechain(msg.responseDestination!!); - sendMsg( - msg.responseDestination!!, - restAmount, - Excesses, - msg.queryId, - beginCell(), - SendPayFwdFeesSeparately, - ); + message(MessageParameters { + bounce: false, + to: msg.responseDestination!!, + value: restAmount, + body: ExcessOut { + queryId: msg.queryId, + }.toCell(), + mode: SendPayFwdFeesSeparately, + }); } self.owner = msg.newOwner; @@ -104,17 +107,3 @@ contract NFTItem( }; } } - -inline fun sendMsg(toAddress: Address, amount: Int, op: Int, queryId: Int, payload: Builder, sendMode: Int) { - message(MessageParameters { - bounce: false, - to: toAddress, - value: amount, - body: beginCell() - .storeUint(op, 32) - .storeUint(queryId, 64) - .storeBuilder(payload) - .endCell(), - mode: sendMode, - }); -} diff --git a/src/benchmarks/nft/tact/messages.tact b/src/benchmarks/nft/tact/messages.tact index eacd7d5b1b..668ea5a50f 100644 --- a/src/benchmarks/nft/tact/messages.tact +++ b/src/benchmarks/nft/tact/messages.tact @@ -73,3 +73,19 @@ message(0xa8cb00ad) ReportRoyaltyParams { queryId: Int as uint64; params: RoyaltyParams; } + +message(0x8b771735) ReportStaticData { + queryId: Int as uint64; + itemIndex: Int as uint256; + collectionAddress: Address; +} + +message(0x05138d91) OwnershipAssigned { + queryId: Int as uint64; + newOwner: Address; + forwardPayload: Slice; +} + +message(0xd53276db) ExcessOut { + queryId: Int as uint64; +} From 247c3ed8ad97aca29745be0ba30ecea5a8e64a94 Mon Sep 17 00:00:00 2001 From: skywardboundd Date: Wed, 28 May 2025 17:13:51 +0300 Subject: [PATCH 2/3] review --- src/benchmarks/nft/gas.json | 2 +- src/benchmarks/nft/size.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/benchmarks/nft/gas.json b/src/benchmarks/nft/gas.json index 2d5807b3e3..5e342aefe8 100644 --- a/src/benchmarks/nft/gas.json +++ b/src/benchmarks/nft/gas.json @@ -191,7 +191,7 @@ } }, { - "label": "1.6.12 with using structs as possible", + "label": "1.6.12 with idiomatic structs handling", "pr": "https://github.com/tact-lang/tact/pull/3299", "gas": { "transfer": "6839", diff --git a/src/benchmarks/nft/size.json b/src/benchmarks/nft/size.json index 1e75084ee7..5dc15d2094 100644 --- a/src/benchmarks/nft/size.json +++ b/src/benchmarks/nft/size.json @@ -201,7 +201,7 @@ } }, { - "label": "1.6.12 with using structs as possible", + "label": "1.6.12 with idiomatic structs handling", "pr": "https://github.com/tact-lang/tact/pull/3299", "size": { "collection cells": "39", From 3ecad62d06f34ac9b4d098a985649c25c7079a6c Mon Sep 17 00:00:00 2001 From: skywardboundd Date: Mon, 11 Aug 2025 13:57:33 +0300 Subject: [PATCH 3/3] typo --- src/benchmarks/nft/tact/item.tact | 2 +- src/benchmarks/nft/tact/messages.tact | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/benchmarks/nft/tact/item.tact b/src/benchmarks/nft/tact/item.tact index 8930490576..61e653da04 100644 --- a/src/benchmarks/nft/tact/item.tact +++ b/src/benchmarks/nft/tact/item.tact @@ -74,7 +74,7 @@ contract NFTItem( value: msg.forwardAmount, body: OwnershipAssigned { queryId: msg.queryId, - newOwner: self.owner!!, + prevOwner: self.owner!!, forwardPayload: msg.forwardPayload, }.toCell(), mode: SendPayFwdFeesSeparately, diff --git a/src/benchmarks/nft/tact/messages.tact b/src/benchmarks/nft/tact/messages.tact index 668ea5a50f..37746478aa 100644 --- a/src/benchmarks/nft/tact/messages.tact +++ b/src/benchmarks/nft/tact/messages.tact @@ -82,7 +82,7 @@ message(0x8b771735) ReportStaticData { message(0x05138d91) OwnershipAssigned { queryId: Int as uint64; - newOwner: Address; + prevOwner: Address; forwardPayload: Slice; }