From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id KOxqCDbJNWI1+AAAgWs5BA (envelope-from ) for ; Sat, 19 Mar 2022 13:14:46 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id sAorATbJNWKUPQAAG6o9tA (envelope-from ) for ; Sat, 19 Mar 2022 13:14:46 +0100 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 9C4788D2D for ; Sat, 19 Mar 2022 13:14:45 +0100 (CET) Received: from localhost ([::1]:34138 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nVXZw-0005Gc-13 for larch@yhetil.org; Sat, 19 Mar 2022 07:49:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48944) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nVXY7-0005GT-FD for emacs-orgmode@gnu.org; Sat, 19 Mar 2022 07:47:11 -0400 Received: from [2607:f8b0:4864:20::42e] (port=38649 helo=mail-pf1-x42e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nVXY5-0002Rn-C1 for emacs-orgmode@gnu.org; Sat, 19 Mar 2022 07:47:11 -0400 Received: by mail-pf1-x42e.google.com with SMTP id p5so5583989pfo.5 for ; Sat, 19 Mar 2022 04:47:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=i3cXlBKETjg/DhckPHZFl2kIj0xLUeVKoTwbEtpW1Jc=; b=dczKoPx5EBpdx9CMtaviBgpSOxiTmic8z/FkQi4g3Q/HqQ8qAG+P55OwcV2Y0kpDIw NCT0Lg/ITQeIi2rs5FcdBFqw+Py3XG2wmo3fFJ8E4Z+W95QD5O5gWJzK2TTC3CI1U8xP bJ/NtoW2notUvJJcLM8foEzhVn1dnqOQSW5JqmrHvRhMPMBtOPe4gfjmQz04OD4rfYIG 2VRPy6Du3hUTYIpXM9xFA8j+IEi13AG1fqF9LwQYzwc51Q4BRByOno4C7bFJAMha3lCu 6UfuQldmkFCVuy/lzuXoAUz+Y5A1iueknHUTK1StibjPJWcXP4X8+HiJuMfAJkjrnDIi A//Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=i3cXlBKETjg/DhckPHZFl2kIj0xLUeVKoTwbEtpW1Jc=; b=QVS/4PY7NyorXU23fVTW1ySwfKh1h3L7o/pYzd5vOvMV0JWJ284xxlSsqJ/Fqs8S5B m8uhRdxU+GXunUKMqFMXaRxgqSDNiYaUr8pvitjp3AIA5MyTEGvO9mXEPEfpwKsRcV6J 1qPnasGbBRzkikEmUiiqRs+knW36yT9lV68bjO7vCEzoMKiEklnjRObB58BgQIkNtV61 1kvMb1dTJ0a7uMgUkK9cvxBMBjCuyGAglducJeI266EB5YEjeetUt/xHl1G3jZzkBG+l 8fVCSoBYNUsZfsMB0/PzhkNGP6fYvkzOcl8uD+3XWH8RMorCkEhzZWC69pca7VVxyma7 mGbQ== X-Gm-Message-State: AOAM532RJJZdogR2BbLAap0WVXWTKTxsAIeS7Veq63NlxPFXP2AjnRWO gXDz9J56n6rUILGwzVc8UDw= X-Google-Smtp-Source: ABdhPJwmwpsauLrkC++jw3VBAEnOKX47TK6X+Aye3nzCWTQfe+zu8Bfwt8G6Oj5EGqz2vVjb8omnnA== X-Received: by 2002:a05:6a00:170c:b0:4f7:260b:297a with SMTP id h12-20020a056a00170c00b004f7260b297amr14972168pfc.12.1647690427516; Sat, 19 Mar 2022 04:47:07 -0700 (PDT) Received: from localhost ([45.128.72.3]) by smtp.gmail.com with ESMTPSA id j13-20020a056a00130d00b004f1025a4361sm13486639pfu.202.2022.03.19.04.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Mar 2022 04:47:06 -0700 (PDT) From: Ihor Radchenko To: Jamie Matthews , Nicolas Goaziou Subject: [PATCH] Re: [BUG] org-cite: 10 second hang opening a ~4k org file with 10MB bibtex library In-Reply-To: References: <87pmmifirh.fsf@localhost> <87fsnefg8b.fsf@localhost> <87cziifeo4.fsf@localhost> <87a6dmfd89.fsf@localhost> Date: Sat, 19 Mar 2022 19:47:35 +0800 Message-ID: <877d8qf9k8.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42e (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=yantar92@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "emacs-orgmode@gnu.org" Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1647692085; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=V2zMFTWJTymy9MWuKt1/hGM0K68DvEXldJUfhnXzgaI=; b=BvU6yWdRwFmrMHktwC6x1QKiXQ+il62PTxLPqJRmBLHgPh10Zn+4cfETUxRyRvYIHaR8hj KJFBRteBlKemu0W4xKc5Dc3HNkzGCRJ1HRWfT6oe9p5q+r00KMVHlpyA5d/fqROkTz24iu Liybtwwi3pz1yStnMN1g2p+oYN8Sy2dJETuhYlKtl6BCiARmZWP5ru8M7pFc8GIl0otn9E GYbvk9eWHC8kZJBN3dEvdzTPvDNZESXpczegKPlSOy+uU+Fg1BzxDKdTwIXzbTCWFzP6xi nK/cWXMw/yh+3QDiXLfpoFTxAKiyTnbWOioXcACwXHlt3hEDtfAFbwwFWTWWPw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1647692085; a=rsa-sha256; cv=none; b=uQYLeawvlQW1HWBhi3vNhOieItozb1pmmu7rbzAeflUZxJlq5qZqkg2krF2Tbox37blecz fVH0yDOvIfK8VQUSIWwG3hER4rQFe8HNoyUs0Ffltt9mf9kLrY4DXHyrxhVkD+S8+NUg13 6HFve4DyEHXW6iAuSbT3c+qxAdUw0kzilc+OOsAJ+QegnKIvXGA+xYp6/hUbDI1nJbPQBa q6CTa9UWJqyJ3ghH+gzsRWiApVr4yQThhvyOw65zkzVQpUqtQR6s0O4j1XZxKT3Cl72fHx djQfzQdVb3rqpufhApuuuRFYCrr6Dloj7OHN1Ntmx3ryOnMF5PAmFR3ynw8RAg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=dczKoPx5; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 5.36 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=dczKoPx5; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 9C4788D2D X-Spam-Score: 5.36 X-Migadu-Scanner: scn0.migadu.com X-TUID: 9oiN9VMawquo --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Jamie Matthews writes: > I can confirm that the key turns red on insert when I altered the key out= side of emacs (with that second version of `org-cite-basic--parse-bibliogra= phy`). Great. Then, I am attaching the patch with the new version of the function. I will let Nicolas decide if it is good enough to be merged. I do not feel confident enough with org-cite code to judge if my approach is not missing some edge cases. > However, I'm now noticing that the hang improvement earlier (< a second d= own from ~10) doesn't always occur. > > Specifically, if I > > 1. emacs -Q > 2. eval your code in scratch > 3. C-x C-f to the org file > > I get the hang. However, if I then > > > 1. kill the org buffer > 2. eval the code again > 3. re-find the org file > > the hang is gone. Without eval=E2=80=8Bing your code in between, killing = the org buffer and finding it again in the same emacs session reproduces th= e hang everytime, which was probably what I did the first time I report the= improvement, as in I didn't check it worked from startup. It is most likely because you defun the function after emacs -Q before org is loaded, then open Org file (opening Org file autoloads org-mode), and then org-mode overwrites the manual defun. If I am correct, putting (require 'oc-basic) before defun will fix what you are seeing. Best, Ihor --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-oc-basic-Speed-up-cached-bibliography-retrival.patch >From 100b722708c68bc65af637c3ad4e289943cccd7c Mon Sep 17 00:00:00 2001 Message-Id: <100b722708c68bc65af637c3ad4e289943cccd7c.1647690044.git.yantar92@gmail.com> From: Ihor Radchenko Date: Sat, 19 Mar 2022 19:24:55 +0800 Subject: [PATCH] oc-basic: Speed up cached bibliography retrival * lisp/oc-basic.el (org-cite-basic--file-id-cache): New variable storing hash of bibliography file contents. (org-cite-basic--parse-bibliography): Skip buffer hash calculation when bibliography file is unchanged on disk. Calculating buffer hash on every call is slow when bibliography file is large. * lisp/org-compat.el: (org-file-has-changed-p--hash-table, org-file-has-changed-p): Define `file-has-changed-p' from Emacs 29 if necessary. See https://list.orgmode.org/LO2P265MB1758E12E04832DC712F5A8E9DC149@LO2P265MB1758.GBRP265.PROD.OUTLOOK.COM/T/#t --- lisp/oc-basic.el | 11 +++++++++-- lisp/org-compat.el | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lisp/oc-basic.el b/lisp/oc-basic.el index e2dfc0603..d1ea620ad 100644 --- a/lisp/oc-basic.el +++ b/lisp/oc-basic.el @@ -233,6 +233,8 @@ (defun org-cite-basic--parse-bibtex (dialect) entries))) entries)) +(defvar org-cite-basic--file-id-cache nil + "Hash table linking files to their hash.") (defun org-cite-basic--parse-bibliography (&optional info) "List all entries available in the buffer. @@ -245,14 +247,19 @@ (defun org-cite-basic--parse-bibliography (&optional info) as symbols, and values as strings or nil. Optional argument INFO is the export state, as a property list." + (unless (hash-table-p org-cite-basic--file-id-cache) + (setq org-cite-basic--file-id-cache (make-hash-table :test #'equal))) (if (plist-member info :cite-basic/bibliography) (plist-get info :cite-basic/bibliography) (let ((results nil)) (dolist (file (org-cite-list-bibliography-files)) (when (file-readable-p file) (with-temp-buffer - (insert-file-contents file) - (let* ((file-id (cons file (org-buffer-hash))) + (when (or (org-file-has-changed-p file) + (not (gethash file org-cite-basic--file-id-cache))) + (insert-file-contents file) + (puthash file (org-buffer-hash) org-cite-basic--file-id-cache)) + (let* ((file-id (cons file (gethash file org-cite-basic--file-id-cache))) (entries (or (cdr (assoc file-id org-cite-basic--bibliography-cache)) (let ((table diff --git a/lisp/org-compat.el b/lisp/org-compat.el index 38d330de6..e96dcba8b 100644 --- a/lisp/org-compat.el +++ b/lisp/org-compat.el @@ -73,6 +73,35 @@ (defvar org-table-dataline-regexp) (defvar org-table-tab-recognizes-table.el) (defvar org-table1-hline-regexp) + +;;; Emacs < 29 compatibility + +(defvar org-file-has-changed-p--hash-table (make-hash-table :test #'equal) + "Internal variable used by `org-file-has-changed-p'.") + +(if (fboundp 'file-has-changed-p) + (defalias org-file-has-changed-p #'file-has-changed-p) + (defun org-file-has-changed-p (file &optional tag) + "Return non-nil if FILE has changed. +The size and modification time of FILE are compared to the size +and modification time of the same FILE during a previous +invocation of `org-file-has-changed-p'. Thus, the first invocation +of `org-file-has-changed-p' always returns non-nil when FILE exists. +The optional argument TAG, which must be a symbol, can be used to +limit the comparison to invocations with identical tags; it can be +the symbol of the calling function, for example." + (let* ((file (directory-file-name (expand-file-name file))) + (remote-file-name-inhibit-cache t) + (fileattr (file-attributes file 'integer)) + (attr (and fileattr + (cons (file-attribute-size fileattr) + (file-attribute-modification-time fileattr)))) + (sym (concat (symbol-name tag) "@" file)) + (cachedattr (gethash sym org-file-has-changed-p--hash-table))) + (when (not (equal attr cachedattr)) + (puthash sym attr org-file-has-changed-p--hash-table))))) + + ;;; Emacs < 28.1 compatibility -- 2.34.1 --=-=-=--