git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 01/19] add strbuf_set operations

From
Jeremiah Mahler <jmmahler@gmail.com>
Date
Jun 12, 2014, 23:59 UTC
Message-ID
<20140612235953.GC17803@hudson.localdomain>
In-Reply-To
<53997F3C.9010608@alum.mit.edu>
Michael,
On Thu, Jun 12, 2014 at 12:21:48PM +0200, Michael Haggerty wrote:
Show 24 quoted lines
> On 06/12/2014 09:10 AM, Jeremiah Mahler wrote:
> > On Wed, Jun 11, 2014 at 02:05:35PM +0200, Michael Haggerty wrote:
> >> [...]
> >> If there were a function like strbuf_grow_to(sb, len):
> >>
> >> void strbuf_grow_to(struct strbuf *sb, size_t len)
> >> {
> >> 	int new_buf = !sb->alloc;
> >> 	if (unsigned_add_overflows(len, 1))
> >> 		die("you want to use way too much memory");
> >> 	if (new_buf)
> >> 		sb->buf = NULL;
> >> 	ALLOC_GROW(sb->buf, len + 1, sb->alloc);
> >> 	if (new_buf)
> >> 		sb->buf[0] = '\0';
> >> }
> >>
> > grow_to() which could reduce in size, interesting.
> 
> I don't understand what you mean by "could reduce in size".  This
> function can only grow, never reduce, the size of the strbuf, because
> ALLOC_GROW doesn't do anything unless the requested size is larger than
> the currently-allocated size.
> 

I got it now. strbuf_grow() adds extra to the current buf. strbuf_grow_to() will add more if len is larger than the current length.

Show 33 quoted lines
> >> (strbuf_grow() could call it:
> >>
> >> static inline void strbuf_grow(struct strbuf *sb, size_t extra)
> >> {
> >> 	if (unsigned_add_overflows(sb->len, extra))
> >> 		die("you want to use way too much memory");
> >> 	strbuf_grow_to(sb, sb->len + extra);
> >> }
> >>
> >> ), then your function could be minimized to
> >>
> >> void strbuf_set(struct strbuf *sb, const void *data, size_t len)
> >> {
> >> 	strbuf_grow_to(sb, len);
> >> 	memcpy(sb->buf, data, len);
> >> 	strbuf_setlen(sb, len);
> >> }
> >>
> >> I think strbuf_grow_to() would be useful in other situations too.
> >>
> >> This is just an idea; I'm not saying that you have to make this change.
> >>
> > I like your idea.  I am leaning towards doing the un-optimized
> > strbuf_set operations first, then optimizing in a later patch.
> 
> That's a good plan.
> 
> In case you're interested, I sketched what the strbuf_grow_to() changes
> might look like, and also looked for other places in the code where
> strbuf_grow_to() could be used instead of strbuf_grow() [1].  This is
> only a sketch; I haven't even tested the result.  Feel free to use what
> you want from it.
> 

I started looking at it. I will let you know if I get something together.

Show 8 quoted lines
> Michael
> 
> [1] Branch "strbuf-grow-to-sketch" on https://github.com/mhagger/git
> 
> -- 
> Michael Haggerty
> mhagger@alum.mit.edu
> http://softwareswirl.blogspot.com/
-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler
Previous: Michael HaggertyNext: Jeremiah Mahler
Message 8 of 28 in “add strbuf_set operations”
  1. 00/19 add strbuf_set operationsJeremiah Mahler, Jun 9, 2014
  2. 01/19 add strbuf_set operationsJeremiah Mahler, Jun 9, 2014
  3. Eric SunshineJun 11, 2014
  4. Jeremiah MahlerJun 12, 2014
  5. Michael HaggertyJun 11, 2014
  6. Jeremiah MahlerJun 12, 2014
  7. Michael HaggertyJun 12, 2014
  8. Jeremiah MahlerJun 12, 2014
  9. 02/19 sha1_name: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  10. 03/19 fast-import: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  11. 04/19 builtin/remote: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  12. 05/19 branch: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  13. 06/19 builtin/branch: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  14. 07/19 builtin/checkout: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  15. 08/19 builtin/mailinfo: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  16. 09/19 builtin/tag: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  17. 10/19 date: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  18. 11/19 diffcore-order: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  19. 12/19 http-backend: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  20. 13/19 http: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  21. 14/19 ident: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  22. 15/19 remote-curl: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  23. 16/19 submodule: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  24. 17/19 transport: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  25. 18/19 vcs-svn/svndump: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  26. 19/19 wt-status: simplify via strbuf_set()Jeremiah Mahler, Jun 9, 2014
  27. Eric SunshineJun 11, 2014
  28. Jeremiah MahlerJun 12, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.