Pass std::string_view by value not by ref (flutter/engine#33699)

String views are small, cheap objects that fit in a couple registers, so
passing by value avoids pointer indirection and thus a memory load.

Fun related reading for future archaeologists:
https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/

And a Windows-specific footnote:
https://quuxplusone.github.io/blog/2021/11/19/string-view-by-value-ps/

No test change since there is no semantic change.
This commit is contained in:
Chris Bracken
2022-05-31 11:13:29 -07:00
committed by GitHub
parent 62e713598f
commit 8d7d82192e
13 changed files with 15 additions and 18 deletions

View File

@@ -304,7 +304,7 @@ static std::string ToString(CompilerBackend::Type type) {
}
std::shared_ptr<fml::Mapping> Reflector::InflateTemplate(
const std::string_view& tmpl) const {
std::string_view tmpl) const {
inja::Environment env;
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);

View File

@@ -91,8 +91,7 @@ class Reflector {
std::shared_ptr<fml::Mapping> GenerateReflectionCC() const;
std::shared_ptr<fml::Mapping> InflateTemplate(
const std::string_view& tmpl) const;
std::shared_ptr<fml::Mapping> InflateTemplate(std::string_view tmpl) const;
std::optional<nlohmann::json::object_t> ReflectResource(
const spirv_cross::Resource& resource) const;

View File

@@ -94,7 +94,7 @@ bool ShaderLibraryGLES::IsValid() const {
// |ShaderLibrary|
std::shared_ptr<const ShaderFunction> ShaderLibraryGLES::GetFunction(
const std::string_view& name,
std::string_view name,
ShaderStage stage) {
const auto key = ShaderKey{name, stage};
if (auto found = functions_.find(key); found != functions_.end()) {

View File

@@ -30,9 +30,8 @@ class ShaderLibraryGLES final : public ShaderLibrary {
std::vector<std::shared_ptr<fml::Mapping>> shader_libraries);
// |ShaderLibrary|
std::shared_ptr<const ShaderFunction> GetFunction(
const std::string_view& name,
ShaderStage stage) override;
std::shared_ptr<const ShaderFunction> GetFunction(std::string_view name,
ShaderStage stage) override;
FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibraryGLES);
};

View File

@@ -79,7 +79,7 @@ bool TextureGLES::IsValid() const {
}
// |Texture|
void TextureGLES::SetLabel(const std::string_view& label) {
void TextureGLES::SetLabel(std::string_view label) {
reactor_->SetDebugLabel(handle_, std::string{label.data(), label.size()});
}

View File

@@ -62,7 +62,7 @@ class TextureGLES final : public Texture,
bool is_wrapped);
// |Texture|
void SetLabel(const std::string_view& label) override;
void SetLabel(std::string_view label) override;
// |Texture|
bool OnSetContents(const uint8_t* contents,

View File

@@ -39,9 +39,8 @@ class ShaderLibraryMTL final : public ShaderLibrary {
ShaderLibraryMTL(NSArray<id<MTLLibrary>>* libraries);
// |ShaderLibrary|
std::shared_ptr<const ShaderFunction> GetFunction(
const std::string_view& name,
ShaderStage stage) override;
std::shared_ptr<const ShaderFunction> GetFunction(std::string_view name,
ShaderStage stage) override;
FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibraryMTL);
};

View File

@@ -24,7 +24,7 @@ bool ShaderLibraryMTL::IsValid() const {
}
std::shared_ptr<const ShaderFunction> ShaderLibraryMTL::GetFunction(
const std::string_view& name,
std::string_view name,
ShaderStage stage) {
if (!IsValid()) {
return nullptr;

View File

@@ -27,7 +27,7 @@ class TextureMTL final : public Texture,
bool is_valid_ = false;
// |Texture|
void SetLabel(const std::string_view& label) override;
void SetLabel(std::string_view label) override;
// |Texture|
bool OnSetContents(const uint8_t* contents,

View File

@@ -26,7 +26,7 @@ TextureMTL::TextureMTL(TextureDescriptor p_desc, id<MTLTexture> texture)
TextureMTL::~TextureMTL() = default;
void TextureMTL::SetLabel(const std::string_view& label) {
void TextureMTL::SetLabel(std::string_view label) {
[texture_ setLabel:@(label.data())];
}

View File

@@ -18,7 +18,7 @@ struct ShaderKey {
std::string name;
ShaderStage stage = ShaderStage::kUnknown;
ShaderKey(const std::string_view& p_name, ShaderStage p_stage)
ShaderKey(std::string_view p_name, ShaderStage p_stage)
: name({p_name.data(), p_name.size()}), stage(p_stage) {}
struct Hash {

View File

@@ -22,7 +22,7 @@ class ShaderLibrary {
virtual bool IsValid() const = 0;
virtual std::shared_ptr<const ShaderFunction> GetFunction(
const std::string_view& name,
std::string_view name,
ShaderStage stage) = 0;
protected:

View File

@@ -18,7 +18,7 @@ class Texture {
public:
virtual ~Texture();
virtual void SetLabel(const std::string_view& label) = 0;
virtual void SetLabel(std::string_view label) = 0;
[[nodiscard]] bool SetContents(const uint8_t* contents,
size_t length,