mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
Switch CARBON_VLOG to support a format string API. (#4283)
The goal is to replace our stream operator APIs with format string APIs that can be made to have much less impact on inlining and other optimizations of the performance critical path through the code. Several experiments show that the most compact representation we can arrange for is one that calls an uninlined function and passes a minimal number of arguments to it. It doesn't help to do any work to minimize the arguments such as building a lambda -- the cost of extra code to merge the arguments is likely to outweigh the benefit. Initial experiments showed that switching a hot but uninlined function to this new API enabled inlining and the subsequent performance improvement. This also adds a 'TemplateString` utility that allows using a string literal as a template parameter. This is useful to remove the format string itself from the arguments passed to the function by passing it as a template argument instead. Currently, support is left in place for both APIs because with `CARBON_VLOG` we can detect whether or not any message was provided expecting a format string. This should allow incrementally migrating code to this API. I've added some test coverage in this PR, but I'll separate out any switching of parts of the codebase over. The goal is to eventually replace all the usages and remove the streaming support entirely. This PR doesn't update `CARBON_CHECK` in the same way because it is substantially more complex to switch. I have a few experimental PRs looking at that and will discuss how best to approach this with the specific challenges check presents separately. But the goal is for all of the macro-based output APIs to move to format strings rather than streams. --------- Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com> Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
josh11b
Richard Smith
parent
c33c9a02f6
commit
e48101b608
@@ -24,7 +24,9 @@ class VLogger {
|
||||
}
|
||||
}
|
||||
|
||||
void VLog() { CARBON_VLOG() << "Test\n"; }
|
||||
void VLog() { CARBON_VLOG("Test\n"); }
|
||||
void VLogFormatArgs() { CARBON_VLOG("Test {0} {1} {2}\n", 1, 2, 3); }
|
||||
void VLogStream() { CARBON_VLOG() << "Test\n"; }
|
||||
|
||||
auto TakeStr() -> std::string { return buffer_.TakeStr(); }
|
||||
|
||||
@@ -38,6 +40,10 @@ TEST(VLogTest, Enabled) {
|
||||
VLogger vlog(/*enable=*/true);
|
||||
vlog.VLog();
|
||||
EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
|
||||
vlog.VLogFormatArgs();
|
||||
EXPECT_THAT(vlog.TakeStr(), StrEq("Test 1 2 3\n"));
|
||||
vlog.VLogStream();
|
||||
EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
|
||||
}
|
||||
|
||||
TEST(VLogTest, Disabled) {
|
||||
|
||||
Reference in New Issue
Block a user