some optimize

This commit is contained in:
blueloveTH 2023-02-22 22:50:37 +08:00
parent 12977fd9ec
commit 08601671dc
3 changed files with 20 additions and 22 deletions

View File

@ -19,8 +19,11 @@ namespace pkpy{
}; };
template <typename T> template <typename T>
class shared_ptr { struct shared_ptr {
int* counter; union {
int* counter;
i64 bits;
};
#define _t() (T*)(counter + 1) #define _t() (T*)(counter + 1)
#define _inc_counter() if(!is_tagged() && counter) ++(*counter) #define _inc_counter() if(!is_tagged() && counter) ++(*counter)
@ -74,20 +77,14 @@ namespace pkpy{
counter = nullptr; counter = nullptr;
} }
template <typename __VAL>
inline __VAL cast() const {
static_assert(std::is_same_v<T, PyObject>, "T must be PyObject");
return reinterpret_cast<__VAL>(counter);
}
inline constexpr bool is_tagged() const { inline constexpr bool is_tagged() const {
if constexpr(!std::is_same_v<T, PyObject>) return false; if constexpr(!std::is_same_v<T, PyObject>) return false;
return (reinterpret_cast<i64>(counter) & 0b11) != 0b00; return (bits & 0b11) != 0b00;
} }
inline bool is_tag_00() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b00; } inline bool is_tag_00() const { return (bits & 0b11) == 0b00; }
inline bool is_tag_01() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b01; } inline bool is_tag_01() const { return (bits & 0b11) == 0b01; }
inline bool is_tag_10() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b10; } inline bool is_tag_10() const { return (bits & 0b11) == 0b10; }
inline bool is_tag_11() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b11; } inline bool is_tag_11() const { return (bits & 0b11) == 0b11; }
}; };
#undef _t #undef _t
@ -112,8 +109,9 @@ namespace pkpy{
} }
}; };
static_assert(sizeof(i64) == sizeof(pkpy::shared_ptr<PyObject>)); static_assert(sizeof(i64) == sizeof(int*));
static_assert(sizeof(f64) == sizeof(pkpy::shared_ptr<PyObject>)); static_assert(sizeof(f64) == sizeof(int*));
static_assert(sizeof(pkpy::shared_ptr<PyObject>) == sizeof(int*));
static_assert(std::numeric_limits<float>::is_iec559); static_assert(std::numeric_limits<float>::is_iec559);
static_assert(std::numeric_limits<double>::is_iec559); static_assert(std::numeric_limits<double>::is_iec559);

View File

@ -124,11 +124,11 @@ inline bool is_type(const PyVar& obj, Type type) noexcept {
} }
inline bool is_both_int_or_float(const PyVar& a, const PyVar& b) noexcept { inline bool is_both_int_or_float(const PyVar& a, const PyVar& b) noexcept {
return ((a.cast<i64>() | b.cast<i64>()) & 0b11) != 0b00; return ((a.bits | b.bits) & 0b11) != 0b00;
} }
inline bool is_both_int(const PyVar& a, const PyVar& b) noexcept { inline bool is_both_int(const PyVar& a, const PyVar& b) noexcept {
return (a.cast<i64>() & b.cast<i64>() & 0b11) == 0b01; return (a.bits & b.bits & 0b11) == 0b01;
} }
inline bool is_int(const PyVar& obj) noexcept { inline bool is_int(const PyVar& obj) noexcept {

View File

@ -554,11 +554,11 @@ public:
inline i64 PyInt_AS_C(const PyVar& obj){ inline i64 PyInt_AS_C(const PyVar& obj){
check_type(obj, tp_int); check_type(obj, tp_int);
return obj.cast<i64>() >> 2; return obj.bits >> 2;
} }
inline i64 _PyInt_AS_C(const PyVar& obj){ inline i64 _PyInt_AS_C(const PyVar& obj){
return obj.cast<i64>() >> 2; return obj.bits >> 2;
} }
inline PyVar PyFloat(f64 value) { inline PyVar PyFloat(f64 value) {
@ -570,13 +570,13 @@ public:
inline f64 PyFloat_AS_C(const PyVar& obj){ inline f64 PyFloat_AS_C(const PyVar& obj){
check_type(obj, tp_float); check_type(obj, tp_float);
i64 bits = obj.cast<i64>(); i64 bits = obj.bits;
bits = (bits >> 2) << 2; bits = (bits >> 2) << 2;
return __8B(bits)._float; return __8B(bits)._float;
} }
inline f64 _PyFloat_AS_C(const PyVar& obj){ inline f64 _PyFloat_AS_C(const PyVar& obj){
i64 bits = obj.cast<i64>(); i64 bits = obj.bits;
bits = (bits >> 2) << 2; bits = (bits >> 2) << 2;
return __8B(bits)._float; return __8B(bits)._float;
} }
@ -663,7 +663,7 @@ public:
} }
return x; return x;
} }
if (is_type(obj, tp_type)) return obj.cast<i64>(); if (is_type(obj, tp_type)) return obj.bits;
if (is_type(obj, tp_bool)) return PyBool_AS_C(obj) ? 1 : 0; if (is_type(obj, tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
if (is_float(obj)){ if (is_float(obj)){
f64 val = PyFloat_AS_C(obj); f64 val = PyFloat_AS_C(obj);