From: Bruno Haible Date: Sun, 4 Nov 2018 19:20:37 +0000 (+0100) Subject: xgettext: Optimize away a memory allocation. X-Git-Tag: v0.20~263 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b3832ec8c713c702c551eb161dd8196a0f13b4b;p=thirdparty%2Fgettext.git xgettext: Optimize away a memory allocation. * gettext-tools/src/x-c.c (phase5_get): Allocate the mixed_string_buffer on the stack, not on the heap. * gettext-tools/src/x-csharp.c (phase6_get): Likewise. * gettext-tools/src/x-javascript.c (phase5_get): Likewise. * gettext-tools/src/x-python.c (phase5_get): Likewise. * gettext-tools/src/x-vala.c (phase3_get): Likewise. * gettext-tools/src/x-rst.c (stringbuf, parse_string, extract_rsj): Allocate the mixed_string_buffer statically, not on the heap. * gettext-tools/src/xg-mixed-string.h (mixed_string_buffer_alloc, mixed_string_buffer_done): Remove declarations. * gettext-tools/src/xg-mixed-string.c (mixed_string_buffer_alloc, mixed_string_buffer_done): Remove functions. --- diff --git a/gettext-tools/src/x-c.c b/gettext-tools/src/x-c.c index 3b4509955..fe1c6ebe6 100644 --- a/gettext-tools/src/x-c.c +++ b/gettext-tools/src/x-c.c @@ -1361,7 +1361,7 @@ phase5_get (token_ty *tp) } if (c == '(') { - struct mixed_string_buffer *bp; + struct mixed_string_buffer msb; /* The state is either 0 or N, after a ')' and N-1 bytes of the delimiter have been encountered. */ @@ -1369,11 +1369,9 @@ phase5_get (token_ty *tp) /* Start accumulating the string. */ if (relevant) - bp = mixed_string_buffer_alloc (lc_string, - logical_file_name, - line_number); - else - bp = NULL; + mixed_string_buffer_init (&msb, lc_string, + logical_file_name, + line_number); state = 0; for (;;) @@ -1382,7 +1380,7 @@ phase5_get (token_ty *tp) /* Keep line_number in sync. */ if (relevant) - bp->line_number = line_number; + msb.line_number = line_number; if (c == EOF) break; @@ -1398,7 +1396,7 @@ phase5_get (token_ty *tp) if (relevant) { tp->type = token_type_string_literal; - tp->string = mixed_string_buffer_done (bp); + tp->string = mixed_string_buffer_result (&msb); tp->comment = add_reference (savable_comment); } else @@ -1414,7 +1412,7 @@ phase5_get (token_ty *tp) can be ')'. */ if (relevant) for (i = 0; i < state; i++) - mixed_string_buffer_append_char (bp, buffer[i]); + mixed_string_buffer_append_char (&msb, buffer[i]); /* But c may be ')'. */ if (c == ')') @@ -1422,7 +1420,7 @@ phase5_get (token_ty *tp) else { if (relevant) - mixed_string_buffer_append_char (bp, c); + mixed_string_buffer_append_char (&msb, c); state = 0; } } @@ -1637,19 +1635,18 @@ phase5_get (token_ty *tp) about the argument not matching the prototype. Just pretend it won't happen. */ { - struct mixed_string_buffer *bp; + struct mixed_string_buffer msb; /* Start accumulating the string. */ - bp = mixed_string_buffer_alloc (lc_string, - logical_file_name, - line_number); + mixed_string_buffer_init (&msb, lc_string, + logical_file_name, line_number); for (;;) { c = phase7_getc (); /* Keep line_number in sync. */ - bp->line_number = line_number; + msb.line_number = line_number; if (c == P7_NEWLINE) { @@ -1668,14 +1665,13 @@ phase5_get (token_ty *tp) { assert (UNICODE_VALUE (c) >= 0 && UNICODE_VALUE (c) < 0x110000); - mixed_string_buffer_append_unicode (bp, - UNICODE_VALUE (c)); + mixed_string_buffer_append_unicode (&msb, UNICODE_VALUE (c)); } else - mixed_string_buffer_append_char (bp, c); + mixed_string_buffer_append_char (&msb, c); } tp->type = token_type_string_literal; - tp->string = mixed_string_buffer_done (bp); + tp->string = mixed_string_buffer_result (&msb); tp->comment = add_reference (savable_comment); return; } diff --git a/gettext-tools/src/x-csharp.c b/gettext-tools/src/x-csharp.c index 4656e5459..692f0f6b5 100644 --- a/gettext-tools/src/x-csharp.c +++ b/gettext-tools/src/x-csharp.c @@ -1639,14 +1639,15 @@ phase6_get (token_ty *tp) case '"': /* Regular string literal. */ { - struct mixed_string_buffer *literal; + struct mixed_string_buffer literal; lexical_context = lc_string; - literal = mixed_string_buffer_alloc (lexical_context, - logical_file_name, - logical_line_number); - accumulate_escaped (literal, '"'); - tp->string = mixed_string_buffer_done (literal); + mixed_string_buffer_init (&literal, + lexical_context, + logical_file_name, + logical_line_number); + accumulate_escaped (&literal, '"'); + tp->string = mixed_string_buffer_result (&literal); tp->comment = add_reference (savable_comment); lexical_context = lc_outside; tp->type = token_type_string_literal; diff --git a/gettext-tools/src/x-javascript.c b/gettext-tools/src/x-javascript.c index ed0a09857..d69157440 100644 --- a/gettext-tools/src/x-javascript.c +++ b/gettext-tools/src/x-javascript.c @@ -1190,43 +1190,42 @@ phase5_get (token_ty *tp) return; } - /* Strings. */ + case '"': case '\'': + /* Strings. */ { - struct mixed_string_buffer *bp; int quote_char; + struct mixed_string_buffer msb; - case '"': case '\'': - quote_char = c; - lexical_context = lc_string; - /* Start accumulating the string. */ - bp = mixed_string_buffer_alloc (lexical_context, - logical_file_name, - line_number); - for (;;) - { - int uc = phase7_getuc (quote_char); + quote_char = c; + lexical_context = lc_string; + /* Start accumulating the string. */ + mixed_string_buffer_init (&msb, lexical_context, + logical_file_name, line_number); + for (;;) + { + int uc = phase7_getuc (quote_char); - /* Keep line_number in sync. */ - bp->line_number = line_number; + /* Keep line_number in sync. */ + msb.line_number = line_number; - if (uc == P7_EOF || uc == P7_STRING_END) - break; + if (uc == P7_EOF || uc == P7_STRING_END) + break; - if (IS_UNICODE (uc)) - { - assert (UNICODE_VALUE (uc) >= 0 - && UNICODE_VALUE (uc) < 0x110000); - mixed_string_buffer_append_unicode (bp, - UNICODE_VALUE (uc)); - } - else - mixed_string_buffer_append_char (bp, uc); - } - tp->string = mixed_string_buffer_done (bp); - tp->comment = add_reference (savable_comment); - lexical_context = lc_outside; - tp->type = last_token_type = token_type_string; - return; + if (IS_UNICODE (uc)) + { + assert (UNICODE_VALUE (uc) >= 0 + && UNICODE_VALUE (uc) < 0x110000); + mixed_string_buffer_append_unicode (&msb, + UNICODE_VALUE (uc)); + } + else + mixed_string_buffer_append_char (&msb, uc); + } + tp->string = mixed_string_buffer_result (&msb); + tp->comment = add_reference (savable_comment); + lexical_context = lc_outside; + tp->type = last_token_type = token_type_string; + return; } case '+': diff --git a/gettext-tools/src/x-python.c b/gettext-tools/src/x-python.c index 3cf8cb1c9..5ab722f34 100644 --- a/gettext-tools/src/x-python.c +++ b/gettext-tools/src/x-python.c @@ -1330,7 +1330,6 @@ phase5_get (token_ty *tp) /* Strings. */ { - struct mixed_string_buffer *bp; int quote_char; bool interpret_ansic; bool interpret_unicode; @@ -1401,35 +1400,38 @@ phase5_get (token_ty *tp) phase2_ungetc (c1); } backslash_counter = 0; - /* Start accumulating the string. */ - bp = mixed_string_buffer_alloc (lexical_context, - logical_file_name, - line_number); - for (;;) - { - int uc = phase7_getuc (quote_char, triple, interpret_ansic, - interpret_unicode, &backslash_counter); + { + struct mixed_string_buffer msb; - /* Keep line_number in sync. */ - bp->line_number = line_number; + /* Start accumulating the string. */ + mixed_string_buffer_init (&msb, lexical_context, + logical_file_name, line_number); + for (;;) + { + int uc = phase7_getuc (quote_char, triple, interpret_ansic, + interpret_unicode, &backslash_counter); - if (uc == P7_EOF || uc == P7_STRING_END) - break; + /* Keep line_number in sync. */ + msb.line_number = line_number; - if (IS_UNICODE (uc)) - { - assert (UNICODE_VALUE (uc) >= 0 - && UNICODE_VALUE (uc) < 0x110000); - mixed_string_buffer_append_unicode (bp, - UNICODE_VALUE (uc)); - } - else - mixed_string_buffer_append_char (bp, uc); - } - tp->string = mixed_string_buffer_done (bp); - tp->comment = add_reference (savable_comment); - lexical_context = lc_outside; - tp->type = token_type_string; + if (uc == P7_EOF || uc == P7_STRING_END) + break; + + if (IS_UNICODE (uc)) + { + assert (UNICODE_VALUE (uc) >= 0 + && UNICODE_VALUE (uc) < 0x110000); + mixed_string_buffer_append_unicode (&msb, + UNICODE_VALUE (uc)); + } + else + mixed_string_buffer_append_char (&msb, uc); + } + tp->string = mixed_string_buffer_result (&msb); + tp->comment = add_reference (savable_comment); + lexical_context = lc_outside; + tp->type = token_type_string; + } return; } diff --git a/gettext-tools/src/x-rst.c b/gettext-tools/src/x-rst.c index 886163cc1..847d60bd8 100644 --- a/gettext-tools/src/x-rst.c +++ b/gettext-tools/src/x-rst.c @@ -386,7 +386,7 @@ parse_integer () return (bufpos == 0 ? pr_none : pr_parsed); } -static struct mixed_string_buffer *stringbuf; +static struct mixed_string_buffer stringbuf; /* Parses a string. Returns it in stringbuf, in UTF-8 encoding. Returns a parse_result. */ @@ -401,14 +401,13 @@ parse_string () phase2_ungetc (c); return pr_none; } - stringbuf = mixed_string_buffer_alloc (lc_string, - logical_file_name, - line_number); + mixed_string_buffer_init (&stringbuf, lc_string, + logical_file_name, line_number); for (;;) { c = phase1_getc (); /* Keep line_number in sync. */ - stringbuf->line_number = line_number; + stringbuf.line_number = line_number; if (c == EOF || (c >= 0 && c < 0x20)) return pr_syntax; if (c == '"') @@ -434,7 +433,7 @@ parse_string () else return pr_syntax; } - mixed_string_buffer_append_unicode (stringbuf, n); + mixed_string_buffer_append_unicode (&stringbuf, n); } else { @@ -462,11 +461,11 @@ parse_string () default: return pr_syntax; } - mixed_string_buffer_append_char (stringbuf, c); + mixed_string_buffer_append_char (&stringbuf, c); } } else - mixed_string_buffer_append_char (stringbuf, c); + mixed_string_buffer_append_char (&stringbuf, c); } return pr_parsed; } @@ -503,7 +502,7 @@ extract_rsj (FILE *f, char *s1; if (parse_string () != pr_parsed) goto invalid_json; - s1 = mixed_string_buffer_done (stringbuf); + s1 = mixed_string_buffer_result (&stringbuf); /* Parse a colon. */ c = phase2_getc (); @@ -550,7 +549,7 @@ extract_rsj (FILE *f, char *s2; if (parse_string () != pr_parsed) goto invalid_json; - s2 = mixed_string_buffer_done (stringbuf); + s2 = mixed_string_buffer_result (&stringbuf); /* Parse a colon. */ c = phase2_getc (); @@ -571,7 +570,7 @@ extract_rsj (FILE *f, goto invalid_rsj; if (r == pr_syntax || location != NULL) goto invalid_json; - location = mixed_string_buffer_done (stringbuf); + location = mixed_string_buffer_result (&stringbuf); } else if (strcmp (s2, "sourcebytes") == 0) { @@ -607,7 +606,7 @@ extract_rsj (FILE *f, goto invalid_rsj; if (r == pr_syntax || msgid != NULL) goto invalid_json; - msgid = mixed_string_buffer_done (stringbuf); + msgid = mixed_string_buffer_result (&stringbuf); } else goto invalid_rsj; diff --git a/gettext-tools/src/x-vala.c b/gettext-tools/src/x-vala.c index a33c48827..618f87a83 100644 --- a/gettext-tools/src/x-vala.c +++ b/gettext-tools/src/x-vala.c @@ -844,7 +844,7 @@ phase3_get (token_ty *tp) /* FALLTHROUGH */ case '"': { - struct mixed_string_buffer *bp; + struct mixed_string_buffer msb; int c2 = phase1_getc (); if (c2 == '"') @@ -862,16 +862,15 @@ phase3_get (token_ty *tp) phase2_ungetc (c2); /* Start accumulating the string. */ - bp = mixed_string_buffer_alloc (lc_string, - logical_file_name, - line_number); + mixed_string_buffer_init (&msb, lc_string, + logical_file_name, line_number); if (verbatim) for (;;) { c = phase1_getc (); /* Keep line_number in sync. */ - bp->line_number = line_number; + msb.line_number = line_number; if (c == '"') { @@ -887,7 +886,7 @@ phase3_get (token_ty *tp) } if (c == EOF) break; - mixed_string_buffer_append_char (bp, c); + mixed_string_buffer_append_char (&msb, c); } else for (;;) @@ -895,7 +894,7 @@ phase3_get (token_ty *tp) c = phase7_getc (); /* Keep line_number in sync. */ - bp->line_number = line_number; + msb.line_number = line_number; if (c == P7_NEWLINE) { @@ -917,16 +916,16 @@ phase3_get (token_ty *tp) { assert (UNICODE_VALUE (c) >= 0 && UNICODE_VALUE (c) < 0x110000); - mixed_string_buffer_append_unicode (bp, + mixed_string_buffer_append_unicode (&msb, UNICODE_VALUE (c)); } else - mixed_string_buffer_append_char (bp, c); + mixed_string_buffer_append_char (&msb, c); } /* Done accumulating the string. */ tp->type = last_token_type = template ? token_type_string_template : token_type_string_literal; - tp->string = mixed_string_buffer_done (bp); + tp->string = mixed_string_buffer_result (&msb); tp->comment = add_reference (savable_comment); return; } diff --git a/gettext-tools/src/xg-mixed-string.c b/gettext-tools/src/xg-mixed-string.c index f6f72e6b0..0023bc77f 100644 --- a/gettext-tools/src/xg-mixed-string.c +++ b/gettext-tools/src/xg-mixed-string.c @@ -54,16 +54,6 @@ mixed_string_buffer_init (struct mixed_string_buffer *bp, bp->line_number = line_number; } -struct mixed_string_buffer * -mixed_string_buffer_alloc (lexical_context_ty lcontext, - const char *logical_file_name, - int line_number) -{ - struct mixed_string_buffer *bp = XMALLOC (struct mixed_string_buffer); - mixed_string_buffer_init (bp, lcontext, logical_file_name, line_number); - return bp; -} - bool mixed_string_buffer_is_empty (const struct mixed_string_buffer *bp) { @@ -263,14 +253,3 @@ mixed_string_buffer_result (struct mixed_string_buffer *bp) /* Return it. */ return utf8_buffer; } - -char * -mixed_string_buffer_done (struct mixed_string_buffer *bp) -{ - char *result = mixed_string_buffer_result (bp); - - /* Free also bp itself. */ - free (bp); - - return result; -} diff --git a/gettext-tools/src/xg-mixed-string.h b/gettext-tools/src/xg-mixed-string.h index 855e77789..b456e1af9 100644 --- a/gettext-tools/src/xg-mixed-string.h +++ b/gettext-tools/src/xg-mixed-string.h @@ -57,12 +57,6 @@ extern void const char *logical_file_name, int line_number); -/* Creates a fresh mixed_string_buffer. */ -extern struct mixed_string_buffer * - mixed_string_buffer_alloc (lexical_context_ty lcontext, - const char *logical_file_name, - int line_number); - /* Determines whether a mixed_string_buffer is still empty. */ extern bool mixed_string_buffer_is_empty (const struct mixed_string_buffer *bp); @@ -81,9 +75,6 @@ extern void mixed_string_buffer_destroy (struct mixed_string_buffer *bp); and returns the accumulated string in UTF-8. */ extern char * mixed_string_buffer_result (struct mixed_string_buffer *bp); -/* Frees mixed_string_buffer and returns the accumulated string in UTF-8. */ -extern char * mixed_string_buffer_done (struct mixed_string_buffer *bp); - #ifdef __cplusplus }