summaryrefslogtreecommitdiff
path: root/core/templates
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-06-29 17:51:22 +0200
committerGitHub <noreply@github.com>2021-06-29 17:51:22 +0200
commit916c37de09871be0f5578f2f23f700ecaa08f836 (patch)
treebbb54b367c30c4d17ec19fb0f5357c4343f64908 /core/templates
parent691c754a6c46b7557e5605f479e028447f923df6 (diff)
parent64c925cca6d5c54b0ef4dd0bcdd7a4357f4b86fb (diff)
Merge pull request #50000 from reduz/improve-rid-owner-memory-usage
Improve RID_Owner memory usage
Diffstat (limited to 'core/templates')
-rw-r--r--core/templates/rid_owner.h40
1 files changed, 26 insertions, 14 deletions
diff --git a/core/templates/rid_owner.h b/core/templates/rid_owner.h
index 31278b71bd..e4964f744e 100644
--- a/core/templates/rid_owner.h
+++ b/core/templates/rid_owner.h
@@ -79,7 +79,7 @@ class RID_Alloc : public RID_AllocBase {
SpinLock spin_lock;
- _FORCE_INLINE_ RID _allocate_rid(const T *p_initializer) {
+ _FORCE_INLINE_ RID _allocate_rid() {
if (THREAD_SAFE) {
spin_lock.lock();
}
@@ -114,11 +114,6 @@ class RID_Alloc : public RID_AllocBase {
uint32_t free_chunk = free_index / elements_in_chunk;
uint32_t free_element = free_index % elements_in_chunk;
- if (p_initializer) {
- T *ptr = &chunks[free_chunk][free_element];
- memnew_placement(ptr, T(*p_initializer));
- }
-
uint32_t validator = (uint32_t)(_gen_id() & 0x7FFFFFFF);
uint64_t id = validator;
id <<= 32;
@@ -126,9 +121,7 @@ class RID_Alloc : public RID_AllocBase {
validator_chunks[free_chunk][free_element] = validator;
- if (!p_initializer) {
- validator_chunks[free_chunk][free_element] |= 0x80000000; //mark uninitialized bit
- }
+ validator_chunks[free_chunk][free_element] |= 0x80000000; //mark uninitialized bit
alloc_count++;
@@ -140,13 +133,20 @@ class RID_Alloc : public RID_AllocBase {
}
public:
+ RID make_rid() {
+ RID rid = _allocate_rid();
+ initialize_rid(rid);
+ return rid;
+ }
RID make_rid(const T &p_value) {
- return _allocate_rid(&p_value);
+ RID rid = _allocate_rid();
+ initialize_rid(rid, p_value);
+ return rid;
}
//allocate but don't initialize, use initialize_rid afterwards
RID allocate_rid() {
- return _allocate_rid(nullptr);
+ return _allocate_rid();
}
_FORCE_INLINE_ T *getornull(const RID &p_rid, bool p_initialize = false) {
@@ -207,6 +207,11 @@ public:
return ptr;
}
+ void initialize_rid(RID p_rid) {
+ T *mem = getornull(p_rid, true);
+ ERR_FAIL_COND(!mem);
+ memnew_placement(mem, T);
+ }
void initialize_rid(RID p_rid, const T &p_value) {
T *mem = getornull(p_rid, true);
ERR_FAIL_COND(!mem);
@@ -333,7 +338,7 @@ public:
description = p_descrption;
}
- RID_Alloc(uint32_t p_target_chunk_byte_size = 4096) {
+ RID_Alloc(uint32_t p_target_chunk_byte_size = 65536) {
elements_in_chunk = sizeof(T) > p_target_chunk_byte_size ? 1 : (p_target_chunk_byte_size / sizeof(T));
}
@@ -434,7 +439,7 @@ public:
alloc.set_description(p_descrption);
}
- RID_PtrOwner(uint32_t p_target_chunk_byte_size = 4096) :
+ RID_PtrOwner(uint32_t p_target_chunk_byte_size = 65536) :
alloc(p_target_chunk_byte_size) {}
};
@@ -443,6 +448,9 @@ class RID_Owner {
RID_Alloc<T, THREAD_SAFE> alloc;
public:
+ _FORCE_INLINE_ RID make_rid() {
+ return alloc.make_rid();
+ }
_FORCE_INLINE_ RID make_rid(const T &p_ptr) {
return alloc.make_rid(p_ptr);
}
@@ -451,6 +459,10 @@ public:
return alloc.allocate_rid();
}
+ _FORCE_INLINE_ void initialize_rid(RID p_rid) {
+ alloc.initialize_rid(p_rid);
+ }
+
_FORCE_INLINE_ void initialize_rid(RID p_rid, const T &p_ptr) {
alloc.initialize_rid(p_rid, p_ptr);
}
@@ -486,7 +498,7 @@ public:
void set_description(const char *p_descrption) {
alloc.set_description(p_descrption);
}
- RID_Owner(uint32_t p_target_chunk_byte_size = 4096) :
+ RID_Owner(uint32_t p_target_chunk_byte_size = 65536) :
alloc(p_target_chunk_byte_size) {}
};