Skip to content

Commit eceae18

Browse files
committed
No longer maintain Godot ref-count in InstanceStorage
Extra state to keep in sync, which is already available as RefCounted::get_reference_count(). Also, the reference() and unreference() callbacks aren't invoked on every refcount operation, only for low numbers; otherwise GDExtension may not witness a change. � Conflicts: � godot-core/src/storage/multi_threaded.rs � godot-core/src/storage/single_threaded.rs
1 parent cf45421 commit eceae18

File tree

4 files changed

+4
-34
lines changed

4 files changed

+4
-34
lines changed

godot-core/src/storage/instance_storage.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ pub unsafe trait Storage {
154154

155155
/// An internal trait for keeping track of reference counts for a storage.
156156
pub(crate) trait StorageRefCounted: Storage {
157-
#[allow(dead_code)] // used in `debug-log` feature. Don't micromanage.
158-
fn godot_ref_count(&self) -> u32;
159-
160157
fn on_inc_ref(&self);
161158

162159
fn on_dec_ref(&self);

godot-core/src/storage/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,22 @@ mod log_active {
6565

6666
pub fn log_construct<T: GodotClass>(base: &Base<T::Base>) {
6767
out!(
68-
" Storage::construct: {base:?} (T={ty})",
68+
" Storage::construct: {base:?} (T={ty})",
6969
ty = type_name::<T>()
7070
);
7171
}
7272

7373
pub fn log_inc_ref<T: StorageRefCounted>(storage: &T) {
7474
out!(
75-
" Storage::on_inc_ref (rc={rc}): {base:?} (T={ty})",
76-
rc = T::godot_ref_count(storage),
75+
" Storage::on_inc_ref: {base:?} (T={ty})",
7776
base = storage.base(),
7877
ty = type_name::<T>(),
7978
);
8079
}
8180

8281
pub fn log_dec_ref<T: StorageRefCounted>(storage: &T) {
8382
out!(
84-
" | Storage::on_dec_ref (rc={rc}): {base:?} (T={ty})",
85-
rc = T::godot_ref_count(storage),
83+
" | Storage::on_dec_ref: {base:?} (T={ty})",
8684
base = storage.base(),
8785
ty = type_name::<T>(),
8886
);
@@ -103,8 +101,7 @@ mod log_active {
103101
// Do not Debug-fmt `self.base()` object here, see above.
104102

105103
out!(
106-
" Storage::drop (rc={rc}): {base_id}",
107-
rc = storage.godot_ref_count(),
104+
" Storage::drop: {base_id}",
108105
base_id = storage.base().debug_instance_id(),
109106
);
110107
}

godot-core/src/storage/multi_threaded.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use std::sync::atomic::{AtomicU32, Ordering};
9-
108
#[cfg(not(feature = "experimental-threads"))]
119
use godot_cell::panicking::{GdCell, InaccessibleGuard, MutGuard, RefGuard};
1210

@@ -22,7 +20,6 @@ pub struct InstanceStorage<T: GodotClass> {
2220

2321
// Declared after `user_instance`, is dropped last
2422
pub(super) lifecycle: AtomicLifecycle,
25-
godot_ref_count: AtomicU32,
2623

2724
// No-op in Release mode.
2825
borrow_tracker: DebugBorrowTracker,
@@ -49,7 +46,6 @@ unsafe impl<T: GodotClass> Storage for InstanceStorage<T> {
4946
user_instance: GdCell::new(user_instance),
5047
base,
5148
lifecycle: AtomicLifecycle::new(Lifecycle::Alive),
52-
godot_ref_count: AtomicU32::new(1),
5349
borrow_tracker: DebugBorrowTracker::new(),
5450
}
5551
}
@@ -104,19 +100,11 @@ unsafe impl<T: GodotClass> Storage for InstanceStorage<T> {
104100
}
105101

106102
impl<T: GodotClass> StorageRefCounted for InstanceStorage<T> {
107-
fn godot_ref_count(&self) -> u32 {
108-
self.godot_ref_count.load(Ordering::Relaxed)
109-
}
110-
111103
fn on_inc_ref(&self) {
112-
self.godot_ref_count.fetch_add(1, Ordering::Relaxed);
113-
114104
super::log_inc_ref(self);
115105
}
116106

117107
fn on_dec_ref(&self) {
118-
self.godot_ref_count.fetch_sub(1, Ordering::Relaxed);
119-
120108
super::log_dec_ref(self);
121109
}
122110
}

godot-core/src/storage/single_threaded.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub struct InstanceStorage<T: GodotClass> {
2222

2323
// Declared after `user_instance`, is dropped last
2424
pub(super) lifecycle: cell::Cell<Lifecycle>,
25-
godot_ref_count: cell::Cell<u32>,
2625

2726
// No-op in Release mode.
2827
borrow_tracker: DebugBorrowTracker,
@@ -49,7 +48,6 @@ unsafe impl<T: GodotClass> Storage for InstanceStorage<T> {
4948
user_instance: GdCell::new(user_instance),
5049
base,
5150
lifecycle: cell::Cell::new(Lifecycle::Alive),
52-
godot_ref_count: cell::Cell::new(1),
5351
borrow_tracker: DebugBorrowTracker::new(),
5452
}
5553
}
@@ -101,21 +99,11 @@ unsafe impl<T: GodotClass> Storage for InstanceStorage<T> {
10199
}
102100

103101
impl<T: GodotClass> StorageRefCounted for InstanceStorage<T> {
104-
fn godot_ref_count(&self) -> u32 {
105-
self.godot_ref_count.get()
106-
}
107-
108102
fn on_inc_ref(&self) {
109-
let refc = self.godot_ref_count.get() + 1;
110-
self.godot_ref_count.set(refc);
111-
112103
super::log_inc_ref(self);
113104
}
114105

115106
fn on_dec_ref(&self) {
116-
let refc = self.godot_ref_count.get() - 1;
117-
self.godot_ref_count.set(refc);
118-
119107
super::log_dec_ref(self);
120108
}
121109
}

0 commit comments

Comments
 (0)