unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: 65491@debbugs.gnu.org
Subject: bug#65491: [PATCH] Improve performance allocating vectors
Date: Sun, 27 Aug 2023 12:21:30 -0400	[thread overview]
Message-ID: <jwvsf8479sv.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87y1i0iwvu.fsf@localhost> (Ihor Radchenko's message of "Thu, 24 Aug 2023 09:59:33 +0000")

> This patch adds a heuristic that reduces the time spent searching
> `vector_free_lists' when trying to allocate a new vector.

The microbenchmarks give surprisingly good performance improvements.

> `vector_free_lists' is a rather long array with few hundreds of
> elements. And it does not make sense to check the whole array in
> `allocate_vector_from_block' if we can get information about free vector
> that was recently made available of if we know for sure that no free
> vectors are available (after GC).

Hmm... after GC we should usually have a non-zero number of free vectors
available (the unused parts of vector blocks which could not be `free`d
because they still contain a live vector), no?
[ See comment below.  ]

> The described approach may sometimes miss free vectors in
> `vector_free_lists', especially if the allocation happens from larger
> vector to smaller.

Right, it could lead to an increase in fragmentation, tho it might tend
to allocate temporally related objects together, which might be beneficial.

> - pack-unpack  0.40±0.00 -> 0.35±0.01

Interesting.  I didn't expect such a large effect there.

> @@ -3145,6 +3145,7 @@ large_vector_vec (struct large_vector *p)
>  
>  static struct Lisp_Vector *vector_free_lists[VECTOR_MAX_FREE_LIST_INDEX];
>  
> +static ptrdiff_t last_known_vector_free_idx = VECTOR_MAX_FREE_LIST_INDEX;
>  /* Singly-linked list of large vectors.  */
>  
>  static struct large_vector *large_vectors;

There's clearly some spacing issue with the following comment but more
importantly the new var would need a good comment explaining what the
variable should hold and why it's useful, so we know when it's safe and
desirable to set or use the var.

> @@ -3180,6 +3181,7 @@ 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;
> +  last_known_vector_free_idx = vindex;
>  }
>  
>  /* Get a new vector block.  */
> @@ -3234,7 +3236,7 @@ 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);
> +  for (index = max (VINDEX (nbytes + VBLOCK_BYTES_MIN), last_known_vector_free_idx);
>         index < VECTOR_MAX_FREE_LIST_INDEX; index++)
>      if (vector_free_lists[index])
>        {

IIUC that's the core of your patch.  Nice.

> @@ -3426,6 +3428,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));
> +  last_known_vector_free_idx = VECTOR_MAX_FREE_LIST_INDEX;
>  
>    /* Looking through vector blocks.  */

Hmm... so I was wrong and after GC there are aren't any free vectors?
I need to go re-read that code, then, because it doesn't match my mental
model of how it work(s|ed).


        Stefan






  parent reply	other threads:[~2023-08-27 16:21 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24  9:59 bug#65491: [PATCH] Improve performance allocating vectors Ihor Radchenko
2023-08-26  7:14 ` Eli Zaretskii
2023-08-26  7:27   ` Ihor Radchenko
2023-08-26  7:31     ` Eli Zaretskii
2023-08-26  7:51       ` Ihor Radchenko
2023-08-26  8:07         ` Ihor Radchenko
2023-08-26  9:01         ` Eli Zaretskii
2023-08-26  7:47     ` Ihor Radchenko
2023-08-26 12:01 ` Mattias Engdegård
2023-08-26 14:54   ` Ihor Radchenko
2023-08-26 14:55   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-27  9:54     ` Mattias Engdegård
2023-09-16 14:58     ` Mattias Engdegård
2023-09-16 16:12       ` Eli Zaretskii
2023-09-16 16:17         ` Eli Zaretskii
2023-09-16 16:32           ` Mattias Engdegård
2023-09-16 16:54             ` Eli Zaretskii
2023-09-16 17:03               ` Mattias Engdegård
2023-09-16 17:11                 ` Eli Zaretskii
2023-09-17  3:02                 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-17 17:02                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-18  2:19                     ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-18  2:27                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-18  3:08                         ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-18  4:10                           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-16 16:54             ` Mattias Engdegård
2023-09-16 17:09               ` Eli Zaretskii
2023-09-16 17:22                 ` Mattias Engdegård
2023-09-16 18:19                   ` Eli Zaretskii
2023-09-16 19:04                     ` Mattias Engdegård
2023-09-16 19:46                 ` Paul Eggert
2023-09-17  5:18                   ` Eli Zaretskii
2023-09-17 15:22                     ` Paul Eggert
2023-09-17 16:15                       ` Eli Zaretskii
2023-09-17 16:37                         ` Paul Eggert
2023-09-17 16:44                           ` Eli Zaretskii
2023-09-18 16:10                             ` Mattias Engdegård
2023-09-18 17:13                               ` Eli Zaretskii
2023-09-19 13:28                               ` Mattias Engdegård
2023-09-19 14:04                                 ` Eli Zaretskii
2023-09-19 14:05                                   ` Mattias Engdegård
2023-09-25 16:06       ` Mattias Engdegård
2023-08-27 16:21 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-08-28 10:14   ` Ihor Radchenko
2023-08-28 16:32     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-28 12:47   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=jwvsf8479sv.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=65491@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=yantar92@posteo.net \
    /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).