Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Commits on Apr 16, 2015
  1. @peff @gitster

    strbuf_getwholeline: use getdelim if it is available

    peff authored gitster committed
    We spend a lot of time in strbuf_getwholeline in a tight
    loop reading characters from a stdio handle into a buffer.
    The libc getdelim() function can do this for us with less
    overhead. It's in POSIX.1-2008, and was a GNU extension
    before that. Therefore we can't rely on it, but can fall
    back to the existing getc loop when it is not available.
    
    The HAVE_GETDELIM knob is turned on automatically for Linux,
    where we have glibc. We don't need to set any new
    feature-test macros, because we already define _GNU_SOURCE.
    Other systems that implement getdelim may need to other
    macros (probably _POSIX_C_SOURCE >= 200809L), but we can
    address that along with setting the Makefile knob after
    testing the feature on those systems.
    
    Running "git rev-parse refs/heads/does-not-exist" on a repo
    with an extremely large (1.6GB) packed-refs file went from
    (best-of-5):
    
      real    0m8.601s
      user    0m8.084s
      sys     0m0.524s
    
    to:
    
      real    0m6.768s
      user    0m6.340s
      sys     0m0.432s
    
    for a wall-clock speedup of 21%.
    
    Based on a patch from Rasmus Villemoes <rv@rasmusvillemoes.dk>.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  2. @peff @gitster

    strbuf_getwholeline: avoid calling strbuf_grow

    peff authored gitster committed
    As with the recent speedup to strbuf_addch, we can avoid
    calling strbuf_grow() in a tight loop of single-character
    adds by instead checking strbuf_avail.
    
    Note that we would instead call strbuf_addch directly here,
    but it does more work than necessary: it will NUL-terminate
    the result for each character read. Instead, in this loop we
    read the characters one by one and then add the terminator
    manually at the end.
    
    Running "git rev-parse refs/heads/does-not-exist" on a repo
    with an extremely large (1.6GB) packed-refs file went from
    (best-of-5):
    
      real    0m10.948s
      user    0m10.548s
      sys     0m0.412s
    
    to:
    
      real    0m8.601s
      user    0m8.084s
      sys     0m0.524s
    
    for a wall-clock speedup of 21%.
    
    Helped-by: Eric Sunshine <sunshine@sunshineco.com>
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  3. @peff @gitster

    strbuf_getwholeline: use getc_unlocked

    peff authored gitster committed
    strbuf_getwholeline calls getc in a tight loop. On modern
    libc implementations, the stdio code locks the handle for
    every operation, which means we are paying a significant
    overhead.  We can get around this by locking the handle for
    the whole loop and using the unlocked variant.
    
    Running "git rev-parse refs/heads/does-not-exist" on a repo
    with an extremely large (1.6GB) packed-refs file went from:
    
      real    0m18.900s
      user    0m18.472s
      sys     0m0.448s
    
    to:
    
      real    0m10.953s
      user    0m10.384s
      sys     0m0.580s
    
    for a wall-clock speedup of 42%. All times are best-of-3,
    and done on a glibc 2.19 system.
    
    Note that we call into strbuf_grow while holding the lock.
    It's possible for that function to call other stdio
    functions (e.g., printing to stderr when dying due to malloc
    error); however, the POSIX.1-2001 definition of flockfile
    makes it clear that the locks are per-handle, so we are fine
    unless somebody else tries to read from our same handle.
    This doesn't ever happen in the current code, and is
    unlikely to be added in the future (we would have to do
    something exotic like add a die_routine that tried to read
    from stdin).
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  4. @peff @gitster

    strbuf_getwholeline: use getc macro

    peff authored gitster committed
    strbuf_getwholeline calls fgetc in a tight loop. Using the
    getc form, which can be implemented as a macro, should be
    faster (and we do not care about it evaluating our argument
    twice, as we just have a plain variable).
    
    On my glibc system, running "git rev-parse
    refs/heads/does-not-exist" on a file with an extremely large
    (1.6GB) packed-refs file went from (best of 3 runs):
    
      real    0m19.383s
      user    0m18.876s
      sys     0m0.528s
    
    to:
    
      real    0m18.900s
      user    0m18.472s
      sys     0m0.448s
    
    for a wall-clock speedup of 2.5%.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Jun 30, 2014
  1. @peff @gitster

    implement ends_with via strip_suffix

    peff authored gitster committed
    The ends_with function is essentially a simplified version
    of strip_suffix, in which we throw away the stripped length.
    Implementing it as an inline on top of strip_suffix has two
    advantages:
    
      1. We save a bit of duplicated code.
    
      2. The suffix is typically a string literal, and we call
         strlen on it. By making the function inline, many
         compilers can replace the strlen call with a constant.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Jun 19, 2014
  1. @peff @gitster

    strbuf: add xstrfmt helper

    peff authored gitster committed
    You can use a strbuf to build up a string from parts, and
    then detach it. In the general case, you might use multiple
    strbuf_add* functions to do the building. However, in many
    cases, a single strbuf_addf is sufficient, and we end up
    with:
    
      struct strbuf buf = STRBUF_INIT;
      ...
      strbuf_addf(&buf, fmt, some, args);
      str = strbuf_detach(&buf, NULL);
    
    We can make this much more readable (and avoid introducing
    an extra variable, which can clutter the code) by
    introducing a convenience function:
    
      str = xstrfmt(fmt, some, args);
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on May 27, 2014
  1. @peff @gitster

    strbuf: add strbuf_reencode helper

    peff authored gitster committed
    This is a convenience wrapper around `reencode_string_len`
    and `strbuf_attach`.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on May 23, 2014
  1. @peff @gitster

    strbuf: add strbuf_tolower function

    peff authored gitster committed
    This is a convenience wrapper to call tolower on each
    character of the string.
    
    This makes config's lowercase() function obsolete, though
    note that because we have a strbuf, we are careful to
    operate over the whole strbuf, rather than assuming that a
    NUL is the end-of-string.
    
    We could continue to offer a pure-string lowercase, but
    there would be no callers (in most pure-string cases, we
    actually duplicate and lowercase the duplicate, for which we
    have the xstrdup_tolower wrapper).
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  2. @peff @gitster

    daemon/config: factor out duplicate xstrdup_tolower

    peff authored gitster committed
    We have two implementations of the same function; let's drop
    that to one. We take the name from daemon.c, but the
    implementation (which is just slightly more efficient) from
    the config code.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Oct 25, 2012
  1. @peff

    Merge branch 'jk/strbuf-detach-always-non-null'

    peff authored
    * jk/strbuf-detach-always-non-null:
      strbuf: always return a non-NULL value from strbuf_detach
