libsnark icon indicating copy to clipboard operation
libsnark copied to clipboard

Many libsnark::protoboard methods return by value rather than const references

Open HarryR opened this issue 6 years ago • 0 comments

Instigated from: https://github.com/HarryR/ethsnarks/issues/99

Several frequently used methods in libsnark::protoboard return by value rather than by reference, this copies all of the data every time they're called:

  • r1cs_variable_assignment<FieldT> full_variable_assignment() const;
  • r1cs_primary_input<FieldT> primary_input() const;
  • r1cs_auxiliary_input<FieldT> auxiliary_input() const;
  • r1cs_constraint_system<FieldT> get_constraint_system() const;

It is unnecessary to perform a full copy of any of this data, and with very large constraint systems there's a significant overhead introduced by the copy constructor.

I will verify whether these can be changed to:

  • const r1cs_variable_assignment<FieldT>& full_variable_assignment() const;
  • const r1cs_primary_input<FieldT>& primary_input() const;
  • const r1cs_auxiliary_input<FieldT>& auxiliary_input() const;
  • const r1cs_constraint_system<FieldT>& get_constraint_system() const;

An alternative would be to change the visibility of properties of libsnark::protoboard from private to public or protected. This is the least-effort solution which requires no changes to dependent code.

I will check which one is the best option, and submit a pull request. 👍

HarryR avatar Dec 05 '18 23:12 HarryR