mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:20:10 +01:00
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>
81 lines
3.0 KiB
C++
81 lines
3.0 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_COMMON_TEMPLATE_STRING_H_
|
|
#define CARBON_COMMON_TEMPLATE_STRING_H_
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Represents a compile-time string in a form suitable for use as a non-type
|
|
// template argument.
|
|
//
|
|
// These arguments are required to be a "structural type", and so we copy the
|
|
// string contents into a public array of `char`s. For details, see:
|
|
// https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter
|
|
//
|
|
// Designed to support implicitly deduced construction from a string literal
|
|
// template argument. This type will implicitly convert to an `llvm::StringRef`
|
|
// for accessing the string contents, and also provides a dedicated `c_str()`
|
|
// method to access the string as a C string.
|
|
//
|
|
// Example usage:
|
|
// ```cpp
|
|
// template <TemplateString Str> auto F() -> void {
|
|
// llvm::cout() << Str;
|
|
// }
|
|
//
|
|
// auto Example() -> void {
|
|
// F<"string contents here">();
|
|
// }
|
|
// ```
|
|
template <int N>
|
|
struct TemplateString {
|
|
// Constructs the template string from a string literal.
|
|
//
|
|
// Intentionally allows implicit conversion from string literals for use as a
|
|
// non-type template parameter.
|
|
//
|
|
// The closest we can get to explicitly accepting a string literal is to
|
|
// accept an array of `const char`s, so we additionally use Clang's constexpr
|
|
// `enable_if` attribute to require the array to be usable as a C string with
|
|
// the expected length. This checks both for null-termination and no embedded
|
|
// `0` bytes.
|
|
//
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr TemplateString(const char (&str)[N + 1]) __attribute__((
|
|
enable_if(__builtin_strlen(str) == N,
|
|
"character array is not null-terminated valid C string"))) {
|
|
// Rely on Clang's constexpr `__builtin_memcpy` to minimize compile time
|
|
// overhead copying the string contents around.
|
|
__builtin_memcpy(storage_, str, N + 1);
|
|
}
|
|
|
|
// This type is designed to act as a `StringRef` implicitly while having the
|
|
// storage necessary to be used as a template parameter.
|
|
//
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr operator llvm::StringRef() const {
|
|
return llvm::StringRef(storage_, N);
|
|
}
|
|
|
|
// Accesses the string data directly as a compile-time C string.
|
|
constexpr auto c_str() const -> const char* { return storage_; }
|
|
|
|
// Note that this must be public for the type to be structural and available
|
|
// as a template argument, but this is not part of the public API.
|
|
char storage_[N + 1];
|
|
};
|
|
|
|
// Allow deducing `N` when implicitly constructing these so that we can directly
|
|
// use a string literal in a template argument. The array needs an extra char
|
|
// for the null terminator.
|
|
template <int M>
|
|
TemplateString(const char (&str)[M]) -> TemplateString<M - 1>;
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_COMMON_TEMPLATE_STRING_H_
|