Project

General

Profile

Actions

Patch #43607

closed

Optimize Issue#recalculate_attributes_for to avoid unnecessary saves and cascading callbacks

Added by Go MAEDA about 1 month ago. Updated 30 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
-
Start date:
Due date:
% Done:

0%

Estimated time:

Description

When a subtask is updated, Issue#recalculate_attributes_for recalculates derived attributes of its parent issue (such as priority, start_date,due_date, and done_ratio) and always saves the parent issue.

In cases where the recalculated values are identical to the existing ones, this still triggers save callbacks and recursive ancestor updates, resulting in unnecessary database queries. This becomes noticeable when the issue has a deep ancestor chain.

The attached patch changes the behavior to save the parent issue only when it has actual changes after recalculation. This preserves current behavior when values change, while avoiding needless callback chains and database queries when they do not.


Files

Actions #1

Updated by Go MAEDA about 1 month ago

  • Subject changed from Save parent issue only when derived attributes actually change to Optimize Issue#recalculate_attributes_for by avoiding unnecessary parent issue saves
  • Target version set to 7.0.0

Setting the target version to 7.0.0.

Actions #2

Updated by Go MAEDA about 1 month ago

I have updated the patch. The updated patch uses has_changes_to_save? instead of changed? for better clarity in the context of save.

Actions #3

Updated by Go MAEDA about 1 month ago

  • Subject changed from Optimize Issue#recalculate_attributes_for by avoiding unnecessary parent issue saves to Optimize Issue#recalculate_attributes_for to avoid unnecessary saves and cascading callbacks
Actions #4

Updated by Go MAEDA about 1 month ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patch in r24223.

Actions #5

Updated by Go MAEDA 30 days ago

  • Status changed from Closed to Reopened

The change made in r24223 must be reverted. Because of this change, the done ratio of parent issues may be calculated incorrectly.

The purpose of this patch was to skip save in the recalculate_attributes_for method when the recalculated values of a parent issue (priority, start date, due date, and done ratio) are the same as the values already stored, and thereby avoid triggering recalculation for higher-level parent issues.

However, in recalculate_attributes_for, the done ratio calculation also depends on estimated hours. Even if the done ratio of an issue itself does not change, a change in estimated hours can still affect the done ratio of higher-level parent issues. Therefore, recalculation must not be skipped.

For example, assume there is an issue named "Root", with two child issues: "Parent" and "Leaf_1". Under "Parent", there are two more issues: "Leaf_2" and "Leaf_3".

Root --+-- Parent --+-- Leaf_2
       |            +-- Leaf_3
       +-- Leaf_1

Assume the following values:

  • Leaf_1: estimated hours = 10, done ratio = 0%
  • Leaf_2: estimated hours = 10, done ratio = 50%
  • Leaf_3: estimated hours = 10, done ratio = 50%

In this case, the done ratio is:

  • Parent: 50%
    (10 * 0.5 + 10 * 0.5) / 20 = 0.5
  • Root: 33%
    (0 * 0.5 + 10 * 0.5 + 10 * 0.5) / 30 = 0.33...

Next, suppose the estimated hours of Leaf_3 are changed from 10 hours to 40 hours. The done ratio of Parent remains 50%, but the done ratio of Root increases to 41% when recalculated with the increased estimated hours.

  • Parent: 50%
    (10 * 0.5 + 40 * 0.5) / 50 = 0.5
  • Root: 41%
    (0 * 0.5 + 10 * 0.5 + 40 * 0.5) / 60 = 0.416...

However, with the change r24223 is applied, recalculation is performed for Parent after modifying Leaf_3, but because the recalculated done ratio of Parent does not change, save is skipped. As a result, the after_save callback is not triggered, recalculation for Root is not performed, and the done ratio of Root incorrectly remains at 33%.

For this reason, r24223 must be reverted.

Actions #6

Updated by Go MAEDA 30 days ago

  • Status changed from Reopened to Closed
  • Target version deleted (7.0.0)

Reverted the change in r24228.

Actions

Also available in: Atom PDF