Alternative schemes for implementing vptr?

前端 未结 4 759
北海茫月
北海茫月 2021-01-18 02:46

This question is not about the C++ language itself(ie not about the Standard) but about how to call a compiler to implement alternative schemes for virtual function.

相关标签:
4条回答
  • 2021-01-18 03:00

    A couple of observations:

    1. Yes, a smaller value could be used to represent the class, but some processors require data to be aligned so that saving in space may be lost by the requirement to align data values to e.g. 4 byte boundaries. Further, the class-id must be in a well defined place for all members of a polymorphic inheritance tree, so it is likely to be ahead of other date, so alignment problems can't be avoided.

    2. The cost of storing the pointer has been moved to the code, where every use of a polymorphic function requires code to translate the class-id to either a vtable pointer, or some equivalent data structure. So it isn't for free. Clearly the cost trade-off depends on the volume of code vs numer of objects.

    3. If objects are allocated from the heap, there is usually space wasted in orer to ensure objects are alogned to the worst boundary, so even if there is a small amount of code, and a large number of polymorphic objects, the memory management overhead migh be significantly bigger than the difference between a pointer and a char.

    4. In order to allow programs to be independently compiled, the number of classes in the whole program, and hence the size of the class-id must be known at compile time, otherwise code can't be compiled to access it. This would be a significant overhead. It is simpler to fix it for the worst case, and simplify compilation and linking.

    Please don't let me stop you trying, but there are quite a lot more issues to resolve using any technique which may use a variable size id to derive the function address.

    I would strongly encourage you to look at Ian Piumarta's Cola also at Wikipedia Cola

    It actually takes a different approach, and uses the pointer in a much more flexible way, to to build inheritance, or prototype-based, or any other mechanism the developer requires.

    0 讨论(0)
  • 2021-01-18 03:03

    No, there is no such switch.

    The LLVM/Clang codebase avoids virtual tables in classes that are allocated by the tens of thousands: this work well in a closed hierachy, because a single enum can enumerate all possible classes and then each class is linked to a value of the enum. The closed is obviously because of the enum.

    Then, virtuality is implemented by a switch on the enum, and appropriate casting before calling the method. Once again, closed. The switch has to be modified for each new class.


    A first alternative: external vpointer.

    If you find yourself in a situation where the vpointer tax is paid way too often, that is most of the objects are of known type. Then you can externalize it.

    class Interface {
    public:
      virtual ~Interface() {}
    
      virtual Interface* clone() const = 0; // might be worth it
    
      virtual void updateCount(int) = 0;
    
    protected:
      Interface(Interface const&) {}
      Interface& operator=(Interface const&) { return *this; }
    };
    
    template <typename T>
    class InterfaceBridge: public Interface {
    public:
      InterfaceBridge(T& t): t(t) {}
    
      virtual InterfaceBridge* clone() const { return new InterfaceBridge(*this); }
    
      virtual void updateCount(int i) { t.updateCount(i); }
    
    private:
      T& t; // value or reference ? Choose...
    };
    
    template <typename T>
    InterfaceBridge<T> interface(T& t) { return InterfaceBridge<T>(t); }
    

    Then, imagining a simple class:

    class Counter {
    public:
      int getCount() const { return c; }
      void updateCount(int i) { c = i; }
    private:
      int c;
    };
    

    You can store the objects in an array:

    static Counter array[5];
    
    assert(sizeof(array) == sizeof(int)*5); // no v-pointer
    

    And still use them with polymorphic functions:

    void five(Interface& i) { i.updateCount(5); }
    
    InterfaceBridge<Counter> ib(array[3]); // create *one* v-pointer
    five(ib);
    
    assert(array[3].getCount() == 5);
    

    The value vs reference is actually a design tension. In general, if you need to clone you need to store by value, and you need to clone when you store by base class (boost::ptr_vector for example). It is possible to actually provide both interfaces (and bridges):

    Interface <--- ClonableInterface
      |                 |
    InterfaceB     ClonableInterfaceB
    

    It's just extra typing.


    Another solution, much more involved.

    A switch is implementable by a jump table. Such a table could perfectly be created at runtime, in a std::vector for example:

    class Base {
    public:
      ~Base() { VTables()[vpointer].dispose(*this); }
    
      void updateCount(int i) {
        VTables()[vpointer].updateCount(*this, i);
      }
    
    protected:
      struct VTable {
        typedef void (*Dispose)(Base&);
        typedef void (*UpdateCount)(Base&, int);
    
        Dispose dispose;
        UpdateCount updateCount;
      };
    
      static void NoDispose(Base&) {}
    
      static unsigned RegisterTable(VTable t) {
        std::vector<VTable>& v = VTables();
        v.push_back(t);
        return v.size() - 1;
      }
    
      explicit Base(unsigned id): vpointer(id) {
        assert(id < VTables.size());
      }
    
    private:
      // Implement in .cpp or pay the cost of weak symbols.
      static std::vector<VTable> VTables() { static std::vector<VTable> VT; return VT; }
    
      unsigned vpointer;
    };
    

    And then, a Derived class:

    class Derived: public Base {
    public:
      Derived(): Base(GetID()) {}
    
    private:
      static void UpdateCount(Base& b, int i) {
        static_cast<Derived&>(b).count = i;
      }
    
      static unsigned GetID() {
        static unsigned ID = RegisterTable(VTable({&NoDispose, &UpdateCount}));
        return ID;
      }
    
      unsigned count;
    };
    

    Well, now you'll realize how great it is that the compiler does it for you, even at the cost of some overhead.

    Oh, and because of alignment, as soon as a Derived class introduces a pointer, there is a risk that 4 bytes of padding are used between Base and the next attribute. You can use them by careful selecting the first few attributes in Derived to avoid padding...

    0 讨论(0)
  • 2021-01-18 03:22

    You're suggestion is interesting, but it won't work if the executable is made of several modules, passing objects among them. Given they are compiled separately (say DLLs), if one module creates an object and passes it to another, and the other invokes a virtual method - how would it know which table the classid refers to? You won't be able to add another moduleid because the two modules might not know about each other when they are compiled. So unless you use pointers, I think it's a dead end...

    0 讨论(0)
  • 2021-01-18 03:23

    The short answer is that no, I don't know of any switch to do this with any common C++ compiler.

    The longer answer is that to do this, you'd just about have to build most of the intelligence into the linker, so it could coordinate distributing the IDs across all the object files getting linked together.

    I'd also point out that it wouldn't generally do a whole lot of good. At least in a typical case, you want each element in a struct/class at a "natural" boundary, meaning its starting address is a multiple of its size. Using your example of a class containing a single int, the compiler would allocate one byte for the vtable index, followed immediately by three byes of padding so the next int would land at an address that was a multiple of four. The end result would be that objects of the class would occupy precisely the same amount of storage as if we used a pointer.

    I'd add that this is not a far-fetched exception either. For years, standard advice to minimize padding inserted into structs/classes has been to put the items expected to be largest at the beginning, and progress toward the smallest. That means in most code, you'd end up with those same three bytes of padding before the first explicitly defined member of the struct.

    To get any good from this, you'd have to be aware of it, and have a struct with (for example) three bytes of data you could move where you wanted. Then you'd move those to be the first items explicitly defined in the struct. Unfortunately, that would also mean that if you turned this switch off so you have a vtable pointer, you'd end up with the compiler inserting padding that might otherwise be unnecessary.

    To summarize: it's not implemented, and if it was wouldn't usually accomplish much.

    0 讨论(0)
提交回复
热议问题