From: Ihor Radchenko <yantar92@posteo.net>
To: Emanuel Berg <incal@dataswamp.org>
Cc: emacs-devel@gnu.org
Subject: [PATCH] Re: Bignum performance (was: Shrinking the C core)
Date: Fri, 11 Aug 2023 14:07:57 +0000 [thread overview]
Message-ID: <87bkfdsmde.fsf@localhost> (raw)
In-Reply-To: <875y5lkb4b.fsf@dataswamp.org>
[-- Attachment #1: Type: text/plain, Size: 3633 bytes --]
Emanuel Berg <incal@dataswamp.org> writes:
>> Maybe we could somehow re-use the already allocated bignum
>> objects, similar to what is done for cons cells (see
>> src/alloc.c:Fcons).
>
> Sounds reasonable :)
And... is has been already done, actually.
allocate_vectorlike calls allocate_vector_from_block, which re-uses
pre-allocated objects.
And looking into the call graph, this exact branch calling
allocate_vector_from_block is indeed called for the bignums:
33.05% 0.00% emacs [unknown] [.] 0000000000000000
|
---0
|
|--28.04%--allocate_vectorlike
| |
| --27.78%--allocate_vector_from_block (inlined)
| |
| |--2.13%--next_vector (inlined)
| |
| --0.74%--setup_on_free_list (inlined)
If it manually cut off `allocate_vector_from_block', the benchmark time
increases twice. So, there is already some improvement coming from
re-using allocated memory.
I looked deeper into the code tried to cut down on unnecessary looping
over the pre-allocated `vector_free_lists'. See the attached patch.
Without the patch:
perf record ~/Git/emacs/src/emacs -Q -batch -l /tmp/fib.eln
2.321 s
28.60% emacs emacs [.] allocate_vectorlike
24.36% emacs emacs [.] process_mark_stack
3.76% emacs libgmp.so.10.5.0 [.] __gmpz_sizeinbase
3.59% emacs emacs [.] pdumper_marked_p_impl
3.53% emacs emacs [.] mark_char_table
With the patch:
perf record ~/Git/emacs/src/emacs -Q -batch -l /tmp/fib.eln
1.968 s
33.17% emacs emacs [.] process_mark_stack
5.51% emacs libgmp.so.10.5.0 [.] __gmpz_sizeinbase
5.05% emacs emacs [.] mark_char_table
4.88% emacs emacs [.] pdumper_marked_p_impl
3.30% emacs emacs [.] pdumper_set_marked_impl
...
2.52% emacs emacs [.] allocate_vectorlike
allocate_vectorlike clearly takes a lot less time by not trying to loop
over all the ~500 empty elements of vector_free_lists.
We can further get rid of the GC by temporarily disabling it (just for
demonstration):
(let ((beg (float-time)))
(setq gc-cons-threshold most-positive-fixnum)
(fib 10000 1000)
(message "%.3f s" (- (float-time) beg)) )
perf record ~/Git/emacs/src/emacs -Q -batch -l /tmp/fib.eln
0.739 s
17.11% emacs libgmp.so.10.5.0 [.] __gmpz_sizeinbase
7.35% emacs libgmp.so.10.5.0 [.] __gmpz_add
6.51% emacs emacs [.] arith_driver
6.03% emacs libc.so.6 [.] malloc
5.57% emacs emacs [.] allocate_vectorlike
5.20% emacs [unknown] [k] 0xffffffffaae01857
4.16% emacs libgmp.so.10.5.0 [.] __gmpn_add_n_coreisbr
3.72% emacs emacs [.] check_number_coerce_marker
3.35% emacs fib.eln [.] F666962_fib_0
3.29% emacs emacs [.] allocate_pseudovector
2.30% emacs emacs [.] Flss
Now, the actual bignum arithmetics (lisp/gmp.c) takes most of the CPU time.
I am not sure what differs between Elisp gmp bindings and analogous SBCL
binding so that SBCL is so much faster.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: allocate_vector_from_block.diff --]
[-- Type: text/x-patch, Size: 1712 bytes --]
diff --git a/src/alloc.c b/src/alloc.c
index 17ca5c725d0..62e96b4c9de 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -3140,6 +3140,7 @@ large_vector_vec (struct large_vector *p)
vectors of the same NBYTES size, so NTH == VINDEX (NBYTES). */
static struct Lisp_Vector *vector_free_lists[VECTOR_MAX_FREE_LIST_INDEX];
+static int vector_free_lists_min_idx = VECTOR_MAX_FREE_LIST_INDEX;
/* Singly-linked list of large vectors. */
@@ -3176,6 +3177,8 @@ setup_on_free_list (struct Lisp_Vector *v, ptrdiff_t nbytes)
set_next_vector (v, vector_free_lists[vindex]);
ASAN_POISON_VECTOR_CONTENTS (v, nbytes - header_size);
vector_free_lists[vindex] = v;
+ if ( vindex < vector_free_lists_min_idx )
+ vector_free_lists_min_idx = vindex;
}
/* Get a new vector block. */
@@ -3230,8 +3233,8 @@ allocate_vector_from_block (ptrdiff_t nbytes)
/* Next, check free lists containing larger vectors. Since
we will split the result, we should have remaining space
large enough to use for one-slot vector at least. */
- for (index = VINDEX (nbytes + VBLOCK_BYTES_MIN);
- index < VECTOR_MAX_FREE_LIST_INDEX; index++)
+ for (index = max ( VINDEX (nbytes + VBLOCK_BYTES_MIN), vector_free_lists_min_idx );
+ index < VECTOR_MAX_FREE_LIST_INDEX; index++, vector_free_lists_min_idx++)
if (vector_free_lists[index])
{
/* This vector is larger than requested. */
@@ -3413,6 +3416,7 @@ sweep_vectors (void)
gcstat.total_vectors = 0;
gcstat.total_vector_slots = gcstat.total_free_vector_slots = 0;
memset (vector_free_lists, 0, sizeof (vector_free_lists));
+ vector_free_lists_min_idx = VECTOR_MAX_FREE_LIST_INDEX;
/* Looking through vector blocks. */
[-- Attachment #3: Type: text/plain, Size: 224 bytes --]
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
next prev parent reply other threads:[~2023-08-11 14:07 UTC|newest]
Thread overview: 247+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-09 9:46 Shrinking the C core Eric S. Raymond
2023-08-09 10:55 ` Andreas Schwab
2023-08-09 11:03 ` Eric S. Raymond
2023-08-09 11:29 ` Andreas Schwab
2023-08-09 12:34 ` Po Lu
2023-08-09 15:51 ` Eric S. Raymond
2023-08-09 23:56 ` Po Lu
2023-08-10 1:19 ` Eric S. Raymond
2023-08-10 1:47 ` Christopher Dimech
2023-08-10 1:58 ` Eric Frederickson
2023-08-10 2:07 ` Sam James
2023-08-10 2:44 ` Po Lu
2023-08-10 6:48 ` Eli Zaretskii
2023-08-10 21:21 ` Eric S. Raymond
2023-08-10 21:19 ` Eric S. Raymond
2023-08-10 21:56 ` Emanuel Berg
2023-08-11 5:46 ` Eli Zaretskii
2023-08-11 8:45 ` Emanuel Berg
2023-08-11 11:24 ` Eli Zaretskii
2023-08-11 12:12 ` Emanuel Berg
2023-08-11 13:16 ` Eli Zaretskii
2023-08-10 2:28 ` Po Lu
2023-08-10 4:15 ` Christopher Dimech
2023-08-10 7:44 ` Eli Zaretskii
2023-08-10 21:54 ` Emanuel Berg
2023-08-11 10:27 ` Bignum performance (was: Shrinking the C core) Ihor Radchenko
2023-08-11 12:10 ` Emanuel Berg
2023-08-11 12:32 ` Ihor Radchenko
2023-08-11 12:38 ` Emanuel Berg
2023-08-11 14:07 ` Ihor Radchenko [this message]
2023-08-11 18:06 ` [PATCH] " Emanuel Berg
2023-08-11 19:41 ` Ihor Radchenko
2023-08-11 19:50 ` Emanuel Berg
2023-08-12 8:24 ` Ihor Radchenko
2023-08-12 16:03 ` Emanuel Berg
2023-08-13 9:09 ` Ihor Radchenko
2023-08-13 9:49 ` Emanuel Berg
2023-08-13 10:21 ` Ihor Radchenko
2023-08-14 2:20 ` Emanuel Berg
2023-08-14 2:42 ` [PATCH] Re: Bignum performance Po Lu
2023-08-14 4:16 ` Emanuel Berg
2023-08-14 7:15 ` Ihor Radchenko
2023-08-14 7:50 ` Po Lu
2023-08-14 9:28 ` Ihor Radchenko
2023-08-15 14:28 ` Emanuel Berg
2023-08-14 7:20 ` [PATCH] Re: Bignum performance (was: Shrinking the C core) Ihor Radchenko
2023-08-11 22:46 ` Emanuel Berg
2023-08-12 8:30 ` Ihor Radchenko
2023-08-12 16:22 ` Emanuel Berg
2023-08-13 9:12 ` Ihor Radchenko
2023-08-11 14:14 ` Mattias Engdegård
2023-08-11 18:09 ` Emanuel Berg
2023-08-10 23:49 ` Shrinking the C core Eric S. Raymond
2023-08-11 0:03 ` Christopher Dimech
2023-08-11 8:24 ` Immanuel Litzroth
2023-08-11 7:03 ` Eli Zaretskii
2023-08-11 7:19 ` tomas
2023-08-11 10:57 ` Eli Zaretskii
2023-08-10 11:28 ` Dmitry Gutov
2023-08-10 21:26 ` Eric S. Raymond
2023-08-12 2:46 ` Richard Stallman
2023-08-12 3:22 ` Emanuel Berg
2023-08-12 8:33 ` Ihor Radchenko
2023-08-12 15:58 ` Emanuel Berg
2023-08-13 9:13 ` Ihor Radchenko
2023-08-13 9:55 ` Emanuel Berg
2023-08-13 10:23 ` Ihor Radchenko
2023-08-13 20:55 ` Emanuel Berg
2023-08-14 0:13 ` Emanuel Berg
2023-08-12 18:32 ` tomas
2023-08-12 22:08 ` Emanuel Berg
2023-08-12 23:09 ` Emanuel Berg
2023-08-13 5:50 ` tomas
2023-08-13 8:38 ` Emanuel Berg
2023-08-13 15:54 ` [External] : " Drew Adams
2023-08-13 8:00 ` Andreas Schwab
2023-08-13 9:21 ` Emanuel Berg
2023-08-14 7:27 ` Alfred M. Szmidt
2023-08-14 7:36 ` Ihor Radchenko
2023-08-14 7:50 ` Alfred M. Szmidt
2023-08-15 22:57 ` Emanuel Berg
2023-08-16 10:27 ` Ihor Radchenko
2023-08-19 13:29 ` Emanuel Berg
2023-08-20 5:09 ` Ihor Radchenko
2023-08-20 6:51 ` Emanuel Berg
2023-08-20 7:14 ` Ihor Radchenko
2023-08-20 7:52 ` Emanuel Berg
2023-08-20 13:01 ` tomas
2023-08-20 13:12 ` Ihor Radchenko
2023-08-20 8:28 ` Alfred M. Szmidt
2023-08-20 9:29 ` Emanuel Berg
2023-08-20 15:22 ` Alfred M. Szmidt
2023-08-20 15:36 ` Ihor Radchenko
2023-08-20 15:45 ` Eli Zaretskii
2023-08-20 15:54 ` Ihor Radchenko
2023-08-20 16:29 ` Alfred M. Szmidt
2023-08-20 16:37 ` Ihor Radchenko
2023-08-20 17:19 ` Alfred M. Szmidt
2023-08-20 17:31 ` Ihor Radchenko
2023-08-20 18:54 ` Alfred M. Szmidt
2023-08-20 19:07 ` Eli Zaretskii
2023-08-27 3:53 ` Emanuel Berg
2023-08-20 19:15 ` Ihor Radchenko
2023-08-20 19:24 ` Ihor Radchenko
2023-08-21 2:33 ` Eli Zaretskii
2023-08-21 4:11 ` Ihor Radchenko
2023-08-21 4:15 ` Po Lu
2023-08-21 4:36 ` Ihor Radchenko
2023-08-21 4:43 ` Po Lu
2023-08-21 5:06 ` Ihor Radchenko
2023-08-21 5:25 ` [External] : " Drew Adams
2023-08-21 5:34 ` Po Lu
2023-08-21 9:17 ` Add more supported primitives in libgccjit IR (was: Shrinking the C core) Ihor Radchenko
2023-08-21 9:42 ` Gregory Heytings
2023-08-21 10:36 ` Ihor Radchenko
2023-08-21 11:02 ` Alfred M. Szmidt
2023-08-21 11:41 ` Ihor Radchenko
2023-08-21 12:20 ` Eli Zaretskii
2023-08-21 14:49 ` Add more supported primitives in libgccjit IR Andrea Corallo
2023-08-23 10:11 ` Ihor Radchenko
2023-08-25 9:19 ` Andrea Corallo
2023-08-25 11:06 ` Ihor Radchenko
2023-08-25 14:26 ` Andrea Corallo
2023-08-26 11:14 ` Ihor Radchenko
2023-08-27 1:40 ` Emanuel Berg
2023-08-27 7:38 ` Emanuel Berg
2023-08-27 13:42 ` Andrea Corallo
2023-08-27 22:19 ` Emanuel Berg
2023-08-28 5:04 ` Andrea Corallo
2023-08-28 19:49 ` Emanuel Berg
2023-08-26 0:47 ` Emanuel Berg
2023-08-26 8:26 ` Ihor Radchenko
2023-08-26 17:52 ` Emanuel Berg
2023-08-21 11:05 ` Add more supported primitives in libgccjit IR (was: Shrinking the C core) Gregory Heytings
2023-08-21 11:46 ` Ihor Radchenko
2023-08-21 12:33 ` Gregory Heytings
2023-08-21 11:34 ` Add more supported primitives in libgccjit IR Manuel Giraud via Emacs development discussions.
2023-08-21 11:02 ` Add more supported primitives in libgccjit IR (was: Shrinking the C core) Alfred M. Szmidt
2023-08-21 11:12 ` Add more supported primitives in libgccjit IR Eli Zaretskii
2023-08-21 11:53 ` Ihor Radchenko
2023-08-27 2:04 ` Shrinking the C core Emanuel Berg
2023-08-21 7:59 ` Gregory Heytings
2023-08-27 5:31 ` Emanuel Berg
2023-08-27 6:16 ` Emanuel Berg
2023-08-21 10:48 ` Eli Zaretskii
2023-08-21 11:56 ` Ihor Radchenko
2023-08-21 12:22 ` Eli Zaretskii
2023-08-28 4:41 ` Emanuel Berg
2023-08-28 11:27 ` Ihor Radchenko
2023-08-20 20:15 ` Alfred M. Szmidt
2023-08-20 20:39 ` Ihor Radchenko
2023-08-21 5:59 ` Alfred M. Szmidt
2023-08-21 6:23 ` Ihor Radchenko
2023-08-21 7:21 ` Alfred M. Szmidt
2023-08-21 7:26 ` Ihor Radchenko
2023-08-21 7:52 ` Alfred M. Szmidt
2023-08-21 10:46 ` Ihor Radchenko
2023-08-21 11:02 ` Alfred M. Szmidt
2023-08-27 4:01 ` Emanuel Berg
2023-08-27 8:53 ` Ihor Radchenko
2023-08-27 3:48 ` Emanuel Berg
2023-08-27 9:06 ` Ihor Radchenko
2023-08-27 3:25 ` Emanuel Berg
2023-08-27 8:55 ` Ihor Radchenko
2023-08-20 16:03 ` Alfred M. Szmidt
2023-08-20 16:34 ` Ihor Radchenko
2023-08-20 17:19 ` Alfred M. Szmidt
2023-08-20 17:25 ` Ihor Radchenko
2023-08-20 18:54 ` Alfred M. Szmidt
2023-08-20 19:02 ` Eli Zaretskii
2023-08-20 20:11 ` Alfred M. Szmidt
2023-08-23 21:09 ` Emanuel Berg
2023-08-26 2:01 ` Richard Stallman
2023-08-26 5:48 ` Eli Zaretskii
2023-08-26 18:15 ` Emanuel Berg
2023-08-26 18:27 ` Eli Zaretskii
2023-08-20 19:14 ` Eli Zaretskii
2023-08-20 19:44 ` Ihor Radchenko
2023-08-20 20:11 ` Alfred M. Szmidt
2023-08-21 2:35 ` Eli Zaretskii
2023-08-21 8:48 ` Ihor Radchenko
2023-08-21 11:10 ` Eli Zaretskii
2023-08-21 11:59 ` Ihor Radchenko
2023-08-21 12:23 ` Eli Zaretskii
2023-08-23 10:13 ` Ihor Radchenko
2023-08-20 20:32 ` Emanuel Berg
2023-08-21 6:19 ` Alfred M. Szmidt
2023-08-21 6:26 ` Ihor Radchenko
2023-08-21 7:21 ` Alfred M. Szmidt
2023-08-21 7:25 ` Ihor Radchenko
2023-08-21 7:52 ` Alfred M. Szmidt
2023-08-21 11:26 ` Ihor Radchenko
2023-08-22 23:55 ` Emanuel Berg
2023-08-23 7:04 ` Alfred M. Szmidt
2023-08-23 17:24 ` Emanuel Berg
2023-08-24 20:02 ` Emanuel Berg
2023-08-20 21:51 ` [External] : " Drew Adams
2023-08-21 8:54 ` Type declarations in Elisp (was: [External] : Re: Shrinking the C core) Ihor Radchenko
2023-08-21 9:30 ` Gerd Möllmann
2023-08-21 11:13 ` Type declarations in Elisp Eli Zaretskii
2023-08-21 11:37 ` Type declarations in Elisp (was: [External] : Re: Shrinking the C core) Ihor Radchenko
2023-08-22 5:34 ` Type declarations in Elisp Gerd Möllmann
2023-08-22 6:16 ` Ihor Radchenko
2023-08-22 11:14 ` Eli Zaretskii
2023-08-22 23:33 ` Emanuel Berg
2023-08-25 9:29 ` Andrea Corallo
2023-08-25 20:42 ` Emanuel Berg
2023-08-27 8:42 ` Ihor Radchenko
2023-08-27 14:04 ` Andrea Corallo
2023-08-27 14:07 ` Ihor Radchenko
2023-08-27 15:46 ` Andrea Corallo
2023-08-27 17:15 ` Ihor Radchenko
2023-08-27 18:06 ` Andrea Corallo
2023-08-28 9:56 ` Ihor Radchenko
2023-08-28 19:06 ` Emanuel Berg
2023-08-18 8:35 ` Shrinking the C core Aurélien Aptel
2023-08-19 13:32 ` Emanuel Berg
2023-08-31 1:41 ` Emanuel Berg
2023-08-14 2:36 ` Richard Stallman
2023-08-14 4:12 ` Emanuel Berg
2023-08-14 11:15 ` Ihor Radchenko
2023-08-12 3:28 ` Christopher Dimech
2023-08-12 3:48 ` Emanuel Berg
2023-08-12 3:50 ` Emanuel Berg
2023-08-12 6:00 ` Christopher Dimech
2023-08-12 6:02 ` Eli Zaretskii
2023-08-12 7:38 ` Christopher Dimech
2023-08-09 12:45 ` Eli Zaretskii
2023-08-09 16:11 ` Eric S. Raymond
2023-08-09 16:44 ` Eli Zaretskii
2023-08-09 17:57 ` Eric S. Raymond
-- strict thread matches above, loose matches on Subject: below --
2023-08-14 6:28 [PATCH] Re: Bignum performance (was: Shrinking the C core) Gerd Möllmann
2023-08-14 6:56 ` Gerd Möllmann
2023-08-14 7:04 ` Ihor Radchenko
2023-08-14 7:35 ` Gerd Möllmann
2023-08-14 8:09 ` Ihor Radchenko
2023-08-14 9:28 ` Gerd Möllmann
2023-08-14 9:42 ` Ihor Radchenko
2023-08-15 14:03 ` Emanuel Berg
2023-08-15 15:01 ` Ihor Radchenko
2023-08-15 22:21 ` Emanuel Berg
2023-08-15 22:33 ` Emanuel Berg
2023-08-16 4:36 ` tomas
2023-08-16 5:23 ` Emanuel Berg
2023-08-14 16:51 ` Emanuel Berg
2023-08-15 4:58 ` Gerd Möllmann
2023-08-15 14:20 ` Emanuel Berg
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=87bkfdsmde.fsf@localhost \
--to=yantar92@posteo.net \
--cc=emacs-devel@gnu.org \
--cc=incal@dataswamp.org \
/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).