algorandfoundation / puya

https://algorandfoundation.github.io/puya/
Other
80 stars 11 forks source link

add a python function that can be called but it compiles to inline teal #247

Open Loedn opened 1 month ago

Loedn commented 1 month ago

Problem

I want to achieve this inlining to save space in a logic signature, which has a size limit of 1000 bytes, while keeping the code organized within functions.

Solution

Example

def x(g1: Bytes) -> Bytes:
    return extract(g1, 0, key_size)

@subroutine
def negate_proof(g1: Bytes) -> Bytes:
    negated_rhs_bigUint: BigUInt = negate(g1)
    negated_rhs: Bytes = negated_rhs_bigUint.bytes
    negated_proof = concat(x(g1), negated_rhs)

Relevant link

Discord convo

joe-p commented 1 month ago

In addition to saving program bytecode size, this would also help reduce opcode cost. Subroutine calls (assuming they have variables) require at least a callsub, proto, and retsub. In scenarios where a set of operations is frequently repeated, this can quickly add up.