From 3a8c14ce6bf328978d8dca3b3bb29bf5fd02a122 Mon Sep 17 00:00:00 2001 From: Xie Han <63350856@qq.com> Date: Wed, 27 Dec 2023 21:10:16 +0800 Subject: [PATCH] Optimize evaluating JSON number. --- src/util/json_parser.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/util/json_parser.c b/src/util/json_parser.c index 65e9cba019..c602834bed 100644 --- a/src/util/json_parser.c +++ b/src/util/json_parser.c @@ -380,23 +380,18 @@ static double __evaluate_json_number(const char *integer, } } - num = mant; - if (exp != 0 && figures != 0) - { - if (exp > 309 - figures) - num = INFINITY; - else if (exp > 0) - num *= __power_of_10[exp]; - else if (exp > -309) - num /= __power_of_10[-exp]; - else if (exp > -324 - figures) - { - num /= __power_of_10[-exp - 308]; - num /= __power_of_10[308]; - } - else - num = 0.0; - } + if (exp == 0 || figures == 0) + num = mant; + else if (exp > 291) + num = INFINITY; + else if (exp > 0) + num = mant * __power_of_10[exp]; + else if (exp > -309) + num = mant / __power_of_10[-exp]; + else if (exp > -324 - figures) + num = mant / __power_of_10[-exp - 308] / __power_of_10[308]; + else + num = 0.0; return sign ? -num : num; }