Skip to content

Possible performance optimisation: replacing multiple pointer dereferences with temporary variable storing dereferenced pointer #5494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
natasha-jeppu opened this issue Sep 16, 2020 · 4 comments
Assignees
Labels
aws Bugs or features of importance to AWS CBMC users work in progress

Comments

@natasha-jeppu
Copy link

CBMC version: 5.12.6 (cbmc-5.12.6-47-ge06f678f2)
Operating system: macOS Catalina 10.15.6
Exact command line resulting in the issue: make result
What behaviour did you expect: Proof runtime below 10mins
What happened instead: Runtime without optimisation = ~ 5hrs, Runtime with optimisation = ~300s

To reproduce these results use the following:
Code base: awslabs/s2n
Proof: s2n_stuffer_write_reservation
Code without optimisation: https://github.com/natasha-jeppu/s2n/blob/without_opt/stuffer/s2n_stuffer.c#L38
Code with optimisation: https://github.com/natasha-jeppu/s2n/blob/with_opt/stuffer/s2n_stuffer.c#L38
Command: make result

Optimisation: Replace multiple dereferences of struct s2n_stuffer_reservation* reservation pointer with a temporary object struct s2n_stuffer_reservation reservation_obj = *reservation and use reservation_obj for subsequent struct member access. Observed runtime decrease from 5hrs to 5mins
It would be convenient if CBMC could do this for similar constructs.

This behaviour can be explained with two profiling metrics (PRs submitted):

Without the optimisation, the reservation pointer is dereferenced 2656 times. Here, dereferences are counted as the number of calls to value_set_dereferencet::dereference(), for a given pointer, that generates the case split expression.

Screenshot 2020-09-15 at 21 47 35

The corresponding solver query complexity metric shows 10798 clauses for https://github.com/natasha-jeppu/s2n/blob/without_opt/stuffer/s2n_stuffer.c#L42 and SSA expr is evidence of the complex case split generated.

Screenshot 2020-09-15 at 22 51 21

With the optimisation, the reservation pointer is dereferenced only once.
Screenshot 2020-09-15 at 21 51 46

The corresponding solver query complexity metric shows a reduction in number of clauses to 3653 for https://github.com/natasha-jeppu/s2n/blob/with_opt/stuffer/s2n_stuffer.c#L49 and the SSA expr is simplified.

Screenshot 2020-09-15 at 22 34 55

@hannes-steffenhagen-diffblue hannes-steffenhagen-diffblue added the aws Bugs or features of importance to AWS CBMC users label Sep 16, 2020
@martin-cs
Copy link
Collaborator

This is very interesting. Thank you. A good find.

I can definitely see why you would get a reduction in clauses and variables. The run-time difference is a bit more of a surprise. Does this still happen if you use different SAT solvers and / or the SMT back-end?

@natasha-jeppu
Copy link
Author

This is very interesting. Thank you. A good find.

I can definitely see why you would get a reduction in clauses and variables. The run-time difference is a bit more of a surprise. Does this still happen if you use different SAT solvers and / or the SMT back-end?

I have tried kissat as the SAT solver on the dimacs cnf output from CBMC.
Without the optimisation, kissat takes 176sec and with the optimisation its 23sec.

@martin-cs
Copy link
Collaborator

Thanks; that rules out it being a MiniSAT performance issue. If it also happens with the SMT back-end it will be very interesting.

@tautschnig
Copy link
Collaborator

Closing as #5964 has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws Bugs or features of importance to AWS CBMC users work in progress
Projects
None yet
Development

No branches or pull requests

6 participants