public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Carlos O'Donell <carlos@redhat.com>
To: Paul Eggert <eggert@cs.ucla.edu>,
	Florian Weimer <fweimer@redhat.com>,
	GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH] Dynamic growable arrays for internal use
Date: Thu, 01 Jun 2017 16:13:00 -0000	[thread overview]
Message-ID: <4ed5035d-3b88-0030-b3b4-b7e4e2e13e88@redhat.com> (raw)
In-Reply-To: <f7536497-ab89-28d1-c565-f9a2baf1cffd@cs.ucla.edu>

On 05/20/2017 08:43 PM, Paul Eggert wrote:
> Carlos O'Donell wrote:
>> The dynamic array growth factor appears to be a value of 2, and
>> that doesn't match best practice nor academic literature. I would
>> be happier with a growth factor closer to 1.5.
> 
> For what it's worth, Gnulib (which has supported dynamically
> growabable arrays for some time) generates the new size via "n += n /
> 2 + 1;" by default. This guarantees growth even if N is zero, and
> approximates the growth factor of 1.5 that you're suggesting. (The
> code also checks for integer overflow of course.) The caller can ask
> for a different amount of growth, if necessary.

The growth factor is related to the underlying storage being used by 
the dynamic array. If the storage had such properties that the array
could be grown in-place and to any size with zero cost, then the growth
could be incredibly small and size kept optimal. Since no underlying storage
has those properties at all times we choose a growth factor that maps to the
properties that are present. Here, those properties are a function
of the underlying system allocator, how it calls mmap, when, how often,
and the costs associated with it. In general I'm surprised to see that 1.5
is a universally good constant, then again, most OS have similar subsystems
when it comes to allocators and virtual memory.

I don't know that having the caller ask for different growth is going to be
initially useful in glibc so I'm not going to ask for this in the API.

>> Can we make the choice to have dynarray always heap allocated?
> 
> In Gnulib we've found it better to support initial buffers on the
> stack, as an option. Commonly these initial buffers are already big
> enough, which is a performance win in the typical case.

OK. Thanks for the data point here.

>> I dont like defaults for this interface. I think the user should 
>> explicitly request a size or this should fail.
> 
> Why impose this requirement? In Gnulib the caller can specify an
> initial size, but this is optional. The default initial size is
> chosen to be efficient for the underlying malloc implementation. It
> would seem odd to require callers to know this implementation
> detail.

I see your point. Having the caller convey that no information is relevant
in the context allows the implementation to choose something optimal given
the backing store. And we have some use cases like this in the loader.
At most we know we have say one link map, but how many more there are after
is completely unknown (unless we had statistical data to guide us).
 
> As a general point, I suggest looking at Gnulib's lib/xalloc.h and
> lib/xmalloc.c for ideas. It has many of the ideas of the proposal
> (including some type safety). It's not as fancy as the proposal, but
> it does have the advantage of widespread use and implementation
> experience.
> 
> http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/xalloc.h 
> http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/xmalloc.c

Before I look...

What license do these files have?

-- 
Cheers,
Carlos.

  reply	other threads:[~2017-06-01 16:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-22 15:46 Florian Weimer
2017-04-24 14:06 ` Joseph Myers
2017-05-05 11:48   ` Florian Weimer
2017-05-05 15:13     ` Paul Eggert
2017-05-05 15:23       ` Florian Weimer
2017-05-05 22:31         ` Paul Eggert
2017-05-20  2:01 ` Carlos O'Donell
2017-05-21  0:43   ` Paul Eggert
2017-06-01 16:13     ` Carlos O'Donell [this message]
2017-06-01 17:09       ` Jeff Law
2017-06-01 20:21         ` Florian Weimer
2017-06-01 21:13           ` Adhemerval Zanella
2017-06-01 18:08       ` Paul Eggert
2017-05-30 12:48   ` Florian Weimer
2017-06-02  7:37     ` Carlos O'Donell
2017-06-02 10:04       ` Florian Weimer
2017-06-06 15:30       ` Stefan Liebler
2017-06-06 15:46         ` H.J. Lu
2017-06-07  9:54           ` Florian Weimer
2017-06-07  9:41         ` Florian Weimer
2017-06-07 14:41           ` Stefan Liebler
2017-06-07 18:56             ` Florian Weimer

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ed5035d-3b88-0030-b3b4-b7e4e2e13e88@redhat.com \
    --to=carlos@redhat.com \
    --cc=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.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.
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).