From 3407d1658f7d60cfb78293d61dfc21258efb0074 Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Wed, 8 May 2019 23:06:52 +0000 Subject: compiler: avoid copy for string([]byte) conversion used in string comparison If a string([]byte) conversion is used immediately in a string comparison, we don't need to copy the backing store of the byte slice, as the string comparison doesn't hold any reference to it. Instead, just create a string header from the byte slice and pass it for comparison. A new type of expression, String_value_expression, is introduced, for constructing string headers. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/170894 * go.dg/cmpstring.go: New test. From-SVN: r271021 --- gcc/go/gofrontend/expressions.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'gcc/go/gofrontend/expressions.h') diff --git a/gcc/go/gofrontend/expressions.h b/gcc/go/gofrontend/expressions.h index 7d3cfd8..2cca824 100644 --- a/gcc/go/gofrontend/expressions.h +++ b/gcc/go/gofrontend/expressions.h @@ -102,6 +102,7 @@ class Expression EXPRESSION_BOOLEAN, EXPRESSION_STRING, EXPRESSION_STRING_INFO, + EXPRESSION_STRING_VALUE, EXPRESSION_INTEGER, EXPRESSION_FLOAT, EXPRESSION_COMPLEX, @@ -248,6 +249,10 @@ class Expression static Expression* make_string_info(Expression* string, String_info, Location); + // Make an expression for a string value. + static Expression* + make_string_value(Expression* valptr, Expression* len, Location); + // Make a character constant expression. TYPE should be NULL for an // abstract type. static Expression* @@ -1668,7 +1673,8 @@ class Type_conversion_expression : public Expression Type_conversion_expression(Type* type, Expression* expr, Location location) : Expression(EXPRESSION_CONVERSION, location), - type_(type), expr_(expr), may_convert_function_types_(false) + type_(type), expr_(expr), may_convert_function_types_(false), + no_copy_(false) { } // Return the type to which we are converting. @@ -1689,6 +1695,12 @@ class Type_conversion_expression : public Expression this->may_convert_function_types_ = true; } + // Mark string([]byte) conversion to reuse the backing store + // without copying. + void + set_no_copy(bool b) + { this->no_copy_ = b; }; + // Import a type conversion expression. static Expression* do_import(Import_expression*, Location); @@ -1751,6 +1763,9 @@ class Type_conversion_expression : public Expression // True if this is permitted to convert function types. This is // used internally for method expressions. bool may_convert_function_types_; + // True if a string([]byte) conversion can reuse the backing store + // without copying. Only used in string([]byte) conversion. + bool no_copy_; }; // An unsafe type conversion, used to pass values to builtin functions. -- cgit v1.1