From e6583117df95373cffb12105de82d3816ca09f85 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 11 Jan 2017 08:53:31 -0300 Subject: [PATCH] Both Array and Dictionary are always in shared mode (removed copy on write). --- core/array.cpp | 22 ++----------- core/array.h | 4 +-- core/dictionary.cpp | 32 +++---------------- core/dictionary.h | 7 ++-- core/io/json.cpp | 4 +-- core/io/marshalls.cpp | 12 +++---- core/io/resource_format_binary.cpp | 8 ++--- core/variant.cpp | 4 +-- core/variant_call.cpp | 4 +-- core/variant_op.cpp | 6 ++-- modules/gdscript/gd_function.cpp | 4 +-- modules/gdscript/gd_functions.cpp | 8 ++--- modules/gdscript/gd_parser.cpp | 4 +-- modules/visual_script/visual_script_nodes.cpp | 6 ++-- servers/physics_2d_server.cpp | 8 ++--- servers/physics_server.cpp | 8 ++--- .../editor_texture_import_plugin.cpp | 2 +- 17 files changed, 50 insertions(+), 93 deletions(-) diff --git a/core/array.cpp b/core/array.cpp index 50cc9eee471..b9c8f543da5 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -36,7 +36,6 @@ struct ArrayPrivate { SafeRefCount refcount; Vector array; - bool shared; }; void Array::_ref(const Array& p_from) const { @@ -54,20 +53,9 @@ void Array::_ref(const Array& p_from) const { _unref(); - if (_fp->shared) { + _p = p_from._p; - _p = p_from._p; - } else { - - _p = memnew( ArrayPrivate ); - _p->shared=false; - _p->refcount.init(); - _p->array=_fp->array; - - if (_fp->refcount.unref()) - memdelete(_fp); - } } void Array::_unref() const { @@ -106,10 +94,6 @@ void Array::clear() { _p->array.clear(); } -bool Array::is_shared() const { - - return _p->shared; -} bool Array::operator==(const Array& p_array) const { @@ -316,11 +300,11 @@ Array::Array(const Array& p_from) { _ref(p_from); } -Array::Array(bool p_shared) { +Array::Array() { _p = memnew( ArrayPrivate ); _p->refcount.init(); - _p->shared=p_shared; + } Array::~Array() { diff --git a/core/array.h b/core/array.h index af57940d791..16ab16002ce 100644 --- a/core/array.h +++ b/core/array.h @@ -53,8 +53,6 @@ public: bool empty() const; void clear(); - bool is_shared() const; - bool operator==(const Array& p_array) const; uint32_t hash() const; @@ -87,7 +85,7 @@ public: Variant pop_front(); Array(const Array& p_from); - Array(bool p_shared=false); + Array(); ~Array(); }; diff --git a/core/dictionary.cpp b/core/dictionary.cpp index d5d29ca0fc0..3b4d3b65d09 100644 --- a/core/dictionary.cpp +++ b/core/dictionary.cpp @@ -49,7 +49,6 @@ struct DictionaryPrivate { SafeRefCount refcount; HashMap variant_map; int counter; - bool shared; }; @@ -79,23 +78,8 @@ void Dictionary::get_key_list( List *p_keys) const { } -void Dictionary::_copy_on_write() const { - - //make a copy of what we have - if (_p->shared) - return; - - DictionaryPrivate *p = memnew(DictionaryPrivate); - p->shared=_p->shared; - p->variant_map=_p->variant_map; - p->refcount.init(); - _unref(); - _p=p; -} - Variant& Dictionary::operator[](const Variant& p_key) { - _copy_on_write(); DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); @@ -126,7 +110,6 @@ const Variant* Dictionary::getptr(const Variant& p_key) const { Variant* Dictionary::getptr(const Variant& p_key) { - _copy_on_write(); DictionaryPrivate::Data *v =_p->variant_map.getptr(p_key); if (!v) return NULL; @@ -171,7 +154,8 @@ bool Dictionary::has_all(const Array& p_keys) const { } void Dictionary::erase(const Variant& p_key) { - _copy_on_write(); + + _p->variant_map.erase(p_key); } @@ -199,16 +183,10 @@ void Dictionary::_ref(const Dictionary& p_from) const { void Dictionary::clear() { - _copy_on_write(); _p->variant_map.clear(); _p->counter=0; } -bool Dictionary::is_shared() const { - - return _p->shared; -} - void Dictionary::_unref() const { @@ -278,7 +256,7 @@ const Variant* Dictionary::next(const Variant* p_key) const { Dictionary Dictionary::copy() const { - Dictionary n(is_shared()); + Dictionary n; List keys; get_key_list(&keys); @@ -304,12 +282,12 @@ Dictionary::Dictionary(const Dictionary& p_from) { } -Dictionary::Dictionary(bool p_shared) { +Dictionary::Dictionary() { _p=memnew( DictionaryPrivate ); _p->refcount.init(); _p->counter=0; - _p->shared=p_shared; + } Dictionary::~Dictionary() { diff --git a/core/dictionary.h b/core/dictionary.h index 9fab653470c..a1bf291d6ba 100644 --- a/core/dictionary.h +++ b/core/dictionary.h @@ -43,7 +43,7 @@ class Dictionary { mutable DictionaryPrivate *_p; - void _copy_on_write() const; + void _ref(const Dictionary& p_from) const; void _unref() const; public: @@ -62,9 +62,6 @@ public: bool empty() const; void clear(); - - bool is_shared() const; - bool has(const Variant& p_key) const; bool has_all(const Array& p_keys) const; @@ -83,7 +80,7 @@ public: Dictionary copy() const; Dictionary(const Dictionary& p_from); - Dictionary(bool p_shared=false); + Dictionary(); ~Dictionary(); }; diff --git a/core/io/json.cpp b/core/io/json.cpp index ed1e74967bc..c0aa530a123 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -288,7 +288,7 @@ Error JSON::_parse_value(Variant &value,Token& token,const CharType *p_str,int & if (token.type==TK_CURLY_BRACKET_OPEN) { - Dictionary d(true); + Dictionary d; Error err = _parse_object(d,p_str,index,p_len,line,r_err_str); if (err) return err; @@ -296,7 +296,7 @@ Error JSON::_parse_value(Variant &value,Token& token,const CharType *p_str,int & return OK; } else if (token.type==TK_BRACKET_OPEN) { - Array a(true); + Array a; Error err = _parse_array(a,p_str,index,p_len,line,r_err_str); if (err) return err; diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index 67baa117cb5..e958edc93e9 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -471,7 +471,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * ERR_FAIL_COND_V(len<4,ERR_INVALID_DATA); uint32_t count = decode_uint32(buf); - bool shared = count&0x80000000; + // bool shared = count&0x80000000; count&=0x7FFFFFFF; buf+=4; @@ -481,7 +481,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int * (*r_len)+=4; } - Dictionary d(shared); + Dictionary d; for(uint32_t i=0;iget_32(); - Dictionary d(len&0x80000000); //last bit means shared + Dictionary d; //last bit means shared len&=0x7FFFFFFF; for(uint32_t i=0;iget_32(); - Array a(len&0x80000000); //last bit means shared + Array a; //last bit means shared len&=0x7FFFFFFF; a.resize(len); for(uint32_t i=0;istore_32(VARIANT_DICTIONARY); Dictionary d = p_property; - f->store_32(uint32_t(d.size())|(d.is_shared()?0x80000000:0)); + f->store_32(uint32_t(d.size())); List keys; d.get_key_list(&keys); @@ -1721,7 +1721,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, f->store_32(VARIANT_ARRAY); Array a=p_property; - f->store_32(uint32_t(a.size())|(a.is_shared()?0x80000000:0)); + f->store_32(uint32_t(a.size())); for(int i=0;i(_data._mem)->is_shared(); - case DICTIONARY: return reinterpret_cast(_data._mem)->is_shared(); + case ARRAY: return true; + case DICTIONARY: return true; default: {} } diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 9a61dd73dff..0c8b40539d4 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -489,7 +489,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM0(Array,sort); VCALL_LOCALMEM2(Array,sort_custom); VCALL_LOCALMEM0(Array,invert); - VCALL_LOCALMEM0R(Array,is_shared); + static void _call_PoolByteArray_get_string_from_ascii(Variant& r_ret,Variant& p_self,const Variant** p_args) { @@ -1588,7 +1588,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC0(ARRAY,NIL,Array,sort,varray()); ADDFUNC2(ARRAY,NIL,Array,sort_custom,OBJECT,"obj",STRING,"func",varray()); ADDFUNC0(ARRAY,NIL,Array,invert,varray()); - ADDFUNC0(ARRAY,BOOL,Array,is_shared,varray()); + ADDFUNC0(POOL_BYTE_ARRAY,INT,PoolByteArray,size,varray()); ADDFUNC2(POOL_BYTE_ARRAY,NIL,PoolByteArray,set,INT,"idx",INT,"byte",varray()); diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 1e67d81ae2e..281d2e81921 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -507,7 +507,7 @@ void Variant::evaluate(const Operator& p_op, const Variant& p_a, const Variant& } const Array &array_a=*reinterpret_cast(p_a._data._mem); const Array &array_b=*reinterpret_cast(p_b._data._mem); - Array sum(array_a.is_shared() || array_b.is_shared()); + Array sum; int asize=array_a.size(); int bsize=array_b.size(); sum.resize(asize+bsize); @@ -2949,7 +2949,7 @@ bool Variant::iter_init(Variant& r_iter,bool &valid) const { #endif Variant::CallError ce; ce.error=Variant::CallError::CALL_OK; - Array ref(true); + Array ref; ref.push_back(r_iter); Variant vref=ref; const Variant *refp[]={&vref}; @@ -3075,7 +3075,7 @@ bool Variant::iter_next(Variant& r_iter,bool &valid) const { #endif Variant::CallError ce; ce.error=Variant::CallError::CALL_OK; - Array ref(true); + Array ref; ref.push_back(r_iter); Variant vref=ref; const Variant *refp[]={&vref}; diff --git a/modules/gdscript/gd_function.cpp b/modules/gdscript/gd_function.cpp index e3217e92185..66599886025 100644 --- a/modules/gdscript/gd_function.cpp +++ b/modules/gdscript/gd_function.cpp @@ -617,7 +617,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a CHECK_SPACE(1); int argc=_code_ptr[ip+1]; - Array array(true); //arrays are always shared + Array array; //arrays are always shared array.resize(argc); CHECK_SPACE(argc+2); @@ -638,7 +638,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a CHECK_SPACE(1); int argc=_code_ptr[ip+1]; - Dictionary dict(true); //arrays are always shared + Dictionary dict; //arrays are always shared CHECK_SPACE(argc*2+2); diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index 35ceeeb1aa4..1c41b2e73bd 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -735,7 +735,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va VALIDATE_ARG_NUM(0); int count=*p_args[0]; - Array arr(true); + Array arr; if (count<=0) { r_ret=arr; return; @@ -761,7 +761,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va int from=*p_args[0]; int to=*p_args[1]; - Array arr(true); + Array arr; if (from>=to) { r_ret=arr; return; @@ -792,7 +792,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va return; } - Array arr(true); + Array arr; if (from>=to && incr>0) { r_ret=arr; return; @@ -921,7 +921,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va NodePath cp(sname,Vector(),false); - Dictionary d(true); + Dictionary d; d["@subpath"]=cp; d["@path"]=p->path; diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index adf13e0a3bd..1c4616555e2 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -1317,7 +1317,7 @@ GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) { //reduce constant array expression ConstantNode *cn = alloc_node(); - Array arr(!p_to_const); + Array arr; //print_line("mk array "+itos(!p_to_const)); arr.resize(an->elements.size()); for(int i=0;ielements.size();i++) { @@ -1352,7 +1352,7 @@ GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) { //reduce constant array expression ConstantNode *cn = alloc_node(); - Dictionary dict(!p_to_const); + Dictionary dict; for(int i=0;ielements.size();i++) { ConstantNode *key_c = static_cast(dn->elements[i].key); ConstantNode *value_c = static_cast(dn->elements[i].value); diff --git a/modules/visual_script/visual_script_nodes.cpp b/modules/visual_script/visual_script_nodes.cpp index 0ccdfedb819..c9e24fc9dcd 100644 --- a/modules/visual_script/visual_script_nodes.cpp +++ b/modules/visual_script/visual_script_nodes.cpp @@ -2631,9 +2631,9 @@ public: return 0; } #endif - Array in_values(true); - Array out_values(true); - Array work_mem(true); + Array in_values; + Array out_values; + Array work_mem; in_values.resize(in_count); diff --git a/servers/physics_2d_server.cpp b/servers/physics_2d_server.cpp index 666982ebee4..c175c04ee9b 100644 --- a/servers/physics_2d_server.cpp +++ b/servers/physics_2d_server.cpp @@ -241,9 +241,9 @@ Dictionary Physics2DDirectSpaceState::_intersect_ray(const Vector2& p_from, cons bool res = intersect_ray(p_from,p_to,inters,exclude,p_layers,p_object_type_mask); if (!res) - return Dictionary(true); + return Dictionary(); - Dictionary d(true); + Dictionary d; d["position"]=inters.position; d["normal"]=inters.normal; d["collider_id"]=inters.collider_id; @@ -282,7 +282,7 @@ Array Physics2DDirectSpaceState::_cast_motion(const Refshape,psq->transform,psq->motion,psq->margin,closest_safe,closest_unsafe,psq->exclude,psq->layer_mask,psq->object_type_mask); if (!res) return Array(); - Array ret(true); + Array ret; ret.resize(2); ret[0]=closest_safe; ret[1]=closest_unsafe; @@ -339,7 +339,7 @@ Dictionary Physics2DDirectSpaceState::_get_rest_info(const Refshape,psq->transform,psq->motion,psq->margin,&sri,psq->exclude,psq->layer_mask,psq->object_type_mask); - Dictionary r(true); + Dictionary r; if (!res) return r; diff --git a/servers/physics_server.cpp b/servers/physics_server.cpp index bbbbfdf3ab9..8f57fcfbdbb 100644 --- a/servers/physics_server.cpp +++ b/servers/physics_server.cpp @@ -271,9 +271,9 @@ Dictionary PhysicsDirectSpaceState::_intersect_ray(const Vector3& p_from, const bool res = intersect_ray(p_from,p_to,inters,exclude,p_layers,p_object_type_mask); if (!res) - return Dictionary(true); + return Dictionary(); - Dictionary d(true); + Dictionary d; d["position"]=inters.position; d["normal"]=inters.normal; d["collider_id"]=inters.collider_id; @@ -310,7 +310,7 @@ Array PhysicsDirectSpaceState::_cast_motion(const Refshape,psq->transform,p_motion,psq->margin,closest_safe,closest_unsafe,psq->exclude,psq->layer_mask,psq->object_type_mask); if (!res) return Array(); - Array ret(true); + Array ret; ret.resize(2); ret[0]=closest_safe; ret[1]=closest_unsafe; @@ -337,7 +337,7 @@ Dictionary PhysicsDirectSpaceState::_get_rest_info(const Refshape,psq->transform,psq->margin,&sri,psq->exclude,psq->layer_mask,psq->object_type_mask); - Dictionary r(true); + Dictionary r; if (!res) return r; diff --git a/tools/editor/io_plugins/editor_texture_import_plugin.cpp b/tools/editor/io_plugins/editor_texture_import_plugin.cpp index fd18dc6cb89..3bf2551778d 100644 --- a/tools/editor/io_plugins/editor_texture_import_plugin.cpp +++ b/tools/editor/io_plugins/editor_texture_import_plugin.cpp @@ -1372,7 +1372,7 @@ Error EditorTextureImportPlugin::import2(const String& p_path, const Refget_region()); rects.push_back(atlases[i]->get_margin());