Both Array and Dictionary are always in shared mode (removed copy on write).

This commit is contained in:
Juan Linietsky 2017-01-11 08:53:31 -03:00
parent 57166cd292
commit e6583117df
17 changed files with 50 additions and 93 deletions

View file

@ -36,7 +36,6 @@ struct ArrayPrivate {
SafeRefCount refcount;
Vector<Variant> 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() {

View file

@ -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();
};

View file

@ -49,7 +49,6 @@ struct DictionaryPrivate {
SafeRefCount refcount;
HashMap<Variant,Data,_DictionaryVariantHash> variant_map;
int counter;
bool shared;
};
@ -79,23 +78,8 @@ void Dictionary::get_key_list( List<Variant> *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<Variant> 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() {

View file

@ -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();
};

View file

@ -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;

View file

@ -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;i<count;i++) {
@ -516,7 +516,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;
@ -526,7 +526,7 @@ Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int *
(*r_len)+=4;
}
Array varr(shared);
Array varr;
for(uint32_t i=0;i<count;i++) {
@ -1232,7 +1232,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) {
Dictionary d = p_variant;
if (buf) {
encode_uint32(uint32_t(d.size())|(d.is_shared()?0x80000000:0),buf);
encode_uint32(uint32_t(d.size()),buf);
buf+=4;
}
r_len+=4;
@ -1275,7 +1275,7 @@ Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len) {
Array v = p_variant;
if (buf) {
encode_uint32(uint32_t(v.size())|(v.is_shared()?0x80000000:0),buf);
encode_uint32(uint32_t(v.size()),buf);
buf+=4;
}

View file

@ -426,7 +426,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) {
case VARIANT_DICTIONARY: {
uint32_t len=f->get_32();
Dictionary d(len&0x80000000); //last bit means shared
Dictionary d; //last bit means shared
len&=0x7FFFFFFF;
for(uint32_t i=0;i<len;i++) {
Variant key;
@ -442,7 +442,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant& r_v) {
case VARIANT_ARRAY: {
uint32_t len=f->get_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;i<len;i++) {
@ -1701,7 +1701,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property,
f->store_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<Variant> 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<a.size();i++) {
write_variant(a[i]);

View file

@ -2985,8 +2985,8 @@ bool Variant::is_shared() const {
switch(type) {
case OBJECT: return true;
case ARRAY: return reinterpret_cast<const Array*>(_data._mem)->is_shared();
case DICTIONARY: return reinterpret_cast<const Dictionary*>(_data._mem)->is_shared();
case ARRAY: return true;
case DICTIONARY: return true;
default: {}
}

View file

@ -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());

View file

@ -507,7 +507,7 @@ void Variant::evaluate(const Operator& p_op, const Variant& p_a, const Variant&
}
const Array &array_a=*reinterpret_cast<const Array *>(p_a._data._mem);
const Array &array_b=*reinterpret_cast<const Array *>(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};

View file

@ -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);

View file

@ -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<StringName>(),false);
Dictionary d(true);
Dictionary d;
d["@subpath"]=cp;
d["@path"]=p->path;

View file

@ -1317,7 +1317,7 @@ GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) {
//reduce constant array expression
ConstantNode *cn = alloc_node<ConstantNode>();
Array arr(!p_to_const);
Array arr;
//print_line("mk array "+itos(!p_to_const));
arr.resize(an->elements.size());
for(int i=0;i<an->elements.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<ConstantNode>();
Dictionary dict(!p_to_const);
Dictionary dict;
for(int i=0;i<dn->elements.size();i++) {
ConstantNode *key_c = static_cast<ConstantNode*>(dn->elements[i].key);
ConstantNode *value_c = static_cast<ConstantNode*>(dn->elements[i].value);

View file

@ -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);

View file

@ -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 Ref<Physics2DShapeQueryParam
bool res = cast_motion(psq->shape,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 Ref<Physics2DShapeQue
ShapeRestInfo sri;
bool res = rest_info(psq->shape,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;

View file

@ -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 Ref<PhysicsShapeQueryParameter
bool res = cast_motion(psq->shape,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 Ref<PhysicsShapeQueryPa
ShapeRestInfo sri;
bool res = rest_info(psq->shape,psq->transform,psq->margin,&sri,psq->exclude,psq->layer_mask,psq->object_type_mask);
Dictionary r(true);
Dictionary r;
if (!res)
return r;

View file

@ -1372,7 +1372,7 @@ Error EditorTextureImportPlugin::import2(const String& p_path, const Ref<Resourc
if (p_external) {
//used by exporter
Array rects(true);
Array rects;
for(int i=0;i<atlases.size();i++) {
rects.push_back(atlases[i]->get_region());
rects.push_back(atlases[i]->get_margin());