git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* strbuf API
@ 2007-09-02 22:42 Pierre Habouzit
  2007-09-03  5:43 ` Johan Herland
                   ` (3 more replies)
  0 siblings, 4 replies; 90+ messages in thread
From: Pierre Habouzit @ 2007-09-02 22:42 UTC (permalink / raw)
  To: git

[-- Attachment #1: Type: text/plain, Size: 3217 bytes --]


  I read the recent thread Timo Sirainen raised about string APIs in
git. ANd I went read the strbuf.[hc] module.

  I believe that the choice made in that module are wrong and could be
made better. I actually use to work with a string buffer API (that
interested readers can look at on [0]), that work almost the same
(except for the eof flag, but it's trivial to keep), but have
two significant differences:

  First, and that's the most important one: the buffer is always NUL
terminated, after its official "len". That means, in terms of strbuf
API, that "alloc" is always greater or equal to len+1 to be able to
store the ending NUL[1]. The advantages are obvious: you can pass the
buffer to any legacy C string function without any fear of read
overflow.  strtol comes to mind, and atm, git has to explicitely use
strbuf_end to put that ending nul to be able to call legacy
applications. But once done, the NUL is accounted into the string (aka
it's in "len") which makes it a non C-string (I mean you cannot append
any more data in it anymore). So current implementations tries to
workaround an issue (the non systematical NUL-termination) but IMHO the
wrong way.

  The other shortcoming is that you cannot tell the buffer "Hey, it's
very likely that you'll end up being _that_ long. That's why, in some
parts of the code (see write_tar_entry in archive-tar.c e.g.) the
programmer actually messes with the buffer allocation, outside from the
strbuf module, which makes it well, useless. In my API, I have a
"buffer_ensure" call, that is supposed to do that: "please ensure that
this buffer still has _this_ amount of free and allocated space to put
more data".


  So my question is, do people think I raise a valid point, and would
patches that would refactor the strbuf module to have those functions,
and would fix the code that uses strbuf's to interact properly, be
accepted ?

  Also, the efficiency of the buffer module API I use has a lot to do
with the fact that copying functions, and length tests are inlined in
the .h, so that the compiler can optimize the ones it already tested 10
calls before. I'm not sure if this is frowned upon or if it makes sense.


  [0] http://git.madism.org/?p=pfixtools.git;a=blob;f=buffer.h;hb=HEAD
      http://git.madism.org/?p=pfixtools.git;a=blob;f=buffer.c;hb=HEAD

  [1] Of course, ensuring the NUL-termination has a cost, though it's
      often benign, and for performance-critical places where characters
      are copied one by one, it's always possible to use an "unsafe"
      addch (that would not maintain the invariant), and then call an
      equivalent of strbuf_end (that would not append a \0 like it does
      now, but just would fix the invariant that for any strbuf,
      buf->buf[buf->len] == '\0') explicitely. For places where the
      invariant generate negligible cost (like concatenating two paths
      parts with a middle '/' e.g.) then we gain safety without even
      having to think about it.

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 90+ messages in thread

end of thread, other threads:[~2007-09-19  8:37 UTC | newest]

Thread overview: 90+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-02 22:42 strbuf API Pierre Habouzit
2007-09-03  5:43 ` Johan Herland
2007-09-03  8:46   ` Pierre Habouzit
2007-09-04  1:52     ` Miles Bader
2007-09-04  8:47       ` strbuf new semantics, let's give it a try Pierre Habouzit
2007-09-04  8:47       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-04 11:11         ` Johannes Schindelin
2007-09-04 11:53           ` Pierre Habouzit
2007-09-04 13:34             ` Andreas Ericsson
2007-09-04 14:01             ` Pierre Habouzit
2007-09-04 15:44               ` Johannes Schindelin
2007-09-04 16:18                 ` Pierre Habouzit
2007-09-04 17:18                   ` Wincent Colaiuta
2007-09-04 14:01             ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions Pierre Habouzit
2007-09-04 23:46               ` René Scharfe
2007-09-04 23:46               ` René Scharfe
2007-09-05  7:48                 ` Pierre Habouzit
2007-09-05  8:05                   ` Junio C Hamano
2007-09-05  8:57                     ` Pierre Habouzit
2007-09-05 19:18                       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-05 19:18                         ` [PATCH] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-05 19:18                           ` [PATCH] Simplify strbuf uses in fast-import.c " Pierre Habouzit
2007-09-05 19:18                             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05 19:18                               ` [PATCH] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-05 19:18                                 ` [PATCH] Further strbuf re-engineering Pierre Habouzit
2007-09-05 19:18                                   ` [PATCH] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-05 19:18                                     ` [PATCH] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-19  8:05                                   ` [PATCH] Further strbuf re-engineering Junio C Hamano
2007-09-05 19:21                             ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions Pierre Habouzit
2007-09-19  8:06                           ` [PATCH] Simplify strbuf uses in archive-tar.c " Junio C Hamano
2007-09-19  8:36                             ` Pierre Habouzit
2007-09-06  9:31                         ` [PATCH] Rework strbuf API and semantics Junio C Hamano
2007-09-06  9:49                           ` Pierre Habouzit
2007-09-06 10:03                         ` Junio C Hamano
2007-09-06 10:22                           ` Pierre Habouzit
2007-09-04 14:01             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05  4:44             ` [PATCH] Rework strbuf API and semantics Miles Bader
2007-09-04  8:48       ` [PATCH] Add strbuf_fread, use it in fast-import.c Pierre Habouzit
2007-09-03  8:32 ` strbuf API Matthieu Moy
2007-09-03  8:49   ` Pierre Habouzit
2007-09-03  9:02     ` Matthieu Moy
2007-09-03  9:18     ` Junio C Hamano
2007-09-03 11:53       ` Pierre Habouzit
2007-09-03 12:29       ` Johannes Schindelin
2007-09-06 11:20 ` strbuf new API, take 2 for inclusion Pierre Habouzit
2007-09-06 11:20   ` [PATCH 1/7] Rework strbuf API and semantics Pierre Habouzit
2007-09-06 11:20     ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-06 11:20       ` [PATCH 3/7] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-06 11:20         ` [PATCH 4/7] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-06 11:20           ` [PATCH 5/7] Further strbuf re-engineering Pierre Habouzit
2007-09-06 11:20             ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-06 11:20               ` [PATCH 7/7] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-06 14:05               ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Johannes Schindelin
2007-09-06 17:17                 ` Pierre Habouzit
2007-09-06 20:16                   ` David Kastrup
2007-09-06 20:54                     ` Pierre Habouzit
2007-09-07  8:03                   ` Junio C Hamano
2007-09-07  9:02                     ` Pierre Habouzit
2007-09-06 17:59       ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Kristian Høgsberg
2007-09-06 18:08         ` Pierre Habouzit
2007-09-06 18:18           ` Kristian Høgsberg
2007-09-06 18:27             ` Pierre Habouzit
2007-09-06 22:54               ` René Scharfe
2007-09-06 14:09     ` [PATCH 1/7] Rework strbuf API and semantics Johannes Schindelin
2007-09-06 14:21       ` Jeff King
2007-09-06 14:44         ` David Kastrup
2007-09-06 14:50           ` Jeff King
2007-09-06 15:06             ` David Kastrup
2007-09-06 15:36               ` Jeff King
2007-09-06 15:53                 ` David Kastrup
2007-09-06 15:45               ` Johannes Sixt
2007-09-06 14:43       ` David Kastrup
2007-09-06 14:52         ` Jeff King
2007-09-06 17:49     ` Kristian Høgsberg
2007-09-06 12:58   ` strbuf new API, take 2 for inclusion Jeff King
2007-09-06 17:15     ` Pierre Habouzit
2007-09-06 17:16       ` Jeff King
2007-09-06 17:19         ` Pierre Habouzit
2007-09-08 11:53 ` Use strbufs in commit.c (pretty printing) Pierre Habouzit
2007-09-08 11:53   ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) Pierre Habouzit
2007-09-08 11:53     ` [PATCH 2/3] Change semantics of interpolate to work like snprintf Pierre Habouzit
2007-09-08 11:53       ` [PATCH 3/3] Rework pretty_print_commit to use strbufs instead of custom buffers Pierre Habouzit
2007-09-08 11:59         ` David Kastrup
2007-09-08 12:17           ` Pierre Habouzit
2007-09-08 12:28           ` Pierre Habouzit
2007-09-08 18:40         ` René Scharfe
2007-09-08 18:49           ` Pierre Habouzit
2007-09-08 16:18     ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) René Scharfe
2007-09-08 22:53       ` Pierre Habouzit
2007-09-08 23:44         ` Pierre Habouzit

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.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).