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