Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualfastcall22

Posted 31 October 2013 - 10:35 AM

Well, for one, you could move the data closer together by using a vector instead of a linked list. You could move the data even closer by using the vector to store the packets themselves, though managing would be a bit tricky:
typedef unsigned char byte;
vector<byte> packet_store;

template<class T>
void write_memory( byte* at, const T& val ) {
    // assumes Packet<-T and T is POD
    *reinterpret_cast<T*>(at) = val;
}

template<class T>
void store_packet( vector<byte>& packet_store, const T& packet ) {
    size_t total_size = sizeof(size_t) + sizeof(T);
    
    size_t idx = packet_store.size();
    packet_store.resize(packet_store.size() + total_size); // assumes Packet<-T and T is POD
    
    char* at = &packet_store[0] + idx;
    write_memory(at,total_size);
    write_memory(at+sizeof(size_t),packet);
}

template<class F>
void foreach_packet( vector<byte>& packet_store, F f ) {
    byte* at = &packet_store[0];
    byte* end = begin + packet_store.size();
    for ( size_t total_size = 0; at < end; at += total_size ) {
        sz = *reinterpret_cast<size_t*>(at);
        Packet* p = reinterpret_cast<Packet*>(at+sizeof(size_t));
        
        f(p);
    }
}
Beware, sample code is not alignment-aware.

.. and like some pointers.

Inb4 XKCD or 0x1234ABCD huehuehue.

#3fastcall22

Posted 31 October 2013 - 10:35 AM

Well, for one, you could move the data closer together by using a vector instead of a linked list. You could move the data even closer by using the vector to store the packets themselves, though managing would be a bit tricky:
[code=auto:0]
typedef unsigned char byte;
vector<byte> packet_store;

template<class T>
void write_memory( byte* at, const T& val ) {
// assumes Packet<-T and T is POD
*reinterpret_cast<T*>(at) = val;
}

template<class T>
void store_packet( vector<byte>& packet_store, const T& packet ) {
size_t total_size = sizeof(size_t) + sizeof(T);

size_t idx = packet_store.size();
packet_store.resize(packet_store.size() + total_size); // assumes Packet<-T and T is POD

char* at = &packet_store[0] + idx;
write_memory(at,total_size);
write_memory(at+sizeof(size_t),packet);
}

template<class F>
void foreach_packet( vector<byte>& packet_store, F f ) {
byte* at = &packet_store[0];
byte* end = begin + packet_store.size();
for ( size_t total_size = 0; at < end; at += total_size ) {
sz = *reinterpret_cast<size_t*>(at);
Packet* p = reinterpret_cast<Packet*>(at+sizeof(size_t));

f(p);
}
}
[code=auto:0]
Beware, sample code is not alignment-aware.

.. and like some pointers.

Inb4 XKCD or 0x1234ABCD huehuehue.

#2fastcall22

Posted 31 October 2013 - 10:23 AM

Well, for one, you could move the data closer together by using a vector instead of a linked list. You could move the data even closer by using the vector to store the packets themselves, though managing would be a bit tricky:


.. and like some pointers.

Inb4 XKCD or 0x1234ABCD huehuehue.

#1fastcall22

Posted 31 October 2013 - 10:14 AM

Well, for one, you could move the data closer together by using a vector instead of a linked list.

.. and like some pointers.
Inb4 XKCD or 0x1234ABCD huehuehue.

PARTNERS