From 3a40a63832bda116e256b40171f593f7b9a9b949 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Wed, 13 May 2020 08:38:05 +0200 Subject: [PATCH] obs/gs/effect-parameter: Use string_view over std::string Using std::string_view over std::string (and const std::string&) has the advantage that we skip potential temporary std::string objects that are immediately thrown away, thus slowing down the code. It can also be implicitly cast to std::string, which makes it compatible with existing code that uses std::string. --- source/common.hpp | 1 + source/obs/gs/gs-effect-parameter.cpp | 14 ++++++-------- source/obs/gs/gs-effect-parameter.hpp | 8 ++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/source/common.hpp b/source/common.hpp index c09151c8..79f98b99 100644 --- a/source/common.hpp +++ b/source/common.hpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include diff --git a/source/obs/gs/gs-effect-parameter.cpp b/source/obs/gs/gs-effect-parameter.cpp index 3e7da66f..c75e9857 100644 --- a/source/obs/gs/gs-effect-parameter.cpp +++ b/source/obs/gs/gs-effect-parameter.cpp @@ -112,11 +112,9 @@ try { return *this; } -std::string gs::effect_parameter::get_name() +std::string_view gs::effect_parameter::get_name() { - const char* name_c = get()->name; - std::size_t name_len = strnlen(name_c, 256); - return name_c ? std::string(name_c, name_c + name_len) : std::string(); + return std::string_view{get()->name}; } gs::effect_parameter::type gs::effect_parameter::get_type() @@ -166,11 +164,11 @@ gs::effect_parameter gs::effect_parameter::get_annotation(std::size_t idx) return effect_parameter(get()->annotations.array + idx, this); } -gs::effect_parameter gs::effect_parameter::get_annotation(std::string name) +gs::effect_parameter gs::effect_parameter::get_annotation(const std::string_view name) { for (std::size_t idx = 0; idx < get()->annotations.num; idx++) { auto ptr = get()->annotations.array + idx; - if (strcmp(ptr->name, name.c_str()) == 0) { + if (name == std::string_view{ptr->name}) { return gs::effect_parameter(ptr, this); } } @@ -178,7 +176,7 @@ gs::effect_parameter gs::effect_parameter::get_annotation(std::string name) return nullptr; } -bool gs::effect_parameter::has_annotation(std::string name) +bool gs::effect_parameter::has_annotation(const std::string_view name) { auto eprm = get_annotation(name); if (eprm) @@ -186,7 +184,7 @@ bool gs::effect_parameter::has_annotation(std::string name) return false; } -bool gs::effect_parameter::has_annotation(std::string name, effect_parameter::type type) +bool gs::effect_parameter::has_annotation(const std::string_view name, effect_parameter::type type) { auto eprm = get_annotation(name); if (eprm) diff --git a/source/obs/gs/gs-effect-parameter.hpp b/source/obs/gs/gs-effect-parameter.hpp index 9894047f..5fa9a85e 100644 --- a/source/obs/gs/gs-effect-parameter.hpp +++ b/source/obs/gs/gs-effect-parameter.hpp @@ -61,15 +61,15 @@ namespace gs { effect_parameter(effect_parameter&& rhs) noexcept; effect_parameter& operator=(effect_parameter&& rhs) noexcept; - std::string get_name(); + std::string_view get_name(); type get_type(); std::size_t count_annotations(); effect_parameter get_annotation(std::size_t idx); - effect_parameter get_annotation(std::string name); - bool has_annotation(std::string name); - bool has_annotation(std::string name, effect_parameter::type type); + effect_parameter get_annotation(const std::string_view name); + bool has_annotation(const std::string_view name); + bool has_annotation(const std::string_view name, effect_parameter::type type); public /* Memory API */: std::size_t get_default_value_size_in_bytes()