From 2bf02493385ffe5e51b5480e57f9643bc80b8267 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Tue, 11 Oct 2011 13:06:55 +0200
Subject: Avoid unnecessary start time calculations

Rename recur_*_inday() to recur_*_find_occurrence() and use the new
functions whenever we actually care about the start time of an
occurrence.

Reintroduce recur_*_inday() as wrappers to recur_*_find_occurrence() and
pass NULL as start time buffer (which means "skip start time
calculation"). Keep using these when we only want to know if a recurrent
item belongs to a specific day but do not care about the actual start
time.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/args.c     |  4 +++-
 src/calcurse.h |  6 ++++++
 src/day.c      | 11 +++++++----
 src/notify.c   | 15 ++++++++-------
 src/recur.c    | 57 ++++++++++++++++++++++++++++++++++++++++++++++-----------
 5 files changed, 70 insertions(+), 23 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index 6670ce0..5e50c03 100644
--- a/src/args.c
+++ b/src/args.c
@@ -432,6 +432,7 @@ app_arg (int add_line, struct date *day, long date, int print_note,
     {
       struct apoint *apt = LLIST_TS_GET_DATA (i);
       struct recur_apoint *ra = LLIST_TS_GET_DATA (j);
+      unsigned occurrence;
 
       while (i && regex && regexec (regex, apt->mesg, 0, 0, 0) != 0)
         {
@@ -447,7 +448,8 @@ app_arg (int add_line, struct date *day, long date, int print_note,
 
       if (apt && ra)
         {
-          if (apt->start <= recur_apoint_inday (ra, today))
+          if (recur_apoint_find_occurrence (ra, today, &occurrence) &&
+              apt->start <= occurrence)
             ra = NULL;
           else
             apt = NULL;
diff --git a/src/calcurse.h b/src/calcurse.h
index a3ea45c..fb2be66 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -821,6 +821,12 @@ struct recur_event  *recur_event_scan (FILE *, struct tm, int, char,
 void                 recur_apoint_write (struct recur_apoint *, FILE *);
 void                 recur_event_write (struct recur_event *, FILE *);
 void                 recur_save_data (FILE *);
+unsigned             recur_item_find_occurrence (long, long, llist_t *, int,
+                                                 int, long, long, unsigned *);
+unsigned             recur_apoint_find_occurrence (struct recur_apoint *,
+                                                   long, unsigned *);
+unsigned             recur_event_find_occurrence (struct recur_event *, long,
+                                                  unsigned *);
 unsigned             recur_item_inday (long, long, llist_t *, int, int, long,
                                        long);
 unsigned             recur_apoint_inday(struct recur_apoint *, long);
diff --git a/src/day.c b/src/day.c
index b264887..b75a7bd 100644
--- a/src/day.c
+++ b/src/day.c
@@ -225,10 +225,13 @@ day_store_recur_apoints (long date)
   LLIST_TS_FIND_FOREACH (&recur_alist_p, date, recur_apoint_inday, i)
     {
       struct recur_apoint *rapt = LLIST_TS_GET_DATA (i);
-      int real_start = recur_apoint_inday (rapt, date);
-      (void)day_add_apoint (RECUR_APPT, rapt->mesg, rapt->note, real_start,
-                            rapt->dur, rapt->state, a_nb);
-      a_nb++;
+      unsigned real_start;
+      if (recur_apoint_find_occurrence (rapt, date, &real_start))
+        {
+          (void)day_add_apoint (RECUR_APPT, rapt->mesg, rapt->note, real_start,
+                                rapt->dur, rapt->state, a_nb);
+          a_nb++;
+        }
     }
   LLIST_TS_UNLOCK (&recur_alist_p);
 
diff --git a/src/notify.c b/src/notify.c
index e1a7dab..bf0fd29 100644
--- a/src/notify.c
+++ b/src/notify.c
@@ -507,15 +507,15 @@ notify_check_added (char *mesg, long start, char state)
 void
 notify_check_repeated (struct recur_apoint *i)
 {
-  long real_app_time;
+  unsigned real_app_time;
   int update_notify = 0;
   time_t current_time;
 
   current_time = time (NULL);
   pthread_mutex_lock (&notify_app.mutex);
-  if ((real_app_time = recur_item_inday (i->start, i->dur, &i->exc,
-                                         i->rpt->type, i->rpt->freq,
-                                         i->rpt->until, get_today ())))
+  if (recur_item_find_occurrence (i->start, i->dur, &i->exc, i->rpt->type,
+                                  i->rpt->freq, i->rpt->until, get_today (),
+                                  &real_app_time))
     {
       if (!notify_app.got_app)
         {
@@ -556,10 +556,11 @@ int
 notify_same_recur_item (struct recur_apoint *i)
 {
   int same = 0;
-  long item_start = 0;
+  unsigned item_start = 0;
 
-  item_start = recur_item_inday (i->start, i->dur, &i->exc, i->rpt->type,
-                                 i->rpt->freq, i->rpt->until, get_today ());
+  recur_item_find_occurrence (i->start, i->dur, &i->exc, i->rpt->type,
+                              i->rpt->freq, i->rpt->until, get_today (),
+                              &item_start);
   pthread_mutex_lock (&notify_app.mutex);
   if (notify_app.got_app && item_start == notify_app.time)
     same = 1;
diff --git a/src/recur.c b/src/recur.c
index da4cdf9..4c27d91 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -615,8 +615,8 @@ exc_inday (struct excp *exc, long day_start)
 }
 
 /*
- * Check if the recurrent item belongs to the selected day,
- * and if yes, return the real start time.
+ * Check if the recurrent item belongs to the selected day, and if yes, store
+ * the start date of the occurrence that belongs to the day in a buffer.
  *
  * This function was improved thanks to Tony's patch.
  * Thanks also to youshe for reporting daylight saving time related problems.
@@ -624,8 +624,9 @@ exc_inday (struct excp *exc, long day_start)
  * calculation of recurrent dates after a turn of years.
  */
 unsigned
-recur_item_inday (long item_start, long item_dur, llist_t *item_exc,
-                  int rpt_type, int rpt_freq, long rpt_until, long day_start)
+recur_item_find_occurrence (long item_start, long item_dur, llist_t *item_exc,
+                            int rpt_type, int rpt_freq, long rpt_until,
+                            long day_start, unsigned *occurrence)
 {
   struct date start_date;
   long diff, span;
@@ -693,16 +694,50 @@ recur_item_inday (long item_start, long item_dur, llist_t *item_exc,
 
   if (diff <= span)
     {
-      start_date.dd = lt_item_day.tm_mday;
-      start_date.mm = lt_item_day.tm_mon + 1;
-      start_date.yyyy = lt_item_day.tm_year + 1900;
+      if (occurrence)
+        {
+          start_date.dd = lt_item_day.tm_mday;
+          start_date.mm = lt_item_day.tm_mon + 1;
+          start_date.yyyy = lt_item_day.tm_year + 1900;
+
+          *occurrence = date2sec (start_date, lt_item.tm_hour, lt_item.tm_min);
+        }
 
-      return date2sec (start_date, lt_item.tm_hour, lt_item.tm_min);
+      return 1;
     }
   else
     return 0;
 }
 
+unsigned
+recur_apoint_find_occurrence (struct recur_apoint *rapt, long day_start,
+                              unsigned *occurrence)
+{
+  return recur_item_find_occurrence (rapt->start, rapt->dur, &rapt->exc,
+                                     rapt->rpt->type, rapt->rpt->freq,
+                                     rapt->rpt->until, day_start, occurrence);
+}
+
+unsigned
+recur_event_find_occurrence (struct recur_event *rev, long day_start,
+                             unsigned *occurrence)
+{
+  return recur_item_find_occurrence (rev->day, DAYINSEC, &rev->exc,
+                                     rev->rpt->type, rev->rpt->freq,
+                                     rev->rpt->until, day_start, occurrence);
+}
+
+/* Check if a recurrent item belongs to the selected day. */
+unsigned
+recur_item_inday (long item_start, long item_dur, llist_t *item_exc,
+                  int rpt_type, int rpt_freq, long rpt_until, long day_start)
+{
+  /* We do not need the (real) start time of the occurrence here, so just
+   * ignore the buffer. */
+  return recur_item_find_occurrence (item_start, item_dur, item_exc, rpt_type,
+                                     rpt_freq, rpt_until, day_start, NULL);
+}
+
 unsigned
 recur_apoint_inday(struct recur_apoint *rapt, long day_start)
 {
@@ -1011,15 +1046,15 @@ struct notify_app *
 recur_apoint_check_next (struct notify_app *app, long start, long day)
 {
   llist_item_t *i;
-  long real_recur_start_time;
+  unsigned real_recur_start_time;
 
   LLIST_TS_LOCK (&recur_alist_p);
   LLIST_TS_FIND_FOREACH (&recur_alist_p, app->time, recur_apoint_starts_before, i)
     {
       struct recur_apoint *rapt = LLIST_TS_GET_DATA (i);
 
-      real_recur_start_time = recur_apoint_inday(rapt, day);
-      if (real_recur_start_time > start)
+      if (recur_apoint_find_occurrence (rapt, day, &real_recur_start_time) &&
+          real_recur_start_time > start)
         {
           app->time = real_recur_start_time;
           app->txt = mem_strdup (rapt->mesg);
-- 
cgit v1.2.3-70-g09d2