From deaff859fdf7fe982edeeea87a24200418f60ad7 Mon Sep 17 00:00:00 2001 From: shixiaowei4 Date: Tue, 26 Mar 2024 09:47:14 +0000 Subject: [PATCH] Code fixed for performance Signed-off-by: shixiaowei4 Change-Id: I4baf4fffcc6c8fe23b7e13280699c8d07931d6e7 --- assembler/asm_metadata.rb | 4 +- assembler/extensions/ecmascript_meta.cpp | 52 +++++++------- assembler/extensions/ecmascript_meta.h | 48 ++++++------- assembler/meta.cpp | 40 ++++++----- assembler/meta.h | 90 ++++++++++++------------ 5 files changed, 121 insertions(+), 113 deletions(-) diff --git a/assembler/asm_metadata.rb b/assembler/asm_metadata.rb index 576790b049..f21d65afac 100644 --- a/assembler/asm_metadata.rb +++ b/assembler/asm_metadata.rb @@ -216,8 +216,8 @@ module MetadataGen end def arg_list(is_bool) - args = ['std::string_view attribute'] - args << 'std::string_view value' if !is_bool + args = ['const std::string_view &attribute'] + args << 'const std::string_view &value' if !is_bool args end diff --git a/assembler/extensions/ecmascript_meta.cpp b/assembler/extensions/ecmascript_meta.cpp index 39bc9f5ada..6c0fe12015 100644 --- a/assembler/extensions/ecmascript_meta.cpp +++ b/assembler/extensions/ecmascript_meta.cpp @@ -17,7 +17,7 @@ namespace panda::pandasm::extensions::ecmascript { -std::optional RecordMetadata::Validate(std::string_view attribute) const +std::optional RecordMetadata::Validate(const std::string_view &attribute) const { if (attribute == "ecmascript.annotation") { if (HasAttribute(attribute)) { @@ -35,7 +35,8 @@ std::optional RecordMetadata::Validate(std::string_view attribu return pandasm::RecordMetadata::Validate(attribute); } -std::optional RecordMetadata::Validate(std::string_view attribute, std::string_view value) const +std::optional RecordMetadata::Validate(const std::string_view &attribute, + const std::string_view &value) const { if (attribute == "ecmascript.extends") { if (HasAttribute(attribute)) { @@ -53,37 +54,40 @@ std::optional RecordMetadata::Validate(std::string_view attribu return pandasm::RecordMetadata::Validate(attribute, value); } -std::optional FieldMetadata::Validate(std::string_view attribute) const +std::optional FieldMetadata::Validate(const std::string_view &attribute) const { return pandasm::FieldMetadata::Validate(attribute); } -std::optional FieldMetadata::Validate(std::string_view attribute, std::string_view value) const +std::optional FieldMetadata::Validate(const std::string_view &attribute, + const std::string_view &value) const { return pandasm::FieldMetadata::Validate(attribute, value); } -std::optional FunctionMetadata::Validate(std::string_view attribute) const +std::optional FunctionMetadata::Validate(const std::string_view &attribute) const { return pandasm::FunctionMetadata::Validate(attribute); } -std::optional FunctionMetadata::Validate(std::string_view attribute, std::string_view value) const +std::optional FunctionMetadata::Validate(const std::string_view &attribute, + const std::string_view &value) const { return pandasm::FunctionMetadata::Validate(attribute, value); } -std::optional ParamMetadata::Validate(std::string_view attribute) const +std::optional ParamMetadata::Validate(const std::string_view &attribute) const { return pandasm::ParamMetadata::Validate(attribute); } -std::optional ParamMetadata::Validate(std::string_view attribute, std::string_view value) const +std::optional ParamMetadata::Validate(const std::string_view &attribute, + const std::string_view &value) const { return pandasm::ParamMetadata::Validate(attribute, value); } -void RecordMetadata::SetFlags(std::string_view attribute) +void RecordMetadata::SetFlags(const std::string_view &attribute) { if (attribute == "ecmascript.annotation") { SetAccessFlags(GetAccessFlags() | ACC_ANNOTATION); @@ -91,12 +95,12 @@ void RecordMetadata::SetFlags(std::string_view attribute) pandasm::RecordMetadata::SetFlags(attribute); } -void RecordMetadata::SetFlags(std::string_view attribute, std::string_view value) +void RecordMetadata::SetFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::RecordMetadata::SetFlags(attribute, value); } -void RecordMetadata::RemoveFlags(std::string_view attribute) +void RecordMetadata::RemoveFlags(const std::string_view &attribute) { if (attribute == "ecmascript.annotation") { if ((GetAccessFlags() & ACC_ANNOTATION) != 0) { @@ -106,67 +110,67 @@ void RecordMetadata::RemoveFlags(std::string_view attribute) pandasm::RecordMetadata::RemoveFlags(attribute); } -void RecordMetadata::RemoveFlags(std::string_view attribute, std::string_view value) +void RecordMetadata::RemoveFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::RecordMetadata::RemoveFlags(attribute, value); } -void FieldMetadata::SetFlags(std::string_view attribute) +void FieldMetadata::SetFlags(const std::string_view &attribute) { pandasm::FieldMetadata::SetFlags(attribute); } -void FieldMetadata::SetFlags(std::string_view attribute, std::string_view value) +void FieldMetadata::SetFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::FieldMetadata::SetFlags(attribute, value); } -void FieldMetadata::RemoveFlags(std::string_view attribute) +void FieldMetadata::RemoveFlags(const std::string_view &attribute) { pandasm::FieldMetadata::RemoveFlags(attribute); } -void FieldMetadata::RemoveFlags(std::string_view attribute, std::string_view value) +void FieldMetadata::RemoveFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::FieldMetadata::RemoveFlags(attribute, value); } -void FunctionMetadata::SetFlags(std::string_view attribute) +void FunctionMetadata::SetFlags(const std::string_view &attribute) { pandasm::FunctionMetadata::SetFlags(attribute); } -void FunctionMetadata::SetFlags(std::string_view attribute, std::string_view value) +void FunctionMetadata::SetFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::FunctionMetadata::SetFlags(attribute, value); } -void FunctionMetadata::RemoveFlags(std::string_view attribute) +void FunctionMetadata::RemoveFlags(const std::string_view &attribute) { pandasm::FunctionMetadata::RemoveFlags(attribute); } -void FunctionMetadata::RemoveFlags(std::string_view attribute, std::string_view value) +void FunctionMetadata::RemoveFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::FunctionMetadata::RemoveFlags(attribute, value); } -void ParamMetadata::SetFlags(std::string_view attribute) +void ParamMetadata::SetFlags(const std::string_view &attribute) { pandasm::ParamMetadata::SetFlags(attribute); } -void ParamMetadata::SetFlags(std::string_view attribute, std::string_view value) +void ParamMetadata::SetFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::ParamMetadata::SetFlags(attribute, value); } -void ParamMetadata::RemoveFlags(std::string_view attribute) +void ParamMetadata::RemoveFlags(const std::string_view &attribute) { pandasm::ParamMetadata::RemoveFlags(attribute); } -void ParamMetadata::RemoveFlags(std::string_view attribute, std::string_view value) +void ParamMetadata::RemoveFlags(const std::string_view &attribute, const std::string_view &value) { pandasm::ParamMetadata::RemoveFlags(attribute, value); } diff --git a/assembler/extensions/ecmascript_meta.h b/assembler/extensions/ecmascript_meta.h index e9f516fcc7..23051d848f 100644 --- a/assembler/extensions/ecmascript_meta.h +++ b/assembler/extensions/ecmascript_meta.h @@ -79,17 +79,17 @@ protected: return false; } - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; class FieldMetadata : public pandasm::FieldMetadata { @@ -125,17 +125,17 @@ protected: return false; } - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; class FunctionMetadata : public pandasm::FunctionMetadata { @@ -171,17 +171,17 @@ protected: return false; } - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; class ParamMetadata : public pandasm::ParamMetadata { @@ -217,17 +217,17 @@ protected: return false; } - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; } // namespace panda::pandasm::extensions::ecmascript diff --git a/assembler/meta.cpp b/assembler/meta.cpp index 2b83d84cf2..0d94ea7bce 100644 --- a/assembler/meta.cpp +++ b/assembler/meta.cpp @@ -24,7 +24,7 @@ namespace panda::pandasm { -std::optional Metadata::ValidateSize(std::string_view value) const +std::optional Metadata::ValidateSize(const std::string_view &value) const { constexpr size_t SIZE = 10; @@ -45,7 +45,7 @@ bool ItemMetadata::IsForeign() const return GetAttribute("external"); } -static panda::pandasm::Value::Type GetType(std::string_view value) +static panda::pandasm::Value::Type GetType(const std::string_view &value) { using VType = panda::pandasm::Value::Type; static std::unordered_map types { @@ -60,7 +60,7 @@ static panda::pandasm::Value::Type GetType(std::string_view value) } template -static T ConvertFromString(std::string_view value, char **end) +static T ConvertFromString(const std::string_view &value, char **end) { static_assert(std::is_integral_v, "T must be integral type"); @@ -85,31 +85,31 @@ static T ConvertFromString(std::string_view value, char **end) } template <> -int64_t ConvertFromString(std::string_view value, char **end) +int64_t ConvertFromString(const std::string_view &value, char **end) { return static_cast(strtoll(value.data(), end, 0)); } template <> -uint64_t ConvertFromString(std::string_view value, char **end) +uint64_t ConvertFromString(const std::string_view &value, char **end) { return static_cast(strtoull(value.data(), end, 0)); } template <> -float ConvertFromString(std::string_view value, char **end) +float ConvertFromString(const std::string_view &value, char **end) { return strtof(value.data(), end); } template <> -double ConvertFromString(std::string_view value, char **end) +double ConvertFromString(const std::string_view &value, char **end) { return strtod(value.data(), end); } template -static Expected ConvertFromString(std::string_view value) +static Expected ConvertFromString(const std::string_view &value) { static_assert(std::is_arithmetic_v, "T must be arithmetic type"); @@ -129,7 +129,7 @@ static Expected ConvertFromString(std::string_view value) } template > -static Expected CreatePrimitiveValue(std::string_view value, +static Expected CreatePrimitiveValue(const std::string_view &value, T max_value = std::numeric_limits::max()) { auto res = ConvertFromString(value); @@ -147,7 +147,7 @@ static Expected CreatePrimitiveValue(std::string_v } static Expected CreateValue( - Value::Type type, std::string_view value, + Value::Type type, const std::string_view &value, const std::unordered_map> &annotation_id_map = {}) { switch (type) { @@ -236,7 +236,7 @@ std::optional AnnotationMetadata::AnnotationElementBuilder::Add return {}; } -std::optional AnnotationMetadata::Store(std::string_view attribute) +std::optional AnnotationMetadata::Store(const std::string_view &attribute) { if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) { return Error(std::string("Unexpected attribute '").append(attribute) + @@ -251,7 +251,7 @@ std::optional AnnotationMetadata::Store(std::string_view attrib return Metadata::Store(attribute); } -std::optional AnnotationMetadata::MeetExpRecordAttribute(std::string_view attribute, +std::optional AnnotationMetadata::MeetExpRecordAttribute(const std::string_view &attribute, const std::string_view &value) { if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) { @@ -265,7 +265,7 @@ std::optional AnnotationMetadata::MeetExpRecordAttribute(std::s return {}; } -std::optional AnnotationMetadata::MeetExpIdAttribute(std::string_view attribute, +std::optional AnnotationMetadata::MeetExpIdAttribute(const std::string_view &attribute, const std::string_view &value) { if (!IsParseAnnotation() || IsParseAnnotationElement()) { @@ -285,7 +285,7 @@ std::optional AnnotationMetadata::MeetExpIdAttribute(std::strin return {}; } -std::optional AnnotationMetadata::MeetExpElementNameAttribute(std::string_view attribute, +std::optional AnnotationMetadata::MeetExpElementNameAttribute(const std::string_view &attribute, const std::string_view &value) { if (!IsParseAnnotation()) { @@ -306,7 +306,7 @@ std::optional AnnotationMetadata::MeetExpElementNameAttribute(s } std::optional AnnotationMetadata::MeetExpElementTypeAttribute( - std::string_view attribute, const std::string_view &value) + const std::string_view &attribute, const std::string_view &value) { if (!IsParseAnnotationElement()) { return Error(std::string("Unexpected attribute '").append(attribute) + @@ -326,7 +326,7 @@ std::optional AnnotationMetadata::MeetExpElementTypeAttribute( } std::optional AnnotationMetadata::MeetExpElementArrayComponentTypeAttribute( - std::string_view attribute, const std::string_view &value) + const std::string_view &attribute, const std::string_view &value) { if (!IsParseAnnotationElement()) { return Error(std::string("Unexpected attribute '").append(attribute) + @@ -350,7 +350,7 @@ std::optional AnnotationMetadata::MeetExpElementArrayComponentT return {}; } -std::optional AnnotationMetadata::MeetExpElementValueAttribute(std::string_view attribute, +std::optional AnnotationMetadata::MeetExpElementValueAttribute(const std::string_view &attribute, const std::string_view &value) { if (!IsParseAnnotationElement()) { @@ -380,7 +380,8 @@ std::optional AnnotationMetadata::MeetExpElementValueAttribute( return annotation_element_builder_.AddValue(value, id_map_); } -std::optional AnnotationMetadata::StoreValue(std::string_view attribute, std::string_view value) +std::optional AnnotationMetadata::StoreValue(const std::string_view &attribute, + const std::string_view &value) { auto err = Metadata::StoreValue(attribute, value); if (err) { @@ -477,7 +478,8 @@ bool FunctionMetadata::IsCctor() const return GetAttribute("cctor"); } -std::optional FieldMetadata::StoreValue(std::string_view attribute, std::string_view value) +std::optional FieldMetadata::StoreValue(const std::string_view &attribute, + const std::string_view &value) { auto err = ItemMetadata::StoreValue(attribute, value); if (err) { diff --git a/assembler/meta.h b/assembler/meta.h index 0b43f35a66..ee4c49df32 100644 --- a/assembler/meta.h +++ b/assembler/meta.h @@ -146,11 +146,12 @@ public: DEFAULT_MOVE_SEMANTIC(Metadata); protected: - virtual std::optional Validate(std::string_view attribute) const = 0; + virtual std::optional Validate(const std::string_view &attribute) const = 0; - virtual std::optional Validate(std::string_view attribute, std::string_view value) const = 0; + virtual std::optional Validate(const std::string_view &attribute, + const std::string_view &value) const = 0; - virtual std::optional StoreValue(std::string_view attribute, std::string_view value) + virtual std::optional StoreValue(const std::string_view &attribute, const std::string_view &value) { std::string key(attribute); auto it = attributes_.find(key); @@ -163,28 +164,28 @@ protected: return {}; } - virtual std::optional Store(std::string_view attribute) + virtual std::optional Store(const std::string_view &attribute) { set_attributes_.emplace(attribute); return {}; } - virtual void SetFlags(std::string_view attribute) = 0; + virtual void SetFlags(const std::string_view &attribute) = 0; - virtual void SetFlags(std::string_view attribute, std::string_view value) = 0; + virtual void SetFlags(const std::string_view &attribute, const std::string_view &value) = 0; - virtual void RemoveFlags(std::string_view attribute) = 0; + virtual void RemoveFlags(const std::string_view &attribute) = 0; - virtual void RemoveFlags(std::string_view attribute, std::string_view value) = 0; + virtual void RemoveFlags(const std::string_view &attribute, const std::string_view &value) = 0; - bool HasAttribute(std::string_view attribute) const + bool HasAttribute(const std::string_view &attribute) const { std::string key(attribute); return GetAttribute(key) || GetAttributeValue(key); } - std::optional ValidateSize(std::string_view value) const; + std::optional ValidateSize(const std::string_view &value) const; private: std::unordered_set set_attributes_; @@ -217,9 +218,9 @@ public: std::optional ValidateData() override; protected: - std::optional Store(std::string_view attribute) override; + std::optional Store(const std::string_view &attribute) override; - std::optional StoreValue(std::string_view attribute, std::string_view value) override; + std::optional StoreValue(const std::string_view &attribute, const std::string_view &value) override; virtual bool IsAnnotationRecordAttribute([[maybe_unused]] const std::string_view &attribute) const { @@ -399,15 +400,16 @@ private: bool is_initialized_ {false}; }; - std::optional MeetExpRecordAttribute(std::string_view attribute, const std::string_view &value); - std::optional MeetExpIdAttribute(std::string_view attribute, const std::string_view &value); - std::optional MeetExpElementNameAttribute(std::string_view attribute, + std::optional MeetExpRecordAttribute(const std::string_view &attribute, + const std::string_view &value); + std::optional MeetExpIdAttribute(const std::string_view &attribute, const std::string_view &value); + std::optional MeetExpElementNameAttribute(const std::string_view &attribute, const std::string_view &value); - std::optional MeetExpElementTypeAttribute(std::string_view attribute, + std::optional MeetExpElementTypeAttribute(const std::string_view &attribute, const std::string_view &value); - std::optional MeetExpElementArrayComponentTypeAttribute(std::string_view attribute, + std::optional MeetExpElementArrayComponentTypeAttribute(const std::string_view &attribute, const std::string_view &value); - std::optional MeetExpElementValueAttribute(std::string_view attribute, + std::optional MeetExpElementValueAttribute(const std::string_view &attribute, const std::string_view &value); void InitializeAnnotationBuilder(const std::string_view &name) @@ -504,17 +506,17 @@ public: virtual bool IsRuntimeTypeAnnotation() const; protected: - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; class FieldMetadata : public ItemMetadata { @@ -540,21 +542,21 @@ public: } protected: - std::optional StoreValue(std::string_view attribute, std::string_view value) override; + std::optional StoreValue(const std::string_view &attribute, const std::string_view &value) override; - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; - virtual bool IsValueAttribute(std::string_view attribute) + virtual bool IsValueAttribute(const std::string_view &attribute) { return attribute == "value"; } @@ -571,32 +573,32 @@ public: virtual bool IsCctor() const; protected: - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; class ParamMetadata : public AnnotationMetadata { protected: - std::optional Validate(std::string_view attribute) const override; + std::optional Validate(const std::string_view &attribute) const override; - std::optional Validate(std::string_view attribute, std::string_view value) const override; + std::optional Validate(const std::string_view &attribute, const std::string_view &value) const override; - void SetFlags(std::string_view attribute) override; + void SetFlags(const std::string_view &attribute) override; - void SetFlags(std::string_view attribute, std::string_view value) override; + void SetFlags(const std::string_view &attribute, const std::string_view &value) override; - void RemoveFlags(std::string_view attribute) override; + void RemoveFlags(const std::string_view &attribute) override; - void RemoveFlags(std::string_view attribute, std::string_view value) override; + void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; }; } // namespace panda::pandasm