lundi 20 juin 2016

Optimal way of sending a list of information

If we assume that this is a program using a UDP type of communication (obviously TCP would be better for most programs sending a list), the send and receive functions are defined externally, and the purpose is to send and receive a list of objects with the same base object (inheritance) from this code:

#include <iostream>
#include <vector>
#include <memory>
#include <utility>

class Base
{
public:
    Base() {

    }
    ~Base() {

    }
};

class Derived_1: public Base
{
public:
    Derived_1() {

    }
    ~Derived_1() {

    }
};

void send_ptr_vect(std::vector<std::shared_ptr<Base>> &ptr_vect_arg);
void receive_ptr_vect(std::vector<std::shared_ptr<Base>> &ptr_vect_arg);

void send_obj_vect(std::vector<Base> &obj_vect_arg);
void receive_obj_vect(std::vector<Base> &obj_vect_arg);

int main() {
    //Pointer solution
    std::vector<std::shared_ptr<Base>> ptr_vect;
    //Object solution
    std::vector<Base> obj_vect;

    //Filling the pointer vector
    ptr_vect.push_back(std::shared_ptr<Base>(std::dynamic_pointer_cast<Base>(std::shared_ptr<Derived_1>(new Derived_1))));
    //Constant speed operation becuase of it being a pointer

    //Filling the object vector
    obj_vect.push_back(static_cast<Base>(Derived_1()));
    //Expensive operation depending on sizeof(object)

    send_ptr_vect(ptr_vect);
    receive_ptr_vect(ptr_vect);

    send_obj_vect(obj_vect);
    receive_obj_vect(obj_vect);

    return 0;
}

//Send every object by sending derefrenced pointers one at the time.
void send_ptr_vect(std::vector<std::shared_ptr<Base>> &ptr_vect_arg)
{
    for (size_t i = 0; i < ptr_vect_arg.size(); i++)
    {
        send(ip, port, *(ptr_vect_arg[i]));
    }
}

//Receive data by adding the data one at a time, this requires the data to be sent one element at the time.
void receive_ptr_vect(std::vector<std::shared_ptr<Base>> &ptr_vect_arg)
{
    for (size_t i = 0; i < ptr_vect_arg.size(); i++)
    {
        ptr_vect_arg.push_back(receive());
    }
}

//Send the whole vector.
void send_obj_vect(std::vector<Base> &obj_vect_arg)
{
    send(ip, port, obj_vect_arg);
}

//Receive the whole vector.
void receive_obj_vect(std::vector<Base> &obj_vect_arg)
{
    std::vector<Base> &temp_ptr = receive(); //Could return a pointer (and probably would) to the data, but whatever.

    for (size_t i = 0; i < temp_ptr.size(); i++)
    {
        obj_vect_arg.push_back(temp_ptr[i]);
    }
}

So the questions I have are:

1. Would sending the vector of objects (being that it is dynamically sized), only send a list of pointers, just wrapped?

2. I assume that it is a performance loss to send multiple packets of data instead of one big packet?

3. I also assume that it can be dramatically more expensive to typecast a big object, instead of a pointer to the object? Being that the pointer will be the same size, no matter the object.

So to sum up; What is a solid way to solve these problems?

Aucun commentaire:

Enregistrer un commentaire