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

Re: [PATCH] bisect: save heap memory. allocate only the required amount

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 25, 2014, 21:36 UTC
Message-ID
<xmqq8umcl0al.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20140825130732.GD17288@peff.net>
Jeff King <peff@peff.net> writes:
Show 9 quoted lines
>> The string will always be "dist=" followed by decimal representation of
>> a count that fits in "int" anyway, so I actually think use of strbuf is way
>> overkill (and formatting it twice also is); the patch as posted should be
>> just fine.
>
> I think you are right, and the patch is the right direction (assuming we
> want to do this; I question whether there are enough elements in the
> list for us to care about the size, and if there are, we are probably
> better off storing the int and formatting the strings on the fly).
;-)
Show 11 quoted lines
> It would be nice if there was some way to abstract the idea of
> formatting a buffer directly into a flex-array. That would involve the
> double-format you mention, but we could use it in lots of places to make
> the code nicer....
> ...
>   struct name_decoration *r = fmt_flex_array(sizeof(*r),
>                                              offsetof(*r, name),
> 					     "dist=%d", x);
>
> which is a little less nice. You could make it nicer with a macro, but
> we don't assume variadic macros. <sigh>

At first I thought "Yuck. A helper only to format into the flex member that holds a string?", and I tried to change my mind, but I couldn't quite convince myself. At least not yet.

Among the flex arrays we use, some are arrays of bools, some others are arrays of object names, and there are many arrays of even more esoteric types. Only a small number of them are "We want a struct with a constant string, and we do not want to do two allocations and to pay an extra dereference cost by using 'const char *'".

For them, by the time we allocate a struct, by definition we should have sufficient information to compute how large to make that structure and a printf-format plus its args would be the preferred form of that "sufficient information", I would think.

The name "fmt_flex_array()", which stresses too much on the "formatting" side without implying that it is the way to allocate the thing, may be horrible, and I agree with you that without variadic macros the end result may not read very well. Unless we have great many number of places we can use the helper to make the code to create these objects look nicer, I am afraid that the pros-and-cons may not be very favourable.

Thanks for an interesting tangent.
Previous: Jeff KingNext: Jeff King
Message 5 of 32 in “bisect: save heap memory. allocate only the required amount”
  1. bisect: save heap memory. allocate only the required amountArjun Sreedharan, Aug 24, 2014
  2. Stefan BellerAug 24, 2014
  3. Junio C HamanoAug 24, 2014
  4. Jeff KingAug 25, 2014
  5. Junio C HamanoAug 25, 2014
  6. Jeff KingAug 26, 2014
  7. Ramsay JonesAug 26, 2014
  8. Jeff KingAug 26, 2014
  9. Ramsay JonesAug 26, 2014
  10. Jeff KingAug 26, 2014
  11. Ramsay JonesAug 26, 2014
  12. Ramsay JonesAug 24, 2014
  13. bisect: save heap memory. allocate only the required amountArjun Sreedharan, Aug 24, 2014
  14. Jeff KingAug 25, 2014
  15. Christian CouderAug 25, 2014
  16. Jeff KingAug 25, 2014
  17. Junio C HamanoAug 25, 2014
  18. Jeff KingAug 25, 2014
  19. Arjun SreedharanAug 25, 2014
  20. Junio C HamanoAug 25, 2014
  21. 0/3 name_decoration cleanupsJeff King, Aug 26, 2014
  22. 1/3 log-tree: make add_name_decoration a public functionJeff King, Aug 26, 2014
  23. Ramsay JonesAug 26, 2014
  24. Jeff KingAug 26, 2014
  25. 2/3 log-tree: make name_decoration hash staticJeff King, Aug 26, 2014
  26. Junio C HamanoAug 26, 2014
  27. Jeff KingAug 26, 2014
  28. Junio C HamanoAug 26, 2014
  29. 3/3 log-tree: use FLEX_ARRAY in name_decorationJeff King, Aug 26, 2014
  30. Eric SunshineAug 27, 2014
  31. Junio C HamanoAug 25, 2014
  32. Arjun SreedharanAug 26, 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.