emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Speed up org-diary
@ 2010-03-28 18:12 Matt Lundin
  2010-03-29 12:20 ` Carsten Dominik
  0 siblings, 1 reply; 3+ messages in thread
From: Matt Lundin @ 2010-03-28 18:12 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/plain, Size: 3082 bytes --]

Hi Carsten,

Thanks to the new variable org-agenda-entry-types, I have moved all my
appointments from emacs diary into org-mode. It is now quite fast to
pull up weekly and monthly calendars with a custom agenda command.

The missing piece of the puzzle is integration with "diary" and
"cal-tex" functions via the org-diary sexp. I have found org-diary to be
excruciatingly slow when called for anything more than a couple of days.
I have the following line in my diary file:

&%%(org-diary :timestamp :sexp)

If I try to view 20 or so upcoming days in the diary by typing C-u 20 d
on a date in the calendar, it can take upwards of 30 seconds to generate
the diary display. This is of little consequence, since I can, after
all, simply use the custom agenda command. But I often want to print out
a nice LaTeX calendar of my appointments with cal-tex-cursor-month. And
that takes upwards of 50 seconds (see attached elp-results file).

Judging from the elp-results, the culprit seems to be
org-prepare-agenda-buffers (46 seconds), which is called 31 times (once
for each day). It seems to me that since org-diary is being called 31
times in quick succession by the same function (diary-sexp-entry), one
should only need to call org-prepare-agenda-buffers once.

The only solution I could see to this problem was to add a test to see
if org-diary had been called less than 1 second ago. Thus, I added the
variable org-diary-last-run-time and a conditional in org-diary that
only runs org-prepare-agenda-buffers if org-diary-last-run-time is less
than 1 second in the past. 

With the patch, it now takes appr. 5 seconds to generate the LaTeX
calendar with cal-tex and org-prepare-agenda-buffers is called only
once. 

--8<---------------cut here---------------start------------->8---
diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 797728b..6179907 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -4086,6 +4086,8 @@ Needed to avoid empty dates which mess up holiday display."
       (apply 'diary-add-to-list args)
     (apply 'add-to-diary-list args)))
 
+(defvar org-diary-last-run-time nil)
+
 ;;;###autoload
 (defun org-diary (&rest args)
   "Return diary information from org-files.
@@ -4123,8 +4125,14 @@ function from a program - use `org-agenda-get-day-entries' instead."
   (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
 		    (list entry)
 		  (org-agenda-files t)))
+	 (time (org-float-time))
 	 file rtn results)
-    (org-prepare-agenda-buffers files)
+    (when (or (not org-diary-last-run-time)
+	      (> (- time
+		    org-diary-last-run-time)
+		 1))
+      (org-prepare-agenda-buffers files))
+    (setq org-diary-last-run-time time)
     ;; If this is called during org-agenda, don't return any entries to
     ;; the calendar.  Org Agenda will list these entries itself.
     (if org-disable-agenda-to-diary (setq files nil))
--8<---------------cut here---------------end--------------->8---

Thanks for looking at this. I'm eager to know if there is a more elegant
way of accomplishing the same thing.

- Matt


[-- Attachment #2: elp-results --]
[-- Type: text/plain, Size: 8888 bytes --]

cal-tex-cursor-month                                          1           50.220853     50.220853
cal-tex-list-diary-entries                                    1           48.950022     48.950022
diary-list-entries                                            1           48.949987     48.949987
diary-list-sexp-entries                                       31          48.934395     1.5785288709
diary-sexp-entry                                              31          48.916153000  1.5779404193
org-diary                                                     31          48.914679     1.5778928709
org-prepare-agenda-buffers                                    31          46.019204000  1.4844904516
org-agenda-get-day-entries                                    868         2.644963      0.0030471923
org-refresh-category-properties                               868         2.3602110000  0.0027191370
org-agenda-get-timestamps                                     868         1.0062780000  0.0011593064
org-agenda-get-sexps                                          868         0.8682250000  0.0010002592
org-end-of-subtree                                            1426        0.7195369999  0.0005045841
org-agenda-get-blocks                                         868         0.5283550000  0.0006087039
org-get-agenda-file-buffer                                    1736        0.4188759999  0.0002412880
org-back-to-heading                                           2826        0.2957579999  0.0001046560
org-diary-sexp-entry                                          620         0.209813      0.0003384080
org-parse-time-string                                         1710        0.1728869999  0.0001011035
org-format-agenda-item                                        71          0.161254      0.0022711830
org-time-string-to-time                                       434         0.1496349999  0.0003447811
org-time-string-to-absolute                                   1276        0.1480890000  0.0001160572
org-find-base-buffer-visiting                                 1736        0.1476030000  8.502...e-05
diary-anniversary                                             465         0.1349429999  0.0002901999
org-before-change-function                                    1302        0.1321850000  0.0001015245
org-get-time-of-day                                           149         0.1294050000  0.0008684899
org-agenda-files                                              31          0.1266870000  0.0040866774
org-closest-date                                              1276        0.1155559999  9.056...e-05
org-at-date-range-p                                           1386        0.1094259999  7.895...e-05
org-finalize-agenda-entries                                   26          0.1068299999  0.0041088461
org-entries-lessp                                             73          0.1007950000  0.0013807534
org-check-agenda-file                                         868         0.0899190000  0.0001035933
org-uniquify                                                  62          0.0846519999  0.0013653548
org-date-to-gregorian                                         2552        0.0658019999  2.578...e-05
org-agenda-skip                                               2246        0.0605190000  2.694...e-05
org-calendar-holiday                                          31          0.041807      0.0013486129
org-trim                                                      2263        0.0414180000  1.830...e-05
org-get-todo-state                                            263         0.0411600000  0.0001565019
cal-tex-insert-days                                           1           0.033083      0.033083
org-on-heading-p                                              403         0.0228089999  5.659...e-05
cal-tex-end-document                                          1           0.01942       0.01942
org-outline-level                                             1522        0.0151009999  9.921...e-06
diary-pull-attrs                                              58          0.0142740000  0.0002461034
org-get-effort                                                71          0.0136870000  0.0001927746
org-entry-get                                                 71          0.012828      0.0001806760
diary-list-entries-2                                          31          0.012181      0.0003929354
cal-tex-arg                                                   135         0.0113300000  8.392...e-05
diary-block                                                   274         0.0108729999  3.968...e-05
cal-tex-latexify-list                                         62          0.0105160000  0.0001696129
org-get-property-block                                        71          0.0099130000  0.0001396197
org-get-tags-at                                               38          0.0093790000  0.0002468157
cal-tex-LaTeXify-string                                       70          0.0089039999  0.0001271999
org-get-priority                                              38          0.0058529999  0.0001540263
org-up-heading-safe                                           67          0.0045239999  6.752...e-05
cal-tex-comment                                               57          0.0044230000  7.759...e-05
diary-make-date                                               1013        0.0035599999  3.514...e-06
cal-tex-insert-preamble                                       1           0.00313       0.00313
org-agenda-highlight-todo                                     71          0.002797      3.939...e-05
cal-tex-cmd                                                   11          0.002558      0.0002325454
org-add-props                                                 199         0.0025159999  1.264...e-05
org-compile-prefix-format                                     31          0.0020719999  6.683...e-05
org-agenda-new-marker                                         109         0.0020570000  1.887...e-05
cal-tex-list-holidays                                         1           0.00142       0.00142
cal-tex-insert-day-names                                      1           0.00133       0.00133
cal-tex-month-name                                            33          0.0011330000  3.433...e-05
org-float-time                                                140         0.0007870000  5.621...e-06
cal-tex-nl                                                    5           0.0007859999  0.0001571999
org-remove-uniherited-tags                                    38          0.0007720000  2.031...e-05
org-downcase-keep-props                                       113         0.0007170000  6.345...e-06
cal-tex-preamble                                              1           0.000525      0.000525
cal-tex-insert-blank-days-at-end                              1           0.000479      0.000479
org-delete-all                                                38          0.000337      8.868...e-06
org-get-category                                              71          0.0003360000  4.732...e-06
cal-tex-insert-month-header                                   1           0.000335      0.000335
org-agenda-fix-displayed-tags                                 71          0.0003129999  4.408...e-06
cal-tex-e-document                                            1           0.00028       0.00028
diary-add-to-list                                             31          0.0002730000  8.806...e-06
cal-tex-vspace                                                1           0.000266      0.000266
cal-tex-b-document                                            1           0.000264      0.000264
cal-tex-hfill                                                 4           0.0002549999  6.374...e-05
cal-tex-insert-blank-days                                     1           0.000237      0.000237
org-set-sorting-strategy                                      31          0.0001240000  4.000...e-06
org-agenda-reset-markers                                      2           9.400...e-05  4.700...e-05
cal-tex-first-blank-p                                         1           8.5e-05       8.5e-05
cal-tex-last-blank-p                                          1           7.1e-05       7.1e-05
org-defkey                                                    2           3.8e-05       1.9e-05
org-get-todo-face                                             1           2.2e-05       2.2e-05
cal-tex-number-weeks                                          1           1.9e-05       1.9e-05
diary-ordinal-suffix                                          3           1.300...e-05  4.333...e-06
org-key                                                       2           6e-06         3e-06
org-face-from-face-or-color                                   1           4e-06         4e-06

[-- Attachment #3: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Speed up org-diary
  2010-03-28 18:12 [PATCH] Speed up org-diary Matt Lundin
@ 2010-03-29 12:20 ` Carsten Dominik
  2010-03-29 14:17   ` Matthew Lundin
  0 siblings, 1 reply; 3+ messages in thread
From: Carsten Dominik @ 2010-03-29 12:20 UTC (permalink / raw)
  To: Matt Lundin; +Cc: Org Mode

Hi Matt,

that is a good catch.  org-diary is a total orphan for me,
I have not looked at this function for 5 years - it was written
when I was still thinking to use Org-files through the Emacs diary.

I have made one change - I have given it 3 seconds instead of 1,
I think this is a better value.

- Carsten

On Mar 28, 2010, at 8:12 PM, Matt Lundin wrote:

> Hi Carsten,
>
> Thanks to the new variable org-agenda-entry-types, I have moved all my
> appointments from emacs diary into org-mode. It is now quite fast to
> pull up weekly and monthly calendars with a custom agenda command.
>
> The missing piece of the puzzle is integration with "diary" and
> "cal-tex" functions via the org-diary sexp. I have found org-diary  
> to be
> excruciatingly slow when called for anything more than a couple of  
> days.
> I have the following line in my diary file:
>
> &%%(org-diary :timestamp :sexp)
>
> If I try to view 20 or so upcoming days in the diary by typing C-u  
> 20 d
> on a date in the calendar, it can take upwards of 30 seconds to  
> generate
> the diary display. This is of little consequence, since I can, after
> all, simply use the custom agenda command. But I often want to print  
> out
> a nice LaTeX calendar of my appointments with cal-tex-cursor-month.  
> And
> that takes upwards of 50 seconds (see attached elp-results file).
>
> Judging from the elp-results, the culprit seems to be
> org-prepare-agenda-buffers (46 seconds), which is called 31 times  
> (once
> for each day). It seems to me that since org-diary is being called 31
> times in quick succession by the same function (diary-sexp-entry), one
> should only need to call org-prepare-agenda-buffers once.
>
> The only solution I could see to this problem was to add a test to see
> if org-diary had been called less than 1 second ago. Thus, I added the
> variable org-diary-last-run-time and a conditional in org-diary that
> only runs org-prepare-agenda-buffers if org-diary-last-run-time is  
> less
> than 1 second in the past.
>
> With the patch, it now takes appr. 5 seconds to generate the LaTeX
> calendar with cal-tex and org-prepare-agenda-buffers is called only
> once.
>
> --8<---------------cut here---------------start------------->8---
> diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
> index 797728b..6179907 100644
> --- a/lisp/org-agenda.el
> +++ b/lisp/org-agenda.el
> @@ -4086,6 +4086,8 @@ Needed to avoid empty dates which mess up  
> holiday display."
>       (apply 'diary-add-to-list args)
>     (apply 'add-to-diary-list args)))
>
> +(defvar org-diary-last-run-time nil)
> +
> ;;;###autoload
> (defun org-diary (&rest args)
>   "Return diary information from org-files.
> @@ -4123,8 +4125,14 @@ function from a program - use `org-agenda-get- 
> day-entries' instead."
>   (let* ((files (if (and entry (stringp entry) (string-match "\\S-"  
> entry))
> 		    (list entry)
> 		  (org-agenda-files t)))
> +	 (time (org-float-time))
> 	 file rtn results)
> -    (org-prepare-agenda-buffers files)
> +    (when (or (not org-diary-last-run-time)
> +	      (> (- time
> +		    org-diary-last-run-time)
> +		 1))
> +      (org-prepare-agenda-buffers files))
> +    (setq org-diary-last-run-time time)
>     ;; If this is called during org-agenda, don't return any entries  
> to
>     ;; the calendar.  Org Agenda will list these entries itself.
>     (if org-disable-agenda-to-diary (setq files nil))
> --8<---------------cut here---------------end--------------->8---
>
> Thanks for looking at this. I'm eager to know if there is a more  
> elegant
> way of accomplishing the same thing.
>
> - Matt
>
> cal-tex-cursor-month                                           
> 1           50.220853     50.220853
> cal-tex-list-diary-entries                                     
> 1           48.950022     48.950022
> diary-list-entries                                             
> 1           48.949987     48.949987
> diary-list-sexp-entries                                        
> 31          48.934395     1.5785288709
> diary-sexp-entry                                               
> 31          48.916153000  1.5779404193
> org-diary                                                      
> 31          48.914679     1.5778928709
> org-prepare-agenda-buffers                                     
> 31          46.019204000  1.4844904516
> org-agenda-get-day-entries                                     
> 868         2.644963      0.0030471923
> org-refresh-category-properties                                
> 868         2.3602110000  0.0027191370
> org-agenda-get-timestamps                                      
> 868         1.0062780000  0.0011593064
> org-agenda-get-sexps                                           
> 868         0.8682250000  0.0010002592
> org-end-of-subtree                                             
> 1426        0.7195369999  0.0005045841
> org-agenda-get-blocks                                          
> 868         0.5283550000  0.0006087039
> org-get-agenda-file-buffer                                     
> 1736        0.4188759999  0.0002412880
> org-back-to-heading                                            
> 2826        0.2957579999  0.0001046560
> org-diary-sexp-entry                                           
> 620         0.209813      0.0003384080
> org-parse-time-string                                          
> 1710        0.1728869999  0.0001011035
> org-format-agenda-item                                         
> 71          0.161254      0.0022711830
> org-time-string-to-time                                        
> 434         0.1496349999  0.0003447811
> org-time-string-to-absolute                                    
> 1276        0.1480890000  0.0001160572
> org-find-base-buffer-visiting                                  
> 1736        0.1476030000  8.502...e-05
> diary-anniversary                                              
> 465         0.1349429999  0.0002901999
> org-before-change-function                                     
> 1302        0.1321850000  0.0001015245
> org-get-time-of-day                                            
> 149         0.1294050000  0.0008684899
> org-agenda-files                                               
> 31          0.1266870000  0.0040866774
> org-closest-date                                               
> 1276        0.1155559999  9.056...e-05
> org-at-date-range-p                                            
> 1386        0.1094259999  7.895...e-05
> org-finalize-agenda-entries                                    
> 26          0.1068299999  0.0041088461
> org-entries-lessp                                              
> 73          0.1007950000  0.0013807534
> org-check-agenda-file                                          
> 868         0.0899190000  0.0001035933
> org-uniquify                                                   
> 62          0.0846519999  0.0013653548
> org-date-to-gregorian                                          
> 2552        0.0658019999  2.578...e-05
> org-agenda-skip                                                
> 2246        0.0605190000  2.694...e-05
> org-calendar-holiday                                           
> 31          0.041807      0.0013486129
> org-trim                                                       
> 2263        0.0414180000  1.830...e-05
> org-get-todo-state                                             
> 263         0.0411600000  0.0001565019
> cal-tex-insert-days                                            
> 1           0.033083      0.033083
> org-on-heading-p                                               
> 403         0.0228089999  5.659...e-05
> cal-tex-end-document                                           
> 1           0.01942       0.01942
> org-outline-level                                              
> 1522        0.0151009999  9.921...e-06
> diary-pull-attrs                                               
> 58          0.0142740000  0.0002461034
> org-get-effort                                                 
> 71          0.0136870000  0.0001927746
> org-entry-get                                                  
> 71          0.012828      0.0001806760
> diary-list-entries-2                                           
> 31          0.012181      0.0003929354
> cal-tex-arg                                                    
> 135         0.0113300000  8.392...e-05
> diary-block                                                    
> 274         0.0108729999  3.968...e-05
> cal-tex-latexify-list                                          
> 62          0.0105160000  0.0001696129
> org-get-property-block                                         
> 71          0.0099130000  0.0001396197
> org-get-tags-at                                                
> 38          0.0093790000  0.0002468157
> cal-tex-LaTeXify-string                                        
> 70          0.0089039999  0.0001271999
> org-get-priority                                               
> 38          0.0058529999  0.0001540263
> org-up-heading-safe                                            
> 67          0.0045239999  6.752...e-05
> cal-tex-comment                                                
> 57          0.0044230000  7.759...e-05
> diary-make-date                                                
> 1013        0.0035599999  3.514...e-06
> cal-tex-insert-preamble                                        
> 1           0.00313       0.00313
> org-agenda-highlight-todo                                      
> 71          0.002797      3.939...e-05
> cal-tex-cmd                                                    
> 11          0.002558      0.0002325454
> org-add-props                                                  
> 199         0.0025159999  1.264...e-05
> org-compile-prefix-format                                      
> 31          0.0020719999  6.683...e-05
> org-agenda-new-marker                                          
> 109         0.0020570000  1.887...e-05
> cal-tex-list-holidays                                          
> 1           0.00142       0.00142
> cal-tex-insert-day-names                                       
> 1           0.00133       0.00133
> cal-tex-month-name                                             
> 33          0.0011330000  3.433...e-05
> org-float-time                                                 
> 140         0.0007870000  5.621...e-06
> cal-tex-nl                                                     
> 5           0.0007859999  0.0001571999
> org-remove-uniherited-tags                                     
> 38          0.0007720000  2.031...e-05
> org-downcase-keep-props                                        
> 113         0.0007170000  6.345...e-06
> cal-tex-preamble                                               
> 1           0.000525      0.000525
> cal-tex-insert-blank-days-at-end                               
> 1           0.000479      0.000479
> org-delete-all                                                 
> 38          0.000337      8.868...e-06
> org-get-category                                               
> 71          0.0003360000  4.732...e-06
> cal-tex-insert-month-header                                    
> 1           0.000335      0.000335
> org-agenda-fix-displayed-tags                                  
> 71          0.0003129999  4.408...e-06
> cal-tex-e-document                                             
> 1           0.00028       0.00028
> diary-add-to-list                                              
> 31          0.0002730000  8.806...e-06
> cal-tex-vspace                                                 
> 1           0.000266      0.000266
> cal-tex-b-document                                             
> 1           0.000264      0.000264
> cal-tex-hfill                                                  
> 4           0.0002549999  6.374...e-05
> cal-tex-insert-blank-days                                      
> 1           0.000237      0.000237
> org-set-sorting-strategy                                       
> 31          0.0001240000  4.000...e-06
> org-agenda-reset-markers                                       
> 2           9.400...e-05  4.700...e-05
> cal-tex-first-blank-p                                          
> 1           8.5e-05       8.5e-05
> cal-tex-last-blank-p                                           
> 1           7.1e-05       7.1e-05
> org-defkey                                                     
> 2           3.8e-05       1.9e-05
> org-get-todo-face                                              
> 1           2.2e-05       2.2e-05
> cal-tex-number-weeks                                           
> 1           1.9e-05       1.9e-05
> diary-ordinal-suffix                                           
> 3           1.300...e-05  4.333...e-06
> org-key                                                        
> 2           6e-06         3e-06
> org-face-from-face-or-color                                    
> 1           4e-06         4e-06
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

- Carsten

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Speed up org-diary
  2010-03-29 12:20 ` Carsten Dominik
@ 2010-03-29 14:17   ` Matthew Lundin
  0 siblings, 0 replies; 3+ messages in thread
From: Matthew Lundin @ 2010-03-29 14:17 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: Org Mode

Hi Carsten,

Carsten Dominik <carsten.dominik@gmail.com> writes:

> that is a good catch.  org-diary is a total orphan for me,
> I have not looked at this function for 5 years - it was written
> when I was still thinking to use Org-files through the Emacs diary.

Thanks for applying the patch. Yes, I think one of the FAQs nicely
cautions against using org-diary (i.e., "think twice before using it!").
My only use for it at the moment is to pass org data to the cal-tex
functions. I certainly wouldn't recommend using org-diary to mark
calendar dates!

> I have made one change - I have given it 3 seconds instead of 1,
> I think this is a better value.

Thanks for making this change. I can see that 3 seconds is a better
value, especially since I set the value of org-diary-last-run-time
before the rest of the function runs.

Best,
Matt

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-03-29 14:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-28 18:12 [PATCH] Speed up org-diary Matt Lundin
2010-03-29 12:20 ` Carsten Dominik
2010-03-29 14:17   ` Matthew Lundin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).