unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Andrew Cohen <acohen@ust.hk>
Cc: mattiase@acm.org, emacs-devel@gnu.org
Subject: Re: sorting in C
Date: Sun, 27 Feb 2022 09:28:30 +0200	[thread overview]
Message-ID: <83zgmcojj5.fsf@gnu.org> (raw)
In-Reply-To: <87h78l9h7x.fsf@ust.hk> (message from Andrew Cohen on Sun, 27 Feb 2022 10:27:30 +0800)

> From: Andrew Cohen <acohen@ust.hk>
> Date: Sun, 27 Feb 2022 10:27:30 +0800
> Cc: Mattias Engdegård <mattiase@acm.org>
> 
> 
> Mattias E. reminds me (in a private email):
> >Since Timsort may allocate temporary scratch space it is important to
> >make sure it's freed if the comparison predicate throws. A specbind
> >may be needed for the clean-up, but I haven't looked at your timsort
> >code -- perhaps you have already solved that problem.
> 
> Dealing with the tmp space is my one remaining question. I note that
> when sorting a list of length L, the current (vector) sorting routine
> requires space for a tmp array of length L/2.  It uses SAFE_ALLOCA_LISP
> (and SAFE_FREE) outside the sorting routine and passes a pointer to the
> storage as an argument to =sort_vector_inplace=. This way memory
> management is easy.
> 
> TIMSORT /also/ requires space for a tmp array of length L/2, but only in
> the worst case (random lists). For partially sorted lists it can make do
> with less. So it takes a dynamic approach: it allocates a small amount
> of storage (enough for an array of length 256) which can handle all
> short lists and longer partially sorted lists; and then allocates
> additional storage on the fly as needed for other cases.
> 
> Right now my routine accepts a pointer to tmp space as an argument; if
> this is null, it uses the dynamic allocation, and otherwise just uses
> the pre-allocated storage.
> 
> Clearly the less memory required the better, and for mostly-sorted lists
> the pre-allocated 256 is usually sufficient. This saves the space, and
> any (minimal) time needed to allocate additional space.
> 
> But the early allocation of the maximum space required (as is done for
> the current sorting routine) makes the memory management trivial (which
> is good!) and avoids the (minmal) additional time for allocs for random
> lists.
> 
> I'm inclined to just go with the current system: allocate the maximum
> required before calling the routine, but welcome any advice or
> expressions of preference for the dynamic allocation. 

It is okay to use the existing scheme, since it will at worst have the
same limitation as the existing one: when the list or vector to sort
is very large, you might run out of memory trying to allocate L/2-size
array.

However, from your description, it doesn't sound like the more optimal
approach of allocating dynamically is much more complicated.  In
particular, what Mattias said should be easy using the unwind-protect
machinery we already have (and use in many similar situations).  See
the calls to record_unwind_protect_ptr whose first argument is
'xfree'.  We also have reallocation routines ready to be used.

You could also start with the existing scheme and then add the dynamic
allocation as a followup patch.

Bottom line: I think it's up to you.

Thanks.



  reply	other threads:[~2022-02-27  7:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-22  2:52 sorting in C Andrew Cohen
2022-02-22 12:30 ` Eli Zaretskii
2022-02-22 12:54   ` Andrew Cohen
2022-02-22 13:11     ` Eli Zaretskii
2022-02-23  4:14   ` Andrew Cohen
2022-02-23 12:34     ` Eli Zaretskii
2022-02-23 12:53       ` Andrew Cohen
2022-02-23 13:14         ` Eli Zaretskii
2022-02-23 13:52           ` Andrew Cohen
2022-02-23 14:06           ` Andrew Cohen
2022-02-23 14:18             ` Eli Zaretskii
2022-02-26 23:54               ` Andrew Cohen
2022-02-27  2:27                 ` Andrew Cohen
2022-02-27  7:28                   ` Eli Zaretskii [this message]
2022-02-27  9:11                     ` Andrew Cohen
2022-02-27  9:29                       ` Eli Zaretskii
2022-02-27 10:42                         ` Andrew Cohen
2022-03-04  0:13                           ` Andrew Cohen
2022-03-04  7:05                             ` Eli Zaretskii
2022-02-23 13:19         ` Yuri Khan
2022-02-23 14:12           ` Andrew Cohen
2022-02-22 13:12 ` Lars Ingebrigtsen

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=83zgmcojj5.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=acohen@ust.hk \
    --cc=emacs-devel@gnu.org \
    --cc=mattiase@acm.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).