From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel 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 Message-ID: References: <165536698076.5328.17316430648307086249@vcs2.savannah.gnu.org> <20220616080945.7DD14C01685@vcs2.savannah.gnu.org> <87y1xwept4.fsf@localhost> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5178"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Ihor Radchenko Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Jun 16 19:44:42 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o1tXt-00019U-Qe for ged-emacs-devel@m.gmane-mx.org; Thu, 16 Jun 2022 19:44:42 +0200 Original-Received: from localhost ([::1]:36348 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o1tXs-0007gL-35 for ged-emacs-devel@m.gmane-mx.org; Thu, 16 Jun 2022 13:44:40 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58704) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1tWs-0006zS-Di for emacs-devel@gnu.org; Thu, 16 Jun 2022 13:43:38 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:64371) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1tWp-0005kf-Qy for emacs-devel@gnu.org; Thu, 16 Jun 2022 13:43:37 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 9B793441679; Thu, 16 Jun 2022 13:43:33 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id E9755441673; Thu, 16 Jun 2022 13:43:31 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1655401411; bh=9m6QyFc1UW/fEzRVugSwAIzuOkH0JL4XdKDyWi4nyZY=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=ZCZxKs0sNolT6TLtGQJSd/zJom5+yofawc2Qb2SUV4XrNSKJQtkBqBTenkjMpF151 lyM1bFymeI37QxdbjQh9ODBpHVzByjbx6qk02THMfDwnoF4BqOYMy2oK/Hq6c+GApm kJLHNtET9cnztvBV8/m3FIfjtVozmsmPeIuXciybz6MYZ0Pnnvg8ysQWOnERFI7I8/ tmq4WjK8ZYRvEn2ojgqcZdbGqN12isueGn+DcDxl+UMXGmrPeHWzNmFAQtYkWu1931 VfwyMMrn/QmRcI8EVxCsCQMO8LKu58FHXwUt8WdXJuY2+FsYBpPtQ8gbAPtRGl2Hso 6VEF1dIszhehw== Original-Received: from lechazo (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id CCF21120173; Thu, 16 Jun 2022 13:43:31 -0400 (EDT) In-Reply-To: <87y1xwept4.fsf@localhost> (Ihor Radchenko's message of "Thu, 16 Jun 2022 20:49:27 +0800") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:291250 Archived-At: 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 ();