essential-contributions / pint

Pint, the constraint-based programming language for declarative blockchains
Apache License 2.0
17 stars 3 forks source link

Optimize `constraints` by removing all if one evaluates to false #853

Closed lethalgem closed 1 month ago

lethalgem commented 1 month ago

If any constraints evaluate to false just remove every constraint there is and add a constraint false. You know the program is going to fail every single time and every other constraint is useless. Effectively this is just merging all the constraints into the simplest form.

  1. evaluate the constraints
  2. the moment you find a false
  3. exit and clear out the constraints vector
  4. add a new constraint equal to false (look at how to add a expr of false, there are examples)
  5. give it an empty span
otrho commented 1 month ago

And emit a huge warning to the user saying 'this predicate cannot be satisfied' or something..? With the constraint in question ideally. Removing all the other constraints is probably overkill.

mohammadfawaz commented 1 month ago

Removing all the other constraints is probably overkill.

Why is that? I realize that no one should deploy a contract that contains predicates that always fail so it really doesn't matter. However, and at the risk of being pedantic here, there is not sense in generating unnecessary bytecode for any successful compile.

otrho commented 1 month ago

OK, sure, since this is an optimisation then if it's on it should minimise the contract. Fair enough. So as per the other optimisation discussion if we have a (hidden or not) flag to disable optimisations then everything should remain.

It just felt a bit weird to 'punish' the good constraints for being associated with a bad one, but I guess it really is just DCE. It's like having a return at the start of main() -- there's no point in emitting any other functions in the program. :slightly_smiling_face: