Currently, Ref and RefMut both have size of two pointers because they store both the address of the value and the address of the borrow flag. But it seems that we only need to store one of those two addresses, the other one can be calculated with the offset of borrow field and value field in RefCell:
Heck, you don’t even need something that clever: they can just store &RefCell and build everything on top of that.
My guess is that Ref and RefMut are usually found on the stack, and so it’s not really worth optimizing for their size. Saving both addresses directly means not having to calculate either when accessing the value and when the borrow ends. It probably also makes implementation simpler. But, it could still be worth benchmarking, if you have the time to whip up a pull request. (Not that I’m in charge of what is and isn’t worth benchmarking.)
There’s also always a possibility that I’m missing a circumstance where you have many outstanding Refs and thus their in-memory size starts to matter. Do you have such a use case?
I see. A possible solution is to add a new RefMap type that contains two pointers for map function, but it’s too late to change the implementation now.