Skip to content

Commit

Permalink
Optimize update:* event logging when only updated_date is changed (
Browse files Browse the repository at this point in the history
…#7559)

Diffing the old and new state of the object is fairly costly, so it
helps to avoid doing that when it isn't necessary.
  • Loading branch information
SpecLad authored Mar 8, 2024
1 parent b5014d1 commit 6cac453
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cvat/apps/events/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.core.exceptions import ObjectDoesNotExist

from cvat.apps.engine.models import (
TimestampedModel,
Project,
Task,
Job,
Expand All @@ -30,7 +31,19 @@
@receiver(pre_save, sender=Issue, dispatch_uid="issue:update_receiver")
@receiver(pre_save, sender=Comment, dispatch_uid="comment:update_receiver")
@receiver(pre_save, sender=Label, dispatch_uid="label:update_receiver")
def resource_update(sender, instance, **kwargs):
def resource_update(sender, *, instance, update_fields, **kwargs):
if (
isinstance(instance, TimestampedModel)
and update_fields and list(update_fields) == ["updated_date"]
):
# This is an optimization for the common case where only the date is bumped
# (see `TimestampedModel.touch`). Since the actual update of the field will
# be performed _after_ this signal is sent (in `DateTimeField.pre_save`),
# and no other fields are updated, there is guaranteed to be no difference
# between the old and current states of the instance. Therefore, no events
# will need be logged, so we can just exit immediately.
return

resource_name = instance.__class__.__name__.lower()

try:
Expand Down

0 comments on commit 6cac453

Please sign in to comment.