Renamed namespaces satcom::lib and satcom::pkf to senf
[senf.git] / Packets / FUTURE
1 Optimize the PacketRegistry using a vtable like approach. This vtable
2 approach should be implemented as a generic vtable library:
3
4 Every class which shall be extensible needs to inheriv from the vtable
5 base class. This base class adds an integer member to the class. This
6 baseclass uses the CRTP pattern to initialize this integer to a type
7 specific value.
8
9 To get theese values, we use a typeid registry. The typeid-registry is
10 a tempalte struct with the class as template arg and a single static
11 member fn. This fn has a member variable of type 'typeid sequence
12 value'. The constructor of this type will automatically allocate the
13 next free typeid integer. This can then efficiently be returned by the
14 static retrieval function:
15
16     template <class T>
17     struct senf::vtable::impl::TypeRegistry
18     {
19         static unsigned id() {
20             static senf::vtable::impl::AutoTypeId typeid;
21             return typeid.id();
22         }
23     };
24
25     struct senf::vtable::impl::AutoTypeId
26     {
27         AutoTypeId() : id(nextAutoTypeId()) {}
28         unsigned id;
29     };
30
31     unsigned nextAutoTypeId()
32     {
33         static unsigned id(0);
34         return id++;
35     }
36
37 This setup will assign id's uniquely. The Id's will be ordered by the
38 first TypeRegistry::id() call. To get the Type id of a type, just call
39 senf::vtable::impl::TypeRegistry<SomeType>::id().
40
41 The above is bogus ... we don't register the extensible types, we
42 register the extensions which are arbitrary types.
43
44 The typeid assigned to the extension type is used as an index into a
45 vtable. This vtable is in the simplest case, just an std::vector of
46 void*. To assign an extension to a specific type, we just add some
47 instance of the registered extension type to the vtable of the
48 to-be-extended type using the slot given by the typeid of the
49 extension type. To get the extension, we just look up the id of the
50 extension type and cast the value from the vtable vector to the
51 appropriate type. All this is O(1) and does not depend on the number
52 of extensions or on the number of extended classes.
53
54 The extension vtables are created inside a static method of a
55 templated class (template arg is the extensible class). Additionally,
56 the extensible class must CRTP-wise inherit the vtable baseclass,
57 which will add a vtable pointer to the extensible class and initialize
58 it automatically to point to the correct vtable.
59
60 To make this even more efficient, all vtables should be kept in a
61 list. This allows to keep all vtables to the same size. Then no range
62 checking must be done on accessing the vtable, since all vtables are
63 all as long as the largest extension id. This of course makes the
64 Operation of registering a new extension type O(n), but that should
65 not be a problem since extensions are registered once during program
66 startup. As a reward, the lookup performance is increased: it is only
67 a memory access to find the extension-type id(the address is fixed at
68 compile time) followed by an indexed access to the vtable, where the
69 vtable address is fetched using another memory access. This differs
70 from a compiler-generated vtable access only by a single memory
71 access.