From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: master 979308b4ca 5/9: org-export-data: Concatenate strings in temporary buffer for performance
Date: Thu, 16 Jun 2022 13:43:29 -0400 [thread overview]
Message-ID: <jwv4k0ktt9l.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87y1xwept4.fsf@localhost> (Ihor Radchenko's message of "Thu, 16 Jun 2022 20:49:27 +0800")
Ihor Radchenko [2022-06-16 20:49:27] wrote:
> I hope that I did not get it wrong. I _believe_ that I did see an
> improvement. So, you better check if it makes a difference on your side
> if you revert that patch (especially with un-optimized build where the
> differences should be more prominent).
The fact that you think you saw a significant difference is already
a good hint that there might be something there, in any case.
But of course, we need to look more closely to see not just "if" but
"how" it is faster.
> AFAIK, there are several caveats with `mapconcat':
> 1. The way it was used in Org before the patch was
> (mapconcat #function sequence "")
> Here, `mapconcat' will call `concat' to generate the new string with 2N
> number of arguments. Half of the arguments will be "".
That's true but we're talking about a single vector, which is allocated
via SAFE_ALLOCA, so it shouldn't put any significant extra pressure on
the GC, and compared to the time taken to do the "map" part of
`mapconcat`, it should be negligible.
This said, it's trivial to eliminate this cost and is probably
a good optimization. See patch below.
> 2. `concat' is doing ad-hoc save/restore of text properties. IDK if it
> matters in this particular case, but buffers should work more
> efficiently with handling text properties (AFAIK)
Indeed, I suspect if there's a performance difference it is likely
coming from that area.
> 3. `concat' tries to consider non-string argument adding some (probably
> small) extra overheads.
This should be dwarfed by the "map" part of `mapconcat`, so I'd be
surprised if it makes any difference. Also your replacement code uses
`insert` here instead which performs the same kind of dance anyway.
Lars Ingebrigtsen [2022-06-16 14:45:26] wrote:
> It looks like it shouldn't be that difficult to improve the performance
> radically in the common case of FUNCTION being #'identity. If there's
> no SEPARATOR we can just pass the arguments more or less directly on to
> Fconcat, and if there is a SEPARATOR, we just have to adjust the arg
> list.
Indeed, we could optimize the common case of function being `identity`,
but it won't help in this particular case.
Stefan
diff --git a/src/fns.c b/src/fns.c
index d81a3bfcac3..c9251a80f53 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -2843,12 +2843,18 @@ DEFUN ("mapconcat", Fmapconcat, Smapconcat, 2, 3, 0,
SAFE_ALLOCA_LISP (args, args_alloc);
ptrdiff_t nmapped = mapcar1 (leni, args, function, sequence);
ptrdiff_t nargs = 2 * nmapped - 1;
+ eassert (nmapped == leni);
- for (ptrdiff_t i = nmapped - 1; i > 0; i--)
- args[i + i] = args[i];
+ if (!NILP (Fequal (separator, empty_multibyte_string)))
+ nargs = nmapped;
+ else
+ {
+ for (ptrdiff_t i = nmapped - 1; i > 0; i--)
+ args[i + i] = args[i];
- for (ptrdiff_t i = 1; i < nargs; i += 2)
- args[i] = separator;
+ for (ptrdiff_t i = 1; i < nargs; i += 2)
+ args[i] = separator;
+ }
Lisp_Object ret = Fconcat (nargs, args);
SAFE_FREE ();
next prev parent reply other threads:[~2022-06-16 17:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <165536698076.5328.17316430648307086249@vcs2.savannah.gnu.org>
[not found] ` <20220616080945.7DD14C01685@vcs2.savannah.gnu.org>
2022-06-16 12:19 ` master 979308b4ca 5/9: org-export-data: Concatenate strings in temporary buffer for performance Stefan Monnier
2022-06-16 12:45 ` Lars Ingebrigtsen
2022-06-16 13:33 ` Robert Pluim
2022-06-16 12:49 ` Ihor Radchenko
2022-06-16 17:43 ` Stefan Monnier [this message]
2022-06-17 11:35 ` Ihor Radchenko
2022-06-16 12:53 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwv4k0ktt9l.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=emacs-devel@gnu.org \
--cc=yantar92@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.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).