anoma / juvix

A language for intent-centric and declarative decentralised applications
https://docs.juvix.org
GNU General Public License v3.0
454 stars 53 forks source link

Cairo: Use the `Alloc` hint for memory allocation #2721

Open lukaszcz opened 7 months ago

lukaszcz commented 7 months ago

Use the Alloc hint instead of calling juvix_get_regs to fetch ap. This would save the call/ret instructions and four words of memory on each allocation.

lukaszcz commented 4 months ago

In Cairo Assembly we can "assign" through a pointer dereference only a memory-stored value, e.g., [[fp + 4]] = [ap - 1]. Because the Alloc hint returns a pointer stored at [ap] instead of just using ap as the pointer, assigning the tag and every constant number and pointer dereferencing argument requires using up a memory word and an execution step to transfer it to [ap] first.

Hence, it makes sense to use Alloc only when no more than 3 arguments are constants or constructor field references.

This also depends on the relative "cost" of executing different instructions (calls and rets versus assignments).