From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 4GqaOIXee2CvxAAAgWs5BA (envelope-from ) for ; Sun, 18 Apr 2021 09:23:49 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id OIXTMoXee2B/QAAAbx9fmQ (envelope-from ) for ; Sun, 18 Apr 2021 07:23:49 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id D79381EF23 for ; Sun, 18 Apr 2021 09:23:48 +0200 (CEST) Received: from localhost ([::1]:40128 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lY1mT-0004yu-Vj for larch@yhetil.org; Sun, 18 Apr 2021 03:23:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52096) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lY1lt-0004yh-OA for emacs-orgmode@gnu.org; Sun, 18 Apr 2021 03:23:09 -0400 Received: from mout02.posteo.de ([185.67.36.66]:45467) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lY1ll-00009t-1u for emacs-orgmode@gnu.org; Sun, 18 Apr 2021 03:23:09 -0400 Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id CD5672400E5 for ; Sun, 18 Apr 2021 09:22:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.eu; s=2017; t=1618730575; bh=od91/vRhcLOh7n5rk93Xpe1nqB8JLrq+RLQXYu3Z8qs=; h=To:From:Subject:Date:From; b=bAGKemecS3PQIntXIMdKn/aE84juMz3dmfPiigszwlhCyYHsrFoyYUq+0dGhNRGIe hOnXLsII6+46wu8TqbmSweOcMLfbEXrEGolProsMutt9VKNg4prKUex3BsjdT9nvj/ DDxeh8y0JobIaTcOopIQDZfISAiTrxPzrtn43WWNY2GB+93iU/Ln7ccy4tSpjb++IU MM7gzii9Y469az+KkkGyGLrPV1hjWyb0dQFmlS2c82C95hprhhjXDCXz7fB/Loyep9 pMIRhcAT8+OpxEola42ir/UE3pejMpwoQEE1UqHfZ5tEQQcYgYSz+f7fkDXxIa41t3 k7mobFUaTcnww== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4FNLy70tVWz6tmL for ; Sun, 18 Apr 2021 09:22:54 +0200 (CEST) To: emacs-orgmode From: =?UTF-8?Q?S=c3=a9bastien_Miquel?= Subject: [PATCH] ob-tangle.el: Speed up tangling Message-ID: <57480e77-024a-adcc-ec9a-c20b84ac762a@posteo.eu> Date: Sun, 18 Apr 2021 07:22:52 +0000 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------C48047491B7A6FA61F577F4E" Content-Language: fr Received-SPF: pass client-ip=185.67.36.66; envelope-from=sebastien.miquel@posteo.eu; helo=mout02.posteo.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sebastien.miquel@posteo.eu Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1618730629; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=1ftqa8Y4yvLBUMnWvvmlduShrIWcipI0yPQ5+pqs1I4=; b=EuVJ58oOm6zssX4UURkbrLbmMpBoKvBni+CjG4Lvr8reGV9AqqgziEtBEjmlLeerMCFNl6 G29WmjJ6KQrl9Z8hrqkk7b32BMzwR9EIjcc+w6LjXeuXq7FCZdOeJ3KqHbEArk1/eUVdiI eoMGQ3pKO0aeFQ0Bnk8Yq+4v+U/q4AYjoQfI/xKteiGElrMs2koWKJnh9WmvjPWsqncYae cWG9/8N3602YmfIaQMDLoC3jAQyJUA1xlNqBzDvy7Z/7ugg2krzCWxPYHkuB/2218v7qET Lje2SBu2/2QuVw2TshDJtcWMshNT6EkwCHQoSMa7IeCXdM0tZW6eOKkP3JTbAQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1618730629; a=rsa-sha256; cv=none; b=Oq6IhZctK1DVoIvPAL3xTl3UX/WVtNk1a0DSt5Ftx1o0d+ImgtNyzzZ0j0UXez0Zeagebs 3W4TIOb9AGDOgOQ6RAwOnuBmlubRazLiXH9J3FmFVP89WWbhfg8J4Q/e0aqVS8JUo8J4Gw 8DGuf6UIgmJAR0799RYols5JtQdb7xcfaBO73pJhhkLwUVPaDN0VS/dayk7dFYKsdfSZZK k3uM0Tf+WNXlOWm09QvR/0YhmVxtFVE4x16VCVlegAHIJgvfpTO27OMG2OKmVVbxcC0m8P mk4J8zcb2Sw84OxEykoqSnOxfGkr9u/PsDedURL82sOA3dakyvY8vd5oZfrLjw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.eu header.s=2017 header.b=bAGKemec; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -3.14 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.eu header.s=2017 header.b=bAGKemec; dmarc=pass (policy=none) header.from=posteo.eu; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: D79381EF23 X-Spam-Score: -3.14 X-Migadu-Scanner: scn0.migadu.com X-TUID: MOCte3Co/wTl This is a multi-part message in MIME format. --------------C48047491B7A6FA61F577F4E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi, The attached patch modifies the ~org-babel-tangle~ function to avoid a quadratic behavior in the number of blocks tangled to a single file. Tangling an org buffer with 200 blocks to 5 different files yields a 25 % speedup. * lisp/ob-tangle.el (org-babel-tangle-collect-blocks): Group collected blocks by tangled file name. (org-babel-tangle): Avoid quadratic behavior in number of blocks. -- Sébastien Miquel --------------C48047491B7A6FA61F577F4E Content-Type: text/x-patch; charset=UTF-8; name="0001-ob-tangle.el-Speed-up-tangling.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-ob-tangle.el-Speed-up-tangling.patch" >From 939fedb0fa94f044eda6966f55f460aa292e345f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Sat, 17 Apr 2021 21:48:30 +0200 Subject: [PATCH] ob-tangle.el: Speed up tangling ,* lisp/ob-tangle.el (org-babel-tangle-collect-blocks): Group collected blocks by tangled file name. (org-babel-tangle): Avoid quadratic behavior in number of blocks. --- lisp/ob-tangle.el | 148 ++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 4c0c3132d..eef300c3d 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -225,67 +225,54 @@ matching a regular expression." (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'light)))) (user-error "Point is not in a source code block")))) path-collector) - (mapc ;; map over all languages - (lambda (by-lang) - (let* ((lang (car by-lang)) - (specs (cdr by-lang)) - (ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang)) - (lang-f (org-src-get-lang-mode lang)) - she-banged) - (mapc - (lambda (spec) - (let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec)))))) - (let* ((tangle (funcall get-spec :tangle)) - (she-bang (let ((sheb (funcall get-spec :shebang))) - (when (> (length sheb) 0) sheb))) - (tangle-mode (funcall get-spec :tangle-mode)) - (base-name (cond - ((string= "yes" tangle) - (file-name-sans-extension - (nth 1 spec))) - ((string= "no" tangle) nil) - ((> (length tangle) 0) tangle))) - (file-name (when base-name - ;; decide if we want to add ext to base-name - (if (and ext (string= "yes" tangle)) - (concat base-name "." ext) base-name)))) - (when file-name - ;; Possibly create the parent directories for file. - (let ((m (funcall get-spec :mkdirp)) - (fnd (file-name-directory file-name))) - (and m fnd (not (string= m "no")) - (make-directory fnd 'parents))) - ;; delete any old versions of file - (and (file-exists-p file-name) - (not (member file-name (mapcar #'car path-collector))) - (delete-file file-name)) - ;; drop source-block to file - (with-temp-buffer - (when (fboundp lang-f) (ignore-errors (funcall lang-f))) - (when (and she-bang (not (member file-name she-banged))) - (insert (concat she-bang "\n")) - (setq she-banged (cons file-name she-banged))) - (org-babel-spec-to-string spec) - ;; We avoid append-to-file as it does not work with tramp. - (let ((content (buffer-string))) - (with-temp-buffer - (when (file-exists-p file-name) - (insert-file-contents file-name)) - (goto-char (point-max)) - ;; Handle :padlines unless first line in file - (unless (or (string= "no" (cdr (assq :padline (nth 4 spec)))) - (= (point) (point-min))) - (insert "\n")) - (insert content) - (write-region nil nil file-name)))) - ;; if files contain she-bangs, then make the executable + (mapc ;; map over file-names + (lambda (by-fn) + (when-let ((file-name (car by-fn))) + (let ((lspecs (cdr by-fn)) + (fnd (file-name-directory file-name)) + modes make-dir she-banged lang) + ;; delete any old version of file + (when (file-exists-p file-name) (delete-file file-name)) + ;; drop source-blocks to file + ;; We avoid append-to-file as it does not work with tramp. + (with-temp-buffer + (mapc + (lambda (lspec) + (let* ((block-lang (car lspec)) + (spec (cdr lspec)) + (get-spec (lambda (name) (cdr (assq name (nth 4 spec))))) + (she-bang (let ((sheb (funcall get-spec :shebang))) + (when (> (length sheb) 0) sheb))) + (tangle-mode (funcall get-spec :tangle-mode))) + (unless (string-equal block-lang lang) + (setq lang block-lang) + (let ((lang-f (org-src-get-lang-mode lang))) + (when (fboundp lang-f) (ignore-errors (funcall lang-f))))) + ;; if files contain she-bangs, then make them executable (when she-bang (unless tangle-mode (setq tangle-mode #o755))) - ;; update counter - (setq block-counter (+ 1 block-counter)) - (unless (assoc file-name path-collector) - (push (cons file-name tangle-mode) path-collector)))))) - specs))) + (when tangle-mode + (push tangle-mode modes)) + ;; Possibly create the parent directories for file. + (let ((m (funcall get-spec :mkdirp))) + (and m fnd (not (string= m "no")) + (setq make-dir t))) + ;; Handle :padlines unless first line in file + (unless (or (string= "no" (funcall get-spec :padline)) + (= (point) (point-min))) + (insert "\n")) + (when (and she-bang (not she-banged)) + (insert (concat she-bang "\n")) + (setq she-banged t)) + (org-babel-spec-to-string spec) + (setq block-counter (+ 1 block-counter)))) + lspecs) + (when make-dir + (make-directory fnd 'parents)) + (write-region nil nil file-name) + ;; set permissions on tangled files + (mapc (lambda (mode) (set-file-modes file-name mode)) modes) + (push file-name path-collector))))) (if (equal arg '(4)) (org-babel-tangle-single-block 1 t) (org-babel-tangle-collect-blocks lang-re tangle-file))) @@ -300,12 +287,8 @@ matching a regular expression." (lambda (file) (org-babel-with-temp-filebuffer file (run-hooks 'org-babel-post-tangle-hook))) - (mapcar #'car path-collector))) - ;; set permissions on tangled files - (mapc (lambda (pair) - (when (cdr pair) (set-file-modes (car pair) (cdr pair)))) - path-collector) - (mapcar #'car path-collector))))) + path-collector)) + path-collector)))) (defun org-babel-tangle-clean () "Remove comments inserted by `org-babel-tangle'. @@ -368,12 +351,12 @@ that the appropriate major-mode is set. SPEC has the form: (defun org-babel-tangle-collect-blocks (&optional lang-re tangle-file) "Collect source blocks in the current Org file. -Return an association list of source-code block specifications of -the form used by `org-babel-spec-to-string' grouped by language. -Optional argument LANG-RE can be used to limit the collected -source code blocks by languages matching a regular expression. -Optional argument TANGLE-FILE can be used to limit the collected -code blocks by target file." +Return an association list of language and source-code block +specifications of the form used by `org-babel-spec-to-string' +grouped by tangled file name. Optional argument LANG-RE can be +used to limit the collected source code blocks by languages +matching a regular expression. Optional argument TANGLE-FILE can +be used to limit the collected code blocks by target file." (let ((counter 0) last-heading-pos blocks) (org-babel-map-src-blocks (buffer-file-name) (let ((current-heading-pos @@ -390,12 +373,23 @@ code blocks by target file." (unless (or (string= src-tfile "no") (and tangle-file (not (equal tangle-file src-tfile))) (and lang-re (not (string-match-p lang-re src-lang)))) - ;; Add the spec for this block to blocks under its - ;; language. - (let ((by-lang (assoc src-lang blocks)) - (block (org-babel-tangle-single-block counter))) - (if by-lang (setcdr by-lang (cons block (cdr by-lang))) - (push (cons src-lang (list block)) blocks))))))) + ;; Add the spec for this block to blocks under its tangled + ;; file name. + (let* ((block (org-babel-tangle-single-block counter)) + (base-name (cond + ((string= "yes" src-tfile) + ;; buffer name + (file-name-sans-extension + (nth 1 block))) + ((> (length src-tfile) 0) src-tfile))) + (ext (or (cdr (assoc src-lang org-babel-tangle-lang-exts)) src-lang)) + (file-name (when base-name + ;; decide if we want to add ext to base-name + (if (and ext (string= "yes" src-tfile)) + (concat base-name "." ext) base-name))) + (by-fn (assoc file-name blocks))) + (if by-fn (setcdr by-fn (cons (cons src-lang block) (cdr by-fn))) + (push (cons file-name (list (cons src-lang block))) blocks))))))) ;; Ensure blocks are in the correct order. (mapcar (lambda (b) (cons (car b) (nreverse (cdr b)))) (nreverse blocks)))) -- 2.31.1 --------------C48047491B7A6FA61F577F4E--