From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 2OJVMLWJH2JGJQEAgWs5BA (envelope-from ) for ; Wed, 02 Mar 2022 16:13:57 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id SE7XLbWJH2JsSgAA9RJhRA (envelope-from ) for ; Wed, 02 Mar 2022 16:13:57 +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 1CC0844B3E for ; Wed, 2 Mar 2022 16:13:57 +0100 (CET) Received: from localhost ([::1]:56816 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPQfr-0002vy-QZ for larch@yhetil.org; Wed, 02 Mar 2022 10:13:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPQew-0002t7-Al for emacs-orgmode@gnu.org; Wed, 02 Mar 2022 10:12:58 -0500 Received: from [2607:f8b0:4864:20::42b] (port=40631 helo=mail-pf1-x42b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPQeu-0004sH-FA for emacs-orgmode@gnu.org; Wed, 02 Mar 2022 10:12:58 -0500 Received: by mail-pf1-x42b.google.com with SMTP id z15so2191239pfe.7 for ; Wed, 02 Mar 2022 07:12:55 -0800 (PST) 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=nIdfOxDayucdk91CaTTtB8P2uPDrMUeO2ZmXlY8XPCo=; b=dyIUqUos6CTPkzFnNYlq74ThM9SL4tugTVOTmDFX752CL1FG0Vti7McczhYmTRdyW8 vP/iWdXYj4FbGDk9CFR5EjfM9oQ5vXHDV3RT24AK72GfLdNDkE+l64OuJkE2tsSo4M6V n+OUoSHwI6kKUAyNjSklRzxTM8R7ZqUEidyPMuIJcnvZ02AMD7xdzLEc4r1tX8u11Dao 4XimUy1jYYezuEMWjZdGWeovY8Cd/Tnw+9mtJg7ZeqSsixGrrJtBlzkMsT7WpUFzo70R tP1JgPANqCJFANtwc9gekqlsWjf13kEKjdt/RfMxMJ7ywRRFySHkho5i2S6pEz4yJRHD WvHQ== 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=nIdfOxDayucdk91CaTTtB8P2uPDrMUeO2ZmXlY8XPCo=; b=BhAflyb8nxShZtOQfKUsK8qhoVlH/VQfdbcgXbXjN3WA9BaIvuprp2uVMh/o2YP1gt IVSMZYzuP3qipYiFzMWSXkLemojPCYbhMU2dMy3UxgANBwpMXu+T/bxdaCdmgOnCh3R2 gfsv5KflcTLF2DPpJTvZd2+wtVf4CXkZp5S6DAH7yk+lCtLq7SL+ASz8lWjUBXOpT9vi 1h0B3c05Xz7XKZ2RjlRwfSv2EEM/jccb7Z8QKC+cuqSihJq9s2l1dS1RgQoauLrMmp+r L4ZyIht14t2VGHlzJ1MKlwYSqInLSyeA5ZyQVd3xrpNZZMGu7Xdo1Bod0eA20LqQjnum lgqQ== X-Gm-Message-State: AOAM530lgw8SmCo0OLUEiNQ5eH2SSS61qMcVXoYQMZhJy9+MLH3tLOi+ ln6uTmFXS82WLvcvkSEBU3w= X-Google-Smtp-Source: ABdhPJxtcAUVOhGF1VZFW5kEIiB8n2IdOly4Mq9F2NU7WRqpXG9sNQndVHh09KY4xzhff6LGFTWXfg== X-Received: by 2002:a63:ce13:0:b0:378:5d08:ac95 with SMTP id y19-20020a63ce13000000b003785d08ac95mr19401651pgf.55.1646233974055; Wed, 02 Mar 2022 07:12:54 -0800 (PST) Received: from localhost ([210.3.160.218]) by smtp.gmail.com with ESMTPSA id h14-20020a63384e000000b00366ba5335e7sm15858358pgn.72.2022.03.02.07.12.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 07:12:53 -0800 (PST) From: Ihor Radchenko To: Max Nikulin Subject: Re: profiling latency in large org-mode buffers (under both main & org-fold feature) In-Reply-To: References: <87fsobpism.fsf@localhost> <87r17to817.fsf@localhost> <87lexy2hrz.fsf@localhost> <87y21wkdwu.fsf@localhost> Date: Wed, 02 Mar 2022 23:12:20 +0800 Message-ID: <87fso0xuaz.fsf@localhost> MIME-Version: 1.0 Content-Type: text/plain X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42b (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=yantar92@gmail.com; helo=mail-pf1-x42b.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-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1646234037; 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=nIdfOxDayucdk91CaTTtB8P2uPDrMUeO2ZmXlY8XPCo=; b=D0Wa9H4+L57X4lkcjpTgbYaQDyfyi9ZZaFUOkmRL402pNYyqxUzLr456gjmmbQDGY0GWhd 6TXj5ol98BVrN1Q6kaFF5d5ijJIH072XmaltCTmXQ4PTdOvsqoJf5pWFVTf4AQe0x8Vmnq rHP7ba3oBldlEHoy/9NZfNtEzatIDw4ELW+T6LO0jlAl29dKCJJnm8MR0LNw6fhuAy7asb dLADIKxfTegjujvnp2CBZsjbohPfzGw0/7XnjitjMy0eFC6YR0qEG8RlDTr/Q2F5kfYnEw +tXeha2w8nTR0TgAO3NOhVgt4/M2cifvvns/UXInC9o80MZvX3ozih62NAxwnA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1646234037; a=rsa-sha256; cv=none; b=StwprDyURKuEDIKHUL1IR0/ArfdVOYOndVe0wRp534eY0uuMCRa38dwE9oNjAT12APFYSl N/iipI4a+4amJfLbZ95vaAxe271Fxdc9o8/QjPXXoDpffs7+kcZdwatiPObpnx+fE2cZCT QCW0Z3m7uKOkmENzE48+KuCvBiiBH7YL5+Sjnoo6dVMhb82oCwVPz42AyyodeR0kWg3B5V fOq6gBiX4zKq0EKEq0kNAUZ0SsNKjRqPnezEmHh5DxFZzLTwpCH9t8+3STXZiB1hF6zRSz 5dOr2jjLoPeYhNruy0apvzIHApLOSotFyddZRjG/QNHE1LZtGYdC11heqDVp2A== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=dyIUqUos; dmarc=pass (policy=none) header.from=gmail.com; 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: -3.78 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=dyIUqUos; dmarc=pass (policy=none) header.from=gmail.com; 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: 1CC0844B3E X-Spam-Score: -3.78 X-Migadu-Scanner: scn1.migadu.com X-TUID: XXm+uHJo8oy8 Max Nikulin writes: > On 27/02/2022 13:43, Ihor Radchenko wrote: >> >> Now, I did an extended profiling of what is happening using perf: >> >> 6.20% [.] buf_bytepos_to_charpos > > Maybe I am interpreting such results wrongly, but it does not look like > a bottleneck. Anyway thank you very much for such efforts, however it is > unlikely that I will join to profiling in near future. The perf data I provided is a bit tricky. I recorded statistics over the whole Emacs session + used fairly small number of iterations in your benchmark code. Now, I repeated the testing plugging perf to Emacs only during the benchmark execution: With refile cache and markers: 22.82% emacs-29.0.50.1 emacs-29.0.50.1 [.] buf_bytepos_to_charpos 16.68% emacs-29.0.50.1 emacs-29.0.50.1 [.] rpl_re_search_2 8.02% emacs-29.0.50.1 emacs-29.0.50.1 [.] re_match_2_internal 6.93% emacs-29.0.50.1 emacs-29.0.50.1 [.] Fmemq 4.05% emacs-29.0.50.1 emacs-29.0.50.1 [.] allocate_vectorlike 1.88% emacs-29.0.50.1 emacs-29.0.50.1 [.] mark_object Without refile cache: 17.25% emacs-29.0.50.1 emacs-29.0.50.1 [.] rpl_re_search_2 15.84% emacs-29.0.50.1 emacs-29.0.50.1 [.] buf_bytepos_to_charpos 8.89% emacs-29.0.50.1 emacs-29.0.50.1 [.] re_match_2_internal 8.00% emacs-29.0.50.1 emacs-29.0.50.1 [.] Fmemq 4.35% emacs-29.0.50.1 emacs-29.0.50.1 [.] allocate_vectorlike 2.01% emacs-29.0.50.1 emacs-29.0.50.1 [.] mark_object Percents should be adjusted for larger execution time in the first dataset, but otherwise it is clear that buf_bytepos_to_charpos dominates the time delta. >> I am not sure if I understand the code correctly, but that loop is >> clearly scaling performance with the number of markers > > I may be terribly wrong, but it looks like an optimization attempt that > may actually ruin performance. My guess is the following. Due to > multibyte characters position in buffer counted in characters may > significantly differ from index in byte sequence. Since markers have > both values bytepos and charpos, they are used (when available) to > narrow down initial estimation interval [0, buffer size) to nearest > existing markers. The code below even creates temporary markers to make > next call of the function faster. I tend to agree after reading the code again. I tried to play around with that marker loop. It seems that the loop should not be mindlessly disabled, but it can be sufficient to check only a small number of markers in front of the marker list. The cached temporary markers are always added in front of the list. Limiting the number of checked markers to 10, I got the following result: With threshold and refile cache: | 9.5.2 | | | | | nm-tst | 28.060029337 | 4 | 1.8427608629999996 | | org-refile-get-targets | 3.2445615439999997 | 0 | 0.0 | | nm-tst | 33.648259137000004 | 4 | 1.2304310540000003 | | org-refile-cache-clear | 0.034879062 | 0 | 0.0 | | nm-tst | 23.974124596 | 5 | 1.4291488149999996 | Markers add +~5.6sec. Original Emacs code and refile cache: | 9.5.2 | | | | | nm-tst | 29.494383528 | 4 | 3.0368508530000002 | | org-refile-get-targets | 3.635947646 | 1 | 0.4542479730000002 | | nm-tst | 36.537926593 | 4 | 1.1297576349999998 | | org-refile-cache-clear | 0.009665364999999999 | 0 | 0.0 | | nm-tst | 23.283457105 | 4 | 1.0536496499999997 | Markers add +7sec. The improvement is there, though markers still somehow come into play. I speculate that limiting the number of checked markers might also force adding extra temporary markers to the list, but I haven't looked into that possibility for now. It might be better to discuss with emacs-devel before trying too hard. >> Finally, FYI. I plan to work on an alternative mechanism to access Org >> headings - generic Org query library. It will not use markers and >> implement ideas from org-ql. org-refile will eventually use that generic >> library instead of current mechanism. > > I suppose that markers might be implemented in an efficient way, and > much better performance may be achieved when low-level data structures > are accessible. I am in doubts concerning attempts to create something > that resembles markers but based purely on high-level API. I am currently using a custom version of org-ql utilising the new element cache. It is substantially faster compared to current org-refile-get-targets. The org-ql version runs in <2 seconds at worst when calculating all refile targets from scratch, while org-refile-get-targets is over 10sec. org-ql version gives 0 noticeable latency when there is an extra text query to narrow down the refile targets. So, is it certainly possible to improve the performance just using high-level org-element cache API + regexp search without markers. Note that we already have something resembling markers on high-level API. It is what org element cache is doing - on every user edit, it re-calculates the Org element boundaries (note that Nicolas did not use markers to store boundaries of org elements). The merged headline support by org-element cache is the first stage of my initial plan to speed up searching staff in Org - be it agenda items, IDs, or refile targets. Best, Ihor