From c442fd96e6853f4023479355680b0023aab6975d Mon Sep 17 00:00:00 2001 From: "Alexander V. Brezgin" Date: Mon, 21 Nov 2016 09:09:56 +0300 Subject: [PATCH] Optimize Value::isIntegral() method Worst case called modf() twice --- src/lib_json/json_value.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 391c20f..6c9ef2e 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1360,11 +1360,23 @@ bool Value::isUInt64() const { } bool Value::isIntegral() const { + switch (type_) { + case intValue: + case uintValue: + return true; + case realValue: #if defined(JSON_HAS_INT64) - return isInt64() || isUInt64(); + // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a + // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we + // require the value to be strictly less than the limit. + return value_.real_ >= double(minInt64) && value_.real_ < maxUInt64AsDouble && IsIntegral(value_.real_); #else - return isInt() || isUInt(); -#endif + return value_.real_ >= minInt && value_.real_ <= maxUInt && IsIntegral(value_.real_); +#endif // JSON_HAS_INT64 + default: + break; + } + return false; } bool Value::isDouble() const { return type_ == intValue || type_ == uintValue || type_ == realValue; }