You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

optimizer.py 3.2KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class MigrationOptimizer:
  2. """
  3. Power the optimization process, where you provide a list of Operations
  4. and you are returned a list of equal or shorter length - operations
  5. are merged into one if possible.
  6. For example, a CreateModel and an AddField can be optimized into a
  7. new CreateModel, and CreateModel and DeleteModel can be optimized into
  8. nothing.
  9. """
  10. def optimize(self, operations, app_label=None):
  11. """
  12. Main optimization entry point. Pass in a list of Operation instances,
  13. get out a new list of Operation instances.
  14. Unfortunately, due to the scope of the optimization (two combinable
  15. operations might be separated by several hundred others), this can't be
  16. done as a peephole optimization with checks/output implemented on
  17. the Operations themselves; instead, the optimizer looks at each
  18. individual operation and scans forwards in the list to see if there
  19. are any matches, stopping at boundaries - operations which can't
  20. be optimized over (RunSQL, operations on the same field/model, etc.)
  21. The inner loop is run until the starting list is the same as the result
  22. list, and then the result is returned. This means that operation
  23. optimization must be stable and always return an equal or shorter list.
  24. The app_label argument is optional, but if you pass it you'll get more
  25. efficient optimization.
  26. """
  27. # Internal tracking variable for test assertions about # of loops
  28. self._iterations = 0
  29. while True:
  30. result = self.optimize_inner(operations, app_label)
  31. self._iterations += 1
  32. if result == operations:
  33. return result
  34. operations = result
  35. def optimize_inner(self, operations, app_label=None):
  36. """Inner optimization loop."""
  37. new_operations = []
  38. for i, operation in enumerate(operations):
  39. right = True # Should we reduce on the right or on the left.
  40. # Compare it to each operation after it
  41. for j, other in enumerate(operations[i + 1:]):
  42. in_between = operations[i + 1:i + j + 1]
  43. result = operation.reduce(other, app_label)
  44. if isinstance(result, list):
  45. if right:
  46. new_operations.extend(in_between)
  47. new_operations.extend(result)
  48. elif all(op.reduce(other, app_label) is True for op in in_between):
  49. # Perform a left reduction if all of the in-between
  50. # operations can optimize through other.
  51. new_operations.extend(result)
  52. new_operations.extend(in_between)
  53. else:
  54. # Otherwise keep trying.
  55. new_operations.append(operation)
  56. break
  57. new_operations.extend(operations[i + j + 2:])
  58. return new_operations
  59. elif not result:
  60. # Can't perform a right reduction.
  61. right = False
  62. else:
  63. new_operations.append(operation)
  64. return new_operations