From 55c73b20ca8ef3a714d1fcc0605284357d237442 Mon Sep 17 00:00:00 2001 From: lkassianik Date: Wed, 3 Jun 2015 13:01:26 -0700 Subject: [PATCH] Attempt to optimize ghost generating code. Summary: Ref T8394, Attempt to optimize ghost generating code. Test Plan: Open Upcoming, cancelled only, list view query. Should not hang. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley Maniphest Tasks: T8394 Differential Revision: https://secure.phabricator.com/D13141 --- .../query/PhabricatorCalendarEventQuery.php | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/applications/calendar/query/PhabricatorCalendarEventQuery.php b/src/applications/calendar/query/PhabricatorCalendarEventQuery.php index 0369dd85aa..8b4e8837b1 100644 --- a/src/applications/calendar/query/PhabricatorCalendarEventQuery.php +++ b/src/applications/calendar/query/PhabricatorCalendarEventQuery.php @@ -110,8 +110,7 @@ final class PhabricatorCalendarEventQuery return $events; } - $map = array(); - $instance_sequence_pairs = array(); + $enforced_end = null; foreach ($events as $key => $event) { $sequence_start = 0; @@ -159,6 +158,12 @@ final class PhabricatorCalendarEventQuery $end = $event->getRecurrenceEndDate(); } else if ($this->rangeEnd) { $end = $this->rangeEnd; + } else if ($enforced_end) { + if ($end) { + $end = min($end, $enforced_end); + } else { + $end = $enforced_end; + } } if ($end) { @@ -167,6 +172,9 @@ final class PhabricatorCalendarEventQuery $sequence_end++; $datetime->modify($modify_key); $date = $datetime->format('U'); + if ($sequence_end > $this->getRawResultLimit() + $sequence_start) { + break; + } } } else { $sequence_end = $this->getRawResultLimit() + $sequence_start; @@ -175,12 +183,25 @@ final class PhabricatorCalendarEventQuery $sequence_start = max(1, $sequence_start); for ($index = $sequence_start; $index < $sequence_end; $index++) { - $instance_sequence_pairs[] = array($event->getPHID(), $index); $events[] = $event->generateNthGhost($index, $viewer); - - $last_key = last_key($events); - $map[$event->getPHID()][$index] = $last_key; } + + if (count($events) >= $this->getRawResultLimit()) { + $events = msort($events, 'getDateFrom'); + $events = array_slice($events, 0, $this->getRawResultLimit(), true); + $enforced_end = last($events)->getDateFrom(); + } + } + } + + $map = array(); + $instance_sequence_pairs = array(); + + foreach ($events as $key => $event) { + if ($event->getIsGhostEvent()) { + $index = $event->getSequenceIndex(); + $instance_sequence_pairs[] = array($event->getPHID(), $index); + $map[$event->getPHID()][$index] = $key; } }