Skip to content

Commit

Permalink
Add memory costs for 'LOCALCALL'.
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Nov 29, 2017
1 parent f3fe7d9 commit 0ec06ad
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ operations need to take into account the size of the operands.

The memory variation introduced by updating the register *r* with value *v*
```
registerLoadDelta(r, v) = limbLength(v) - registerDataSize(r)
registerLoadDelta(r, v) = limbLength(v) - registerSize(r)
```

### High level view of the gas model
Expand Down Expand Up @@ -376,11 +376,30 @@ We assume that all operations interrogating the local state have complexity
Save the local context (including the current register stack memory
requirements), copy the arguments into the new context and jump to
the call site.

We're assuming that, in an efficient implementation, saving the caller's
registers is a small constant time operation. One way or another, an
efficient implementation will, in the general case, hold the actual register
data in memory, while, maybe, using machine registers for the machine
metadata. Regardless of how these optimizations are done, saving the caller
context and creating the callee context takes
constantTime + someConstant * registerCount, and a similar amount of memory.

```hs
computationCost(LOCALCALL(_, nARGS, _) _ rARGS) =
saveContextCost + wordCopyCost * (sum [registerSize r | r <- rARGS]) + jumpCost
returnAddressSaveCost + byteSetCost * registerMetadataSize * REGISTERS +
wordCopyCost * (sum [registerSize r | r <- rARGS])
+ jumpCost + callStackDepthCheckCost
memoryDelta(LOCALCALL(_, nARGS, _) _ rARGS) =
currentRegisterMemory + sum [registerSize(r) | r <- rARGS]
+ returnAddressSize -- return address
+ registerMetadataSize * REGISTERS -- unused register table
```

`REGISTERS` is the maximum number of registers as declared in the contract
prefix. TODO: Use the caller's actual register count or callee's max register
count instead.

* `RETURN`
Copy values from return registers to caller locations restore local context,
including (the current register stack memory requirements), mark registers'
Expand All @@ -390,7 +409,7 @@ We assume that all operations interrogating the local state have complexity
```hs
computationCost(RETURN(nRETURNS) rVALUES) =
restoreContextCost + metadataCopyCost * nRETURNS + jumpCost
requiredRegisterMemory = callerRegisterMemory +
requiredRegisterMemory(RETURN(nRETURNS) rVALUES) = callerRegisterMemory +
sum [registerLoadDelta(r, v) | (r, v) <- getReturns() `zip` rVALUES]
```

Expand Down

0 comments on commit 0ec06ad

Please sign in to comment.