in reply to Re: How is redefining a sub internally done?
in thread How is redefining a sub internally done?

Normally, sub calls are compiled into a typeglob retrieval followed by a retrieval of the CV slot within that typeglob. The GV OP has a pointer to the GV associated with the name hard-baked in at compile time. The OP_GV pushes the GV on the stack, then the OP_ENTERSUB pops thes the GV off the stack, accesses its CV slot, calls the associated CV.

However as an optimisation, GVs which only have their CV slot used, are instead created as an RV to a CV. So for example, for

package FOO; sub f { ... } f()
at compile time, the value of the hash entry $FOO::{f} is created as an RV to the CV associated with f, rather than as a full typeglob. When the GV op is compiled, it points to that RV. When the GV op is called, it pushes that RV onto the stack. When the ENTERSUB is is called, it pops that value, notices that it's an RV rather than a GV, and extracts the CV as the thing referenced.

When things get more complex, the 'RV to a CV' SV is upgraded to a full GV with the CV in its code slot.

An 'RV to CV' is smaller and quicker than a full GV (a GV points to a GP which has a CV slot - so two allocations, two dereferences).

Dave.