[goffice] dtoa: speed-up shortest-representation.



commit a5a8c1f9b500a025a899d9549b4311587db189d9
Author: Morten Welinder <terra gnome org>
Date:   Tue Apr 29 09:09:01 2014 -0400

    dtoa: speed-up shortest-representation.
    
    Detect early when we make no progress and have made a sudden progress.

 ChangeLog              |    1 +
 goffice/math/go-dtoa.c |   35 +++++++++++++++++++++--------------
 2 files changed, 22 insertions(+), 14 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b7b0705..ccfbcdf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2014-04-29  Morten Welinder  <terra gnome org>
 
        * goffice/math/go-dtoa.c (fmt_fp): Fix FMR.
+       (go_dtoa): Speed up shortest-representation.
 
 2014-04-28  Morten Welinder  <terra gnome org>
 
diff --git a/goffice/math/go-dtoa.c b/goffice/math/go-dtoa.c
index 7434076..579ad38 100644
--- a/goffice/math/go-dtoa.c
+++ b/goffice/math/go-dtoa.c
@@ -496,12 +496,12 @@ go_dtoa (GString *dst, const char *fmt, ...)
        if (debug) g_printerr ("  --> %s\n", dst->str);
 
        if (fl & FLAG_SHORTEST) {
+               GString *alt = g_string_new (NULL);
                const char *dec = (fl & FLAG_ASCII)
                        ? "."
                        : go_locale_get_decimal()->str;
-               gboolean again = TRUE;
-               while (again) {
-                       GString *alt;
+               while (p > 0) {
+                       ssize_t len_dif;
                        const char *dot = strstr (dst->str + oldlen, dec);
                        long double dalt;
                        if (!dot)
@@ -513,20 +513,27 @@ go_dtoa (GString *dst, const char *fmt, ...)
                         * that the result round-trips.
                         */
 
-                       alt = g_string_new (NULL);
-                       fmt_fp (alt, d, w, p - 1, fl, t);
+                       fmt_fp (alt, d, w, p - 1, fl | FLAG_TRUNCATE, t);
+                       len_dif = dst->len - oldlen - alt->len;
+
+                       if (len_dif == 0) {
+                               /* Same string so we've already removed 0s. */
+                               break;
+                       }
+
                        dalt = strto (alt->str, is_long, (fl & FLAG_ASCII));
+                       if (dalt != d)
+                               break;
+
+                       g_string_truncate (dst, oldlen);
+                       go_string_append_gstring (dst, alt);
+                       if (debug) g_printerr ("  --> %s\n", dst->str);
 
-                       if (dalt == d) {
-                               g_string_truncate (dst, oldlen);
-                               go_string_append_gstring (dst, alt);
-                               if (debug) g_printerr ("  --> %s\n", dst->str);
-                               p--;
-                               again = (p > 0);
-                       } else
-                               again = FALSE;
+                       p--;
 
-                       g_string_free (alt, TRUE);
+                       if (len_dif > 1)
+                               break;
                }
+               g_string_free (alt, TRUE);
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]