Inner optimization loop.
(self, operations, app_label)
| 38 | operations = result |
| 39 | |
| 40 | def optimize_inner(self, operations, app_label): |
| 41 | """Inner optimization loop.""" |
| 42 | new_operations = [] |
| 43 | for i, operation in enumerate(operations): |
| 44 | right = True # Should we reduce on the right or on the left. |
| 45 | # Compare it to each operation after it |
| 46 | for j, other in enumerate(operations[i + 1 :]): |
| 47 | result = operation.reduce(other, app_label) |
| 48 | if isinstance(result, list): |
| 49 | in_between = operations[i + 1 : i + j + 1] |
| 50 | if right: |
| 51 | new_operations.extend(in_between) |
| 52 | new_operations.extend(result) |
| 53 | elif all(op.reduce(other, app_label) is True for op in in_between): |
| 54 | # Perform a left reduction if all of the in-between |
| 55 | # operations can optimize through other. |
| 56 | new_operations.extend(result) |
| 57 | new_operations.extend(in_between) |
| 58 | else: |
| 59 | # Otherwise keep trying. |
| 60 | new_operations.append(operation) |
| 61 | break |
| 62 | new_operations.extend(operations[i + j + 2 :]) |
| 63 | return new_operations |
| 64 | elif not result: |
| 65 | # Can't perform a right reduction. |
| 66 | right = False |
| 67 | else: |
| 68 | new_operations.append(operation) |
| 69 | return new_operations |