Parcourir la source

improve number parsing speed

blueloveTH il y a 2 ans
Parent
commit
b26dbf08cc
5 fichiers modifiés avec 7 ajouts et 31 suppressions
  1. 0 6
      include/pocketpy/common.h
  2. 1 1
      include/pocketpy/lexer.h
  3. 0 0
      src/lexer.cpp
  4. 6 2
      src/pocketpy.cpp
  5. 0 22
      tests/99_builtin_func.py

+ 0 - 6
include/pocketpy/common.h

@@ -74,18 +74,12 @@ template <>
 struct NumberTraits<4> {
 	using int_t = int32_t;
 	using float_t = float;
-
-	template<typename... Args>
-	static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
 };
 
 template <>
 struct NumberTraits<8> {
 	using int_t = int64_t;
 	using float_t = double;
-
-	template<typename... Args>
-	static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
 };
 
 using Number = NumberTraits<sizeof(void*)>;

+ 1 - 1
include/pocketpy/lexer.h

@@ -138,6 +138,6 @@ struct Lexer {
     std::vector<Token> run();
 };
 
-bool parse_int(std::string_view text, i64* out, int base=10);
+bool parse_int(std::string_view text, i64* out, int base);
 
 } // namespace pkpy

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
src/lexer.cpp


+ 6 - 2
src/pocketpy.cpp

@@ -498,12 +498,16 @@ void init_builtins(VM* _vm) {
             const Str& s = CAST(Str&, args[1]);
             if(s == "inf") return VAR(INFINITY);
             if(s == "-inf") return VAR(-INFINITY);
+
+            double float_out;
+            char* p_end;
             try{
-                f64 val = Number::stof(s.str());
-                return VAR(val);
+                float_out = std::strtod(s.begin(), &p_end);
+                PK_ASSERT(p_end == s.end());
             }catch(...){
                 vm->ValueError("invalid literal for float(): " + s.escape());
             }
+            return VAR(float_out);
         }
         vm->TypeError("invalid arguments for float()");
         return vm->None;

+ 0 - 22
tests/99_builtin_func.py

@@ -346,28 +346,6 @@ assert type(10//11) is int
 # test int.__mod__:
 assert type(11%2) is int
 
-
-# /************ float ************/
-#       136:  433:    _vm->bind_constructor<2>("float", [](VM* vm, ArgsView args) {
-#        10:  434:        if (is_type(args[1], vm->tp_int)) return VAR((f64)CAST(i64, args[1]));
-#         9:  435:        if (is_type(args[1], vm->tp_float)) return args[1];
-#         3:  436:        if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1.0 : 0.0);
-#         3:  437:        if (is_type(args[1], vm->tp_str)) {
-#         3:  438:            const Str& s = CAST(Str&, args[1]);
-#         3:  439:            if(s == "inf") return VAR(INFINITY);
-#         2:  440:            if(s == "-inf") return VAR(-INFINITY);
-#         -:  441:            try{
-#         2:  442:                f64 val = Number::stof(s.str());
-#         2:  443:                return VAR(val);
-#     #####:  444:            }catch(...){
-#     #####:  445:                vm->ValueError("invalid literal for float(): " + s.escape());
-#     #####:  446:            }
-#     #####:  447:        }
-#     #####:  448:        vm->TypeError("float() argument must be a int, float, bool or str");
-#     #####:  449:        return vm->None;
-#        10:  450:    });
-# test float:
-
 try:
     float('asad')
     print('未能拦截错误, 在测试 float')

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff