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

Cost Tracking Implementation #616

Open
ureeves opened this issue Feb 19, 2025 · 0 comments · May be fixed by #617
Open

Cost Tracking Implementation #616

ureeves opened this issue Feb 19, 2025 · 0 comments · May be fixed by #617

Comments

@ureeves
Copy link
Collaborator

ureeves commented Feb 19, 2025

Cost Tracking

This issue is meant to specify and track the progress of the implementation of the compiler's cost-tracking feature.

To track the costs we've chosen to modify the output of the compiler to emit WebAssembly code that keeps track of costs during execution of the program. We do this by defining a set of integer globals, one for each tracked cost:

cost-runtime
cost-read-count
cost-read-length
cost-write-count
cost-write-length

The compiler then emits code that, during execution, subtracts the appropriate cost for these globals according to the Clarity executed. If any of the globals falls below zero, an unreachable instruction is executed, with an appropriate return code set.
The host will become responsible for setting the globals to the allowed limit, and for handling the error codes on cost overrun.

Below an example of the code that can introduced before each word to track the cost.

global.get $clarity_cost_left
i64.const <pre-computed-cost>
i64.sub
global.set $clarity_cost_left
global.get $clarity_cost_left
i64.const 0
i64.lt_s
(if (then unreachable))
end

originally posted on #344

Progress

Simple Variadic Words

  • Sub
  • Div
  • Add
  • Mul

Simple Words

  • Log2
  • Modulo
  • Power
  • Sqrti
  • BitwiseAnd
  • BitwiseLShift
  • BitwiseNot
  • BitwiseOr
  • BitwiseRShift
  • BitwiseXor
  • Xor
  • BuffToIntBe
  • BuffToIntLe
  • BuffToUintBe
  • BuffToUintLe
  • CmpGeq
  • CmpGreater
  • CmpLeq
  • CmpLess
  • SimpleOr
  • SimpleAnd
  • IntToAscii
  • IntToUtf8
  • StringToInt
  • StringToUint
  • Hash160
  • Keccak256
  • Sha256
  • Sha512
  • Sha512_256
  • Not
  • ToInt
  • ToUint
  • Destruct
  • IsStandard
  • StxBurn
  • StxGetAccount
  • StxGetBalance

Complex Words

  • Let
  • AtBlock
  • GetBlockInfo
  • GetBurnBlockInfo
  • GetStacksBlockInfo
  • GetTenureInfo
  • And
  • Asserts
  • Filter
  • If
  • Match
  • Or
  • Try
  • Unwrap
  • UnwrapErr
  • FromConsensusBuff
  • ToConsensusBuff
  • DefineConstant
  • AsContract
  • ContractCall
  • Begin
  • UnwrapErrPanic
  • UnwrapPanic
  • DefineDataVar
  • GetDataVar
  • SetDataVar
  • DefaultTo
  • ClarityErr
  • ClarityOk
  • ClaritySome
  • IndexOf::Alias
  • IndexOf::Original
  • IsEq
  • DefinePrivateFunction
  • DefinePublicFunction
  • DefineReadonlyFunction
  • MapDefinition
  • MapDelete
  • MapGet
  • MapInsert
  • MapSet
  • ContractOf
  • IsNone
  • IsSome
  • Construct
  • PrincipalOf
  • Print
  • IsErr
  • IsOk
  • Recover
  • Verify
  • Append
  • AsMaxLen
  • Concat
  • ElementAt::Alias
  • ElementAt::Original
  • Fold
  • Len
  • ListCons
  • Map
  • ReplaceAt
  • Slice
  • StxTransfer
  • StxTransferMemo
  • BurnFungibleToken
  • BurnNonFungibleToken
  • DefineFungibleToken
  • DefineNonFungibleToken
  • GetBalanceOfFungibleToken
  • GetOwnerOfNonFungibleToken
  • GetSupplyOfFungibleToken
  • MintFungibleToken
  • MintNonFungibleToken
  • TransferFungibleToken
  • TransferNonFungibleToken
  • DefineTrait
  • ImplTrait
  • UseTrait
  • TupleCons
  • TupleGet
  • TupleMerge
This was referenced Feb 19, 2025
ureeves added a commit that referenced this issue Feb 21, 2025
Code generated for the `concat` word is changed to ensure both its
arguments are traversed *before* copying their contents onto the
resulting sequence.

This avoids a partially copied to return buffer should the code fail
during resolution of the second argument. It will also allow for a
cleaner implementation of cost tracking since we can then avoid copying
on a cost overrun.

See-also: #616
ureeves added a commit that referenced this issue Feb 21, 2025
Code generated for the `concat` word is changed to ensure both its
arguments are traversed *before* copying their contents onto the
resulting sequence.

This avoids a partially copied to return buffer should the code fail
during resolution of the second argument. It will also allow for a
cleaner implementation of cost tracking since we can then also avoid
copying on a cost overrun.

See-also: #616
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant