# Michelson Performance Agenda/Notes
Links:
- [Design of an implementation-independent validation system for Tezos economic protocol](https://hackmd.io/v9zHVliuR3mv_jrOU40SAw?view)
- [TZS](https://gitlab.com/metastatedev/tezos/-/merge_requests/320/diffs#baf5ae9731b9394007fd74e845e331ddbffc1564_0_95)
- [git repo of all originated Michelson scripts](https://gitlab.com/nomadic-labs/smart-contracts/-/tree/rafoo@michelson_scripts)
## 4/28/2021
### Agenda
- Progress operationalizing benchmark suite!
- Reference machines - is Raspberry Pi necessary?
### Notes
## 4/14/2021
### Agenda
- Benjamin found a way to reduce cost of logging by moving logging to a continuation.
### Notes
- Code Unloading
- Feasible, but tricky - we need the experts.
- Machinery exists, but it's never been tested.
- You can load/unload code completely; however, you can't unload data yet.
## 3/2/2021
### Agenda
- [Prototype TZS interpreter](https://gitlab.com/marigold/tezos/-/commit/454da4f5cfb001828d63f7c58d19e85b77917d51)
- Operationalizing a TZS suite:
- Stick to SQL?
- Patch [Nomadic's indexer](https://gitlab.com/nomadic-labs/tezos-indexer)?
- Use Irmin/ocaml-git?
- Directions for a Michelson compiler
### Notes
- Native Compilation
- Eduardo notices that it is difficult to evaluate the gas consumption for native compilation.
- Besides, the back-end of the compiler can produce invalid code or stack overflow.
- We need a guaranteed total order on gas accounting operations.
- This can be guaranteed by telling the compiler "this is a side effect"
- Alternatively, you can give a pointer an external.
- We would need to rewrite register allocation, else there are Michelson programs that would cause (Yann fill in here)
- Something like Linear Scan
- Eduardo believes Lambda is the most reliable target for Michelson.
- Pipeline is Michelson -> Lambda -> CMM -> ASM
- Eduardo thinks he has a trick to do this.
- Every step would need to be benchmarked
- _Or_, perhaps we just charge a based on some weighting of the AST.
- In general, type checking in OCaml is non-linear; however, for Michelson, we just have simple types and no inference.
- Caching strategy for compiled contracts
- Store source code on chain
- Store compiled version off chain in on-disk db
- When starting new nodes, and on compiler upgrades, node admins have to wait for recompilation to finish; however, attackers cannot force new nodes or compiler upgrades.
- What if users generate CMM and originate that instead?
- We would need a safe subset of CMM
- Run contract in a sandbox
- Compiler sandbox is relatively simple
- Contract sandbox is more complicated (see JVM for mobile devices)
- Alternative approach is MetaOCaml
- Typechecking for G
- Yann's idea: lazy elaboration
- Create a carbonated version of Std.Lazy
- Eduardo's idea: store the typed IR + tree shaking.
- But, Yann's encoding of typed IR is just a prototype and he's not sure it would scale.
- Consensus: if our goal is compilation, we should avoid major changes. Yann's idea is small - let's go with that.
- Cursors
- Could improve access time for storage in Michelson interpretation.
### Actions
- [ ] Ask Xavier what he thinks about compiling Michelson to CMM.
- He's not available till April or May.
- [x] Yann will work on a lazy elaboration MR
- [x] Yann will write a MetaOCaml experiment.
- [ ] Benjamin: proposal for improving serialization
- Also, Marigold people are working on this.
- [x] Daniel: write HackMD proposal
## 2/23/2021
### Agenda
- Look at Yann's well-typed serialization of typed scripts https://hackmd.io/O_0TMVU5SCKWoldnJzsNng?view
- News about hash-consing
- Evaluate the current three directions:
- to develop memory dumping infrastructure
- to optimize the existing elaboration (no more annotations, laziness, etc)
- to develop the lazy serialization of typed scripts
- Review draft TZS spec for Dexter/FA12 https://hackmd.io/G---4SQ-RVOAW25UJbXi2w
### Notes
- YRG confirmed type elabaration is roughly 60% of Dexter's benchmark
- Goal is low thousands of TPS
- However, these are TZ1 transactions - interpreter has no relation.
- Interpreter is a bottleneck, but not the only bottleneck.
- E.g., storage
- Multicore is coming to Tezos!
- YRG has an encoding for GADT's that relies on adding dependent sum's
to the data_endcoding lib
- According to YRG, this isn't as scary as it sounds 😅 Indeed, Eduardo has a similar prototype.
- We need to keep in mind that at some point a typed encoding may become overly cumbersome and we may want to switch to a compiled version of Michelson.
### Action Items
- [ ] Create proposal for Michelson -> CMM compiler
- How to handle caching compilation?
- How to handle gas?
- Other concerns?
- [x] TZS Formatting
- Extend Micheline to allow "#" in primitive names
- Create Interpreter of TZS based one existing benchmark DSL.
- (informally) formalize notion of variable scope in the TZS format.