emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [ANN] faster org-table-to-lisp
@ 2020-04-30  6:34 tbanelwebmin
  2020-04-30  8:09 ` Nicolas Goaziou
  0 siblings, 1 reply; 12+ messages in thread
From: tbanelwebmin @ 2020-04-30  6:34 UTC (permalink / raw)
  To: emacs-orgmode

Hi The List.

Here is an alternative, faster version of org-table-to-lisp. It can be
more than 100 times faster.

#+BEGIN_SRC elisp
(defun org-table-to-lisp-faster (&optional org-table-at-p-done)
  "Convert the table at point to a Lisp structure.
The structure will be a list.  Each item is either the symbol `hline'
for a horizontal separator line, or a list of field values as strings.
The table is taken from the buffer at point.
When the optional ORG-TABLE-AT-P-DONE parameter is not nil, it is
assumed that (org-at-table-p) was already called."
  (or org-table-at-p-done (org-at-table-p) (user-error "No table at point"))
  (save-excursion
    (goto-char (org-table-begin))
    (let ((end (org-table-end))
          (row)
          (table))
      (while (< (point) end)
        (setq row nil)
        (search-forward "|" end)
        (if (looking-at "-")
            (progn
              (search-forward "\n" end)
              (push 'hline table))
          (while (not (search-forward-regexp "\\=\n" end t))
            (unless (search-forward-regexp "\\=\\s-*\\([^|]*\\)" end t)
              (user-error "Malformed table at char %s" (point)))
            (let ((b (match-beginning 1))
          (e (match-end       1)))
              (and (search-backward-regexp "[^ \t]" b t)
               (forward-char 1))
              (push
           (buffer-substring-no-properties b (point))
           row)
          (goto-char (1+ e))))
          (push (nreverse row) table)))
      (nreverse table))))
#+END_SRC

Bellow is an example of a large table borrowed from the Datamash
software. On my PC, the reproducible benches show:
- Traditional org-table-to-lisp: 130 seconds
- Alternative org-table-to-lisp: 0.8 seconds (not compiled)

It is faster because it operates directly on the buffer with
(search-forward-regexp). Whereas the standard function splits a string
extracted from the buffer.

This function is a drop-in replacement for the standard one. It can
benefit to Babel and Gnuplot.

Would it make sense to upgrade Org Mode code base?


Beware! The optional parameter has a slightly different meaning for both
functions:
- for the traditional function, it is a string representing an Org table
- for the alternative function, it is a Boolean telling whether
(org-table-at-p) has been called or not

This difference makes no difference for the use cases in the code base.
The function is always called without a parameter, or as:

#+BEGIN_SRC elisp
(org-table-to-lisp
  (buffer-substring-no-properties
    (org-table-begin)
    (org-table-end)))
#+END_SRC



Here is the reproducible bench. It is a self-contained, Org Mode file to
be opened in Emacs.
wget http://tbanelwebmin.free.fr/OrgMode/bench-org-table-to-lisp.org.gz



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

end of thread, other threads:[~2020-05-02  9:39 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-30  6:34 [ANN] faster org-table-to-lisp tbanelwebmin
2020-04-30  8:09 ` Nicolas Goaziou
2020-04-30 20:28   ` tbanelwebmin
2020-04-30 20:47     ` Daniele Nicolodi
2020-04-30 21:01       ` tbanelwebmin
2020-04-30 22:35     ` Nicolas Goaziou
2020-05-01  6:35       ` tbanelwebmin
2020-05-01 10:15         ` Nicolas Goaziou
2020-05-01 12:41           ` tbanelwebmin
2020-05-01 13:11             ` Nicolas Goaziou
2020-05-02  7:41               ` tbanelwebmin
2020-05-02  9:35                 ` Nicolas Goaziou

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).