]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Optimize away a memory allocation.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 19:20:37 +0000 (20:20 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 23:25:34 +0000 (00:25 +0100)
* 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.

gettext-tools/src/x-c.c
gettext-tools/src/x-csharp.c
gettext-tools/src/x-javascript.c
gettext-tools/src/x-python.c
gettext-tools/src/x-rst.c
gettext-tools/src/x-vala.c
gettext-tools/src/xg-mixed-string.c
gettext-tools/src/xg-mixed-string.h

index 3b4509955ac3a6a21b78bc29cf3080a6e88e5f60..fe1c6ebe6cfc5d1a13641a64a0fa8b17b6553368 100644 (file)
@@ -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;
       }
index 4656e5459844943c5763659cbf341b32c44d8130..692f0f6b50ff0ff795c333ae67def42e038d2e30 100644 (file)
@@ -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;
index ed0a09857e52911e1e08aa3fa7da787508dce9f3..d69157440af305de298d5fafba9ef3feb57c21a0 100644 (file)
@@ -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 '+':
index 3cf8cb1c988b82c88ee0118eda3fd80ea9352745..5ab722f34dca988d3960d4c223288bab03c89e19 100644 (file)
@@ -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;
           }
 
index 886163cc17fa6ef5d4175b50f8538737d7b40bc3..847d60bd84ad3133d84979ac5cb1dcda926bc4ea 100644 (file)
@@ -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;
index a33c48827dff879a742c9fef1a643131d351e65a..618f87a83ebc60ef349d804bf5a62a2ac8532885 100644 (file)
@@ -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;
           }
index f6f72e6b08456d4665db53886740777b543766f8..0023bc77f3fb5cab001178f2aa54694385860627 100644 (file)
@@ -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;
-}
index 855e77789639edd8ea3180e66735c964b87b3d51..b456e1af9c41a6e7bd3fe53f76733e786b70502a 100644 (file)
@@ -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
 }