hard to diagnose as most of it is in variables. But i think you are have your answer right there.
If clauses are much slower than set analysis. i dont know if your logic can be convereted. but a lot it could be theoritically be done in set analysis . alternatively create flags while loading data and use that in set analysis too