3 ms·
Something I've wondered about reference counting and optimising compilers: I assume at a lowish level an inlined function call might look something like, p
by repsilat 11y ago
Something I've wondered about reference counting and optimising compilers: I assume at a lowish level an inlined function call might look something like,
pointer->ref_count += 1
obj = pointer->ptr
//...do something with obj...
pointer->ref_count -= 1
if pointer->ref_count == 0:
free(pointer->ptr)
Assuming ref_count isn't messed up by another thread (which I guess we're free to do if we haven't put locks around it, or declared it volatile?) then you could optimise away the increment and decrement on `ref_count`, right? I guess I wouldn't be surprised either way.
What I'd like to know, though, is whether the `ref_count == 0` check could get optimised away as well. To you and me it's obvious that the count is positive at the start (as a precondition for the pointer access being correct), but I can't really see how an optimising compiler could know that.