Gabriel B. has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/67676?usp=email )
Change subject: mem-ruby: Optimize enum to string conversion in SLICC
......................................................................
mem-ruby: Optimize enum to string conversion in SLICC
The result was previously returned in a brand new std::string every
time. std::strings are now cached in a static const array and returned
by const reference.
Change-Id: I5ef6e3ef94898bc606e29b950caef9ab32935da2
---
M src/mem/slicc/ast/EnumDeclAST.py
M src/mem/slicc/ast/StateDeclAST.py
M src/mem/slicc/symbols/Type.py
3 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/src/mem/slicc/ast/EnumDeclAST.py
b/src/mem/slicc/ast/EnumDeclAST.py
index 5ffc8bb..25fd124 100644
--- a/src/mem/slicc/ast/EnumDeclAST.py
+++ b/src/mem/slicc/ast/EnumDeclAST.py
@@ -66,7 +66,7 @@
# Add the implicit State_to_string method - FIXME, this is a bit
dirty
func_id = "%s_to_string" % t.c_ident
- pairs = {"external": "yes"}
+ pairs = {"external": "yes", "return_by_const_ref": "yes"}
func = Func(
self.symtab,
func_id + "_" + t.c_ident,
diff --git a/src/mem/slicc/ast/StateDeclAST.py
b/src/mem/slicc/ast/StateDeclAST.py
index f6e5d6e..ae50b82 100644
--- a/src/mem/slicc/ast/StateDeclAST.py
+++ b/src/mem/slicc/ast/StateDeclAST.py
@@ -65,7 +65,7 @@
# Add the implicit State_to_string method - FIXME, this is a bit
dirty
func_id = "%s_to_string" % t.c_ident
- pairs = {"external": "yes"}
+ pairs = {"external": "yes", "return_by_const_ref": "yes"}
func = Func(
self.symtab,
func_id + "_" + t.ident,
diff --git a/src/mem/slicc/symbols/Type.py b/src/mem/slicc/symbols/Type.py
index a0bcd7b..797ced5 100644
--- a/src/mem/slicc/symbols/Type.py
+++ b/src/mem/slicc/symbols/Type.py
@@ -582,7 +582,7 @@
${{self.c_ident}} string_to_${{self.c_ident}}(const ::std::string& str);
// Code to convert state to a string
-::std::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj);
+const ::std::string& ${{self.c_ident}}_to_string(const ${{self.c_ident}}&
obj);
// Code to increment an enumeration type
${{self.c_ident}} &operator++(${{self.c_ident}} &e);
@@ -735,29 +735,24 @@
}
// Code to convert state to a string
-std::string
+const std::string&
${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj)
{
- switch(obj) {
+ static const std::array<std::string, ${{self.c_ident}}_NUM> lut = {
"""
)
-
- # For each field
+ code.indent()
code.indent()
for enum in self.enums.values():
- code(" case ${{self.c_ident}}_${{enum.ident}}:")
- code(' return "${{enum.ident}}";')
+ code('"${{enum.ident}}",')
code.dedent()
+ code("};")
+ code("return lut.at(obj);")
+ code.dedent()
+ code("}")
- # Trailer
code(
"""
- default:
- panic("Invalid range for type ${{self.c_ident}}");
- }
- // Appease the compiler since this function has a return value
- return "";
-}
// Code to convert from a string to the enumeration
${{self.c_ident}}
--
To view, visit
https://gem5-review.googlesource.com/c/public/gem5/+/67676?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I5ef6e3ef94898bc606e29b950caef9ab32935da2
Gerrit-Change-Number: 67676
Gerrit-PatchSet: 1
Gerrit-Owner: Gabriel B. <gabriel.bus...@arteris.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org