[Mesa-dev] [PATCH 2/7] glsl/glcpp: use ralloc_sprint_rewrite_tail to avoid slow vsprintf

Marek Olšák maraeo at gmail.com
Sun Jan 1 00:34:27 UTC 2017


From: Marek Olšák <marek.olsak at amd.com>

This reduces compile times by 4.5% with the Gallium noop driver and
gl_constants::GLSLOptimizeConservatively == true.
---
 src/compiler/glsl/glcpp/glcpp-parse.y | 39 +++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y
index 63012bc..b84e5ff 100644
--- a/src/compiler/glsl/glcpp/glcpp-parse.y
+++ b/src/compiler/glsl/glcpp/glcpp-parse.y
@@ -202,21 +202,21 @@ add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
 input:
 	/* empty */
 |	input line
 ;
 
 line:
 	control_line
 |	SPACE control_line
 |	text_line {
 		_glcpp_parser_print_expanded_token_list (parser, $1);
-		ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
+		ralloc_sprint_rewrite_tail(&parser->output, &parser->output_length, "\n", 1);
 	}
 |	expanded_line
 ;
 
 expanded_line:
 	IF_EXPANDED expression NEWLINE {
 		if (parser->is_gles && $2.undefined_macro)
 			glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
 		_glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
 	}
@@ -252,21 +252,21 @@ define:
 |	FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
 		_define_function_macro (parser, & @1, $1, NULL, $4);
 	}
 |	FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
 		_define_function_macro (parser, & @1, $1, $3, $5);
 	}
 ;
 
 control_line:
 	control_line_success {
-		ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
+		ralloc_sprint_rewrite_tail(&parser->output, &parser->output_length, "\n", 1);
 	}
 |	control_line_error
 |	HASH_TOKEN LINE pp_tokens NEWLINE {
 
 		if (parser->skip_stack == NULL ||
 		    parser->skip_stack->type == SKIP_NO_SKIP)
 		{
 			_glcpp_parser_expand_and_lex_from (parser,
 							   LINE_EXPANDED, $3,
 							   EXPANSION_MODE_IGNORE_DEFINED);
@@ -428,21 +428,22 @@ control_line_success:
 |	HASH_TOKEN VERSION_TOKEN version_constant IDENTIFIER NEWLINE {
 		if (parser->version_set) {
 			glcpp_error(& @1, parser, "#version must appear on the first line");
 		}
 		_glcpp_parser_handle_version_declaration(parser, $3, $4, true);
 	}
 |	HASH_TOKEN NEWLINE {
 		glcpp_parser_resolve_implicit_version(parser);
 	}
 |	HASH_TOKEN PRAGMA NEWLINE {
-		ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
+		ralloc_sprint_rewrite_tail(&parser->output, &parser->output_length, "#", 1);
+		ralloc_sprint_rewrite_tail(&parser->output, &parser->output_length, $2, strlen($2));
 	}
 ;
 
 control_line_error:
 	HASH_TOKEN ERROR_TOKEN NEWLINE {
 		glcpp_error(& @1, parser, "#%s", $2);
 	}
 |	HASH_TOKEN DEFINE_TOKEN NEWLINE {
 		glcpp_error (& @1, parser, "#define without macro name");
 	}
@@ -1109,71 +1110,73 @@ _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
       node_b = node_b->next;
    }
 
    return 1;
 }
 
 static void
 _token_print(char **out, size_t *len, token_t *token)
 {
    if (token->type < 256) {
-      ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
+      char s[2] = {token->type, 0};
+      ralloc_sprint_rewrite_tail(out, len, s, 1);
       return;
    }
 
    switch (token->type) {
    case INTEGER:
       ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
       break;
    case IDENTIFIER:
    case INTEGER_STRING:
    case OTHER:
-      ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
+      ralloc_sprint_rewrite_tail(out, len, token->value.str,
+                                 strlen(token->value.str));
       break;
    case SPACE:
-      ralloc_asprintf_rewrite_tail (out, len, " ");
+      ralloc_sprint_rewrite_tail(out, len, " ", 1);
       break;
    case LEFT_SHIFT:
-      ralloc_asprintf_rewrite_tail (out, len, "<<");
+      ralloc_sprint_rewrite_tail(out, len, "<<", 2);
       break;
    case RIGHT_SHIFT:
-      ralloc_asprintf_rewrite_tail (out, len, ">>");
+      ralloc_sprint_rewrite_tail(out, len, ">>", 2);
       break;
    case LESS_OR_EQUAL:
-      ralloc_asprintf_rewrite_tail (out, len, "<=");
+      ralloc_sprint_rewrite_tail(out, len, "<=", 2);
       break;
    case GREATER_OR_EQUAL:
-      ralloc_asprintf_rewrite_tail (out, len, ">=");
+      ralloc_sprint_rewrite_tail(out, len, ">=", 2);
       break;
    case EQUAL:
-      ralloc_asprintf_rewrite_tail (out, len, "==");
+      ralloc_sprint_rewrite_tail(out, len, "==", 2);
       break;
    case NOT_EQUAL:
-      ralloc_asprintf_rewrite_tail (out, len, "!=");
+      ralloc_sprint_rewrite_tail(out, len, "!=", 2);
       break;
    case AND:
-      ralloc_asprintf_rewrite_tail (out, len, "&&");
+      ralloc_sprint_rewrite_tail(out, len, "&&", 2);
       break;
    case OR:
-      ralloc_asprintf_rewrite_tail (out, len, "||");
+      ralloc_sprint_rewrite_tail(out, len, "||", 2);
       break;
    case PASTE:
-      ralloc_asprintf_rewrite_tail (out, len, "##");
+      ralloc_sprint_rewrite_tail(out, len, "##", 2);
       break;
    case PLUS_PLUS:
-      ralloc_asprintf_rewrite_tail (out, len, "++");
+      ralloc_sprint_rewrite_tail(out, len, "++", 2);
       break;
    case MINUS_MINUS:
-      ralloc_asprintf_rewrite_tail (out, len, "--");
+      ralloc_sprint_rewrite_tail(out, len, "--", 2);
       break;
    case DEFINED:
-      ralloc_asprintf_rewrite_tail (out, len, "defined");
+      ralloc_sprint_rewrite_tail(out, len, "defined", 7);
       break;
    case PLACEHOLDER:
       /* Nothing to print. */
       break;
    default:
       assert(!"Error: Don't know how to print token.");
 
       break;
    }
 }
-- 
2.7.4



More information about the mesa-dev mailing list