[Lldb-commits] [PATCH] D27459: Add a more succinct logging syntax

Zachary Turner via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Jan 17 11:11:32 PST 2017


zturner added inline comments.


================
Comment at: source/Core/Log.cpp:78
+  char *text;
+  vasprintf(&text, format, args);
+  message << text;
----------------
dancol wrote:
> I usually implement printf-into-std::string by using `vsnprintf` to figure out how many characters we generate, using `std::string::resize` to create a buffer of that many characters (unfortunately, zero-filling them), then `vsnprintf` directly into that buffer. This way, you only need one allocation.
> 
> The currant approach involves at least three allocations: first, the string generated by `vasprintf`. Second, the internal `stringstream` buffer. Third, the copy of the buffer that `std::stringstream::str` generates.
> 
> It's more expensive that it needs to be.
To be fair, we should really be deleting these methods long term, and using `formatv`.  This way you often end up with 0 allocations (for short messages), and on top of that, only one underlying format call (as opposed to wasting time calling `vasprintf` twice here).


https://reviews.llvm.org/D27459





More information about the lldb-commits mailing list