blueloveTH 1 年間 前
コミット
2616c8378b
7 ファイル変更24 行追加25 行削除
  1. 2 2
      include/pocketpy/obj.h
  2. 12 7
      include/pocketpy/tuplelist.h
  3. 1 5
      src/ceval.cpp
  4. 1 1
      src/compiler.cpp
  5. 6 2
      src/pocketpy.cpp
  6. 0 6
      src/tuplelist.cpp
  7. 2 2
      src/vm.cpp

+ 2 - 2
include/pocketpy/obj.h

@@ -113,8 +113,8 @@ struct PyObject final{
     static constexpr int FIXED_SIZE = 16;
     
     bool gc_marked;     // whether this object is marked
-    Type type;          // we have a duplicated type here for saving memory
-    NameDict* _attr;
+    Type type;          // we have a duplicated type here for convenience
+    NameDict* _attr;    // gc will delete this on destruction
 
     bool is_attr_valid() const noexcept { return _attr != nullptr; }
     void* _value_ptr() noexcept { return (char*)this + FIXED_SIZE; }

+ 12 - 7
include/pocketpy/tuplelist.h

@@ -7,13 +7,8 @@
 
 namespace pkpy {
 
-struct List: pod_vector<PyVar>{
-    using pod_vector<PyVar>::pod_vector;
-    void _gc_mark(VM*) const;
-};
-
 struct Tuple {
-    static const int INLINED_SIZE = 4;
+    static const int INLINED_SIZE = 3;
 
     PyVar* _args;
     PyVar _inlined[INLINED_SIZE];
@@ -22,7 +17,6 @@ struct Tuple {
     Tuple(int n);
     Tuple(const Tuple& other);
     Tuple(Tuple&& other) noexcept;
-    Tuple(List&& other) noexcept;
     ~Tuple();
 
     Tuple(PyVar, PyVar);
@@ -41,6 +35,17 @@ struct Tuple {
     void _gc_mark(VM*) const;
 };
 
+struct List: pod_vector<PyVar>{
+    using pod_vector<PyVar>::pod_vector;
+    void _gc_mark(VM*) const;
+
+    Tuple to_tuple() const{
+        Tuple ret(size());
+        for(int i=0; i<size(); i++) ret[i] = (*this)[i];
+        return ret;
+    }
+};
+
 // a lightweight view for function args, it does not own the memory
 struct ArgsView{
     PyVar* _begin;

+ 1 - 5
src/ceval.cpp

@@ -428,15 +428,13 @@ __NEXT_STEP:
     } DISPATCH()
     /*****************************************/
     case OP_BUILD_TUPLE_UNPACK: {
-        auto _lock = heap.gc_scope_lock();
         List list;
         __unpack_as_list(STACK_VIEW(byte.arg), list);
         STACK_SHRINK(byte.arg);
-        PyVar _0 = VAR(Tuple(std::move(list)));
+        PyVar _0 = VAR(list.to_tuple());
         PUSH(_0);
     } DISPATCH()
     case OP_BUILD_LIST_UNPACK: {
-        auto _lock = heap.gc_scope_lock();
         List list;
         __unpack_as_list(STACK_VIEW(byte.arg), list);
         STACK_SHRINK(byte.arg);
@@ -444,7 +442,6 @@ __NEXT_STEP:
         PUSH(_0);
     } DISPATCH()
     case OP_BUILD_DICT_UNPACK: {
-        auto _lock = heap.gc_scope_lock();
         Dict dict;
         __unpack_as_dict(STACK_VIEW(byte.arg), dict);
         STACK_SHRINK(byte.arg);
@@ -452,7 +449,6 @@ __NEXT_STEP:
         PUSH(_0);
     } DISPATCH()
     case OP_BUILD_SET_UNPACK: {
-        auto _lock = heap.gc_scope_lock();
         List list;
         __unpack_as_list(STACK_VIEW(byte.arg), list);
         STACK_SHRINK(byte.arg);

+ 1 - 1
src/compiler.cpp

@@ -1212,7 +1212,7 @@ __EAT_DOTS_END:
                     if(curr().type == TK(")")) break;
                 }
                 consume(TK(")"));
-                return VAR(Tuple(std::move(cpnts)));
+                return VAR(cpnts.to_tuple());
             }
             default: break;
         }

+ 6 - 2
src/pocketpy.cpp

@@ -21,7 +21,11 @@ PyVar PyArrayGetItem(VM* vm, PyVar _0, PyVar _1){
         vm->parse_int_slice(s, self.size(), start, stop, step);
         List new_list;
         PK_SLICE_LOOP(i, start, stop, step) new_list.push_back(self[i]);
-        return VAR(T(std::move(new_list)));
+
+        if constexpr(std::is_same_v<T, List>)
+            return VAR(std::move(new_list));
+        else
+            return VAR(new_list.to_tuple());
     }
     vm->TypeError("indices must be integers or slices");
 }
@@ -1024,7 +1028,7 @@ void __init_builtins(VM* _vm) {
         if(args.size() == 1+0) return VAR(Tuple(0));
         if(args.size() == 1+1){
             List list = vm->py_list(args[1]);
-            return VAR(Tuple(std::move(list)));
+            return VAR(list.to_tuple());
         }
         vm->TypeError("tuple() takes at most 1 argument");
         return vm->None;

+ 0 - 6
src/tuplelist.cpp

@@ -27,12 +27,6 @@ Tuple::Tuple(Tuple&& other) noexcept {
     }
 }
 
-Tuple::Tuple(List&& other) noexcept {
-    _size = other.size();
-    _args = other._data;
-    other._data = nullptr;
-}
-
 Tuple::Tuple(PyVar _0, PyVar _1): Tuple(2){
     _args[0] = _0;
     _args[1] = _1;

+ 2 - 2
src/vm.cpp

@@ -942,8 +942,8 @@ void VM::__init_builtin_types(){
     this->_main = new_module("__main__");
 }
 
-// `heap.gc_scope_lock();` needed before calling this function
 void VM::__unpack_as_list(ArgsView args, List& list){
+    auto _lock = heap.gc_scope_lock();
     for(PyVar obj: args){
         if(is_type(obj, tp_star_wrapper)){
             const StarWrapper& w = _CAST(StarWrapper&, obj);
@@ -962,8 +962,8 @@ void VM::__unpack_as_list(ArgsView args, List& list){
     }
 }
 
-// `heap.gc_scope_lock();` needed before calling this function
 void VM::__unpack_as_dict(ArgsView args, Dict& dict){
+    auto _lock = heap.gc_scope_lock();
     for(PyVar obj: args){
         if(is_type(obj, tp_star_wrapper)){
             const StarWrapper& w = _CAST(StarWrapper&, obj);