Skip to content

Commit

Permalink
Both Array and Dictionary are always in shared mode (removed copy on …
Browse files Browse the repository at this point in the history
…write).
  • Loading branch information
reduz committed Jan 11, 2017
1 parent 57166cd commit e658311
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 93 deletions.
22 changes: 3 additions & 19 deletions core/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct ArrayPrivate {

SafeRefCount refcount;
Vector<Variant> array;
bool shared;
};

void Array::_ref(const Array& p_from) const {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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() {

Expand Down
4 changes: 1 addition & 3 deletions core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class Array {
bool empty() const;
void clear();

bool is_shared() const;

bool operator==(const Array& p_array) const;

uint32_t hash() const;
Expand Down Expand Up @@ -87,7 +85,7 @@ class Array {
Variant pop_front();

Array(const Array& p_from);
Array(bool p_shared=false);
Array();
~Array();

};
Expand Down
32 changes: 5 additions & 27 deletions core/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct DictionaryPrivate {
SafeRefCount refcount;
HashMap<Variant,Data,_DictionaryVariantHash> variant_map;
int counter;
bool shared;

};

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
7 changes: 2 additions & 5 deletions core/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -62,9 +62,6 @@ class Dictionary {
bool empty() const;
void clear();


bool is_shared() const;

bool has(const Variant& p_key) const;
bool has_all(const Array& p_keys) const;

Expand All @@ -83,7 +80,7 @@ class Dictionary {
Dictionary copy() const;

Dictionary(const Dictionary& p_from);
Dictionary(bool p_shared=false);
Dictionary();
~Dictionary();
};

Expand Down
4 changes: 2 additions & 2 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ 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;
value=d;
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;
Expand Down
12 changes: 6 additions & 6 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++) {

Expand Down Expand Up @@ -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;
Expand All @@ -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++) {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions core/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

}
Expand Down
4 changes: 2 additions & 2 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions core/variant_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gd_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

Expand Down
Loading

0 comments on commit e658311

Please sign in to comment.