[Mesa-dev] [PATCH 09/12] glcpp: Avoid unnecessary linear_strdup

Vladislav Egorov vegorov180 at gmail.com
Sat Jan 7 19:02:10 UTC 2017


The lexer copies every identifier it finds. At first look it seems
needless, because all these identifiers are already available stored
inside of shader text, but my experiments with immutable explicitly
sized strings (ptr, len) resulted in a lot of code change and didn't
result in any speed increase.

Still, it can't be improved a bit. Length of the token was already
calculated by flex and stored in yyleng, no need to implicitly call
strlen() via linear_strdup().

I see no point in other strdups(). They copy strings that will not be
modified and will live at least to the end of the preprocessing stage.
The only exception is strdup in _token_paste() that actually modifies
the string, so strdup() is really necessary.
---
 src/compiler/glsl/glcpp/glcpp-lex.l   |  3 ++-
 src/compiler/glsl/glcpp/glcpp-parse.y | 12 ++++++------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/compiler/glsl/glcpp/glcpp-lex.l b/src/compiler/glsl/glcpp/glcpp-lex.l
index 381b973..93e5cb3 100644
--- a/src/compiler/glsl/glcpp/glcpp-lex.l
+++ b/src/compiler/glsl/glcpp/glcpp-lex.l
@@ -101,7 +101,8 @@ void glcpp_set_column (int  column_no , yyscan_t yyscanner);
 #define RETURN_STRING_TOKEN(token)					\
 	do {								\
 		if (! parser->skipping) {				\
-			yylval->str = linear_strdup(yyextra->linalloc, yytext);	\
+			yylval->str = linear_alloc_child(yyextra->linalloc, yyleng + 1);	\
+			memcpy(yylval->str, yytext, yyleng + 1);        \
 			RETURN_TOKEN_NEVER_SKIP (token);		\
 		}							\
 	} while(0)
diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y
index 47bf1e1..438ca51 100644
--- a/src/compiler/glsl/glcpp/glcpp-parse.y
+++ b/src/compiler/glsl/glcpp/glcpp-parse.y
@@ -480,7 +480,7 @@ expression:
 |	IDENTIFIER {
 		$$.value = 0;
 		if (parser->is_gles)
-			$$.undefined_macro = linear_strdup(parser->linalloc, $1);
+			$$.undefined_macro = $1;
 		else
 			$$.undefined_macro = NULL;
 	}
@@ -773,7 +773,7 @@ _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
    string_node_t *node;
 
    node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
-   node->str = linear_strdup(parser->linalloc, str);
+   node->str = str;
 
    node->next = NULL;
 
@@ -1869,7 +1869,7 @@ _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
       token_list_t *expansion;
       token_t *final;
 
-      str = linear_strdup(parser->linalloc, token->value.str);
+      str = token->value.str;
       final = _token_create_str(parser, OTHER, str);
       expansion = _token_list_create(parser);
       _token_list_append(parser, expansion, final);
@@ -1905,7 +1905,7 @@ _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
    active_list_t *node;
 
    node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
-   node->identifier = linear_strdup(parser->linalloc, identifier);
+   node->identifier = identifier;
    node->marker = marker;
    node->next = parser->active;
 
@@ -2114,7 +2114,7 @@ _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
 
    macro->is_function = 0;
    macro->parameters = NULL;
-   macro->identifier = linear_strdup(parser->linalloc, identifier);
+   macro->identifier = identifier;
    macro->replacements = replacements;
 
    entry = _mesa_hash_table_search(parser->defines, identifier);
@@ -2150,7 +2150,7 @@ _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
 
    macro->is_function = 1;
    macro->parameters = parameters;
-   macro->identifier = linear_strdup(parser->linalloc, identifier);
+   macro->identifier = identifier;
    macro->replacements = replacements;
 
    entry = _mesa_hash_table_search(parser->defines, identifier);
-- 
2.7.4



More information about the mesa-dev mailing list