Commits on Oct 18, 2012
  1. @peff @gitster

    strbuf: always return a non-NULL value from strbuf_detach

    peff authored gitster committed
    The current behavior is to return NULL when strbuf did not
    actually allocate a string. This can be quite surprising to
    callers, though, who may feed the strbuf from arbitrary data
    and expect to always get a valid value.
    
    In most cases, it does not make a difference because calling
    any strbuf function will cause an allocation (even if the
    function ends up not inserting any data). But if the code is
    structured like:
    
      struct strbuf buf = STRBUF_INIT;
      if (some_condition)
    	  strbuf_addstr(&buf, some_string);
      return strbuf_detach(&buf, NULL);
    
    then you may or may not return NULL, depending on the
    condition. This can cause us to segfault in http-push
    (when fed an empty URL) and in http-backend (when an empty
    parameter like "foo=bar&&" is in the $QUERY_STRING).
    
    This patch forces strbuf_detach to allocate an empty
    NUL-terminated string when it is called on a strbuf that has
    not been allocated.
    
    I investigated all call-sites of strbuf_detach. The majority
    are either not affected by the change (because they call a
    strbuf_* function unconditionally), or can handle the empty
    string just as easily as NULL.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Dec 13, 2011
  1. @peff @gitster

    strbuf: add strbuf_add*_urlencode

    peff authored gitster committed
    This just follows the rfc3986 rules for percent-encoding
    url data into a strbuf.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Jun 22, 2011
  1. @peff @gitster

    strbuf: allow strbuf_split to work on non-strbufs

    peff authored gitster committed
    The strbuf_split function takes a strbuf as input, and
    outputs a list of strbufs. However, there is no reason that
    the input has to be a strbuf, and not an arbitrary buffer.
    
    This patch adds strbuf_split_buf for a length-delimited
    buffer, and strbuf_split_str for NUL-terminated strings.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  2. @peff @gitster

    strbuf_split: add a max parameter

    peff authored gitster committed
    Sometimes when splitting, you only want a limited number of
    fields, and for the final field to contain "everything
    else", even if it includes the delimiter.
    
    This patch introduces strbuf_split_max, which provides a
    "max number of fields" parameter; it behaves similarly to
    perl's "split" with a 3rd field.
    
    The existing 2-argument form of strbuf_split is retained for
    compatibility and ease-of-use.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Feb 26, 2011
  1. @peff @gitster

    strbuf: add strbuf_vaddf

    peff authored gitster committed
    In a variable-args function, the code for writing into a strbuf is
    non-trivial. We ended up cutting and pasting it in several places
    because there was no vprintf-style function for strbufs (which in turn
    was held up by a lack of va_copy).
    
    Now that we have a fallback va_copy, we can add strbuf_vaddf, the
    strbuf equivalent of vsprintf. And we can clean up the cut and paste
    mess.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Improved-by: Christian Couder <christian.couder@gmail.com>
    Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Commits on Jan 14, 2010
  1. @peff @gitster

    strbuf: add strbuf_addbuf_percentquote

    peff authored gitster committed
    This is handy for creating strings which will be fed to printf() or
    strbuf_expand().
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
  2. @peff @gitster

    strbuf_expand: convert "%%" to "%"

    peff authored gitster committed
    The only way to safely quote arbitrary text in a pretty-print user
    format is to replace instances of "%" with "%x25". This is slightly
    unreadable, and many users would expect "%%" to produce a single
    "%", as that is what printf format specifiers do.
    
    This patch converts "%%" to "%" for all users of strbuf_expand():
    
     (1) git-daemon interpolated paths
    
     (2) pretty-print user formats
    
     (3) merge driver command lines
    
    Case (1) was already doing the conversion itself outside of
    strbuf_expand(). Case (2) is the intended beneficiary of this patch.
    Case (3) users probably won't notice, but as this is user-facing
    behavior, consistently providing the quoting mechanism makes sense.
    
    Signed-off-by: Jeff King <peff@peff.net>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
Something went wrong with that request. Please try again.