Thread: How to exchange a std::vector<> efficiently between two classes

  1. #1
    Guest
    Guest

    How to exchange a std::vector<> efficiently between two classes

    Let's assume the vector memVec is a private member of class A, i.e. an lvalue. What would be a good way to pass it to class B for further processing?

    Note that memVec does not need to retain its data beyond the exchange.

    Could I create an empty vector inside A's return function and swap it in-place to return a movable rvalue?
    Code:
    std::vector<char> A::next()
    {
        std::vector<char> tempVec;
        memVec.swap(tempVec);
        return tempVec;
    }
    Note that memVec will be filled with a virtually identical amount of data (give or take 1KB) many times over. So I intend to call
    Code:
    memVec.reserve(moreThanEnoughRoom);
    on it after every exchange/swap, which I imagine would still be better than copying the vector to B and calling memVec.clear(). What are your thoughts?
    Last edited by Guest; 02-01-2015 at 10:59 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Why can't you just use a reference instead?
    Can you show us more code?


    There is std::move (that internally does this swap with the vector's data), but it is not an exact fit for your situation in my opinion.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    From what you've posted, you sound as if you are trying to overuse decoupling and verb an object.

    If you can't post more code, you need to at least tell us more about your purpose.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by phantomotap View Post
    overuse decoupling
    No such thing ^_^
    His problem here probably is from a design flaw, but that's certainly not it.


    Would this not work?
    Code:
    cost std::vector<char>& A::next()
    {
        return memVec;
    }
    After all, you never said B would need the data to be mutable.

  5. #5
    Guest
    Guest
    I'm indeed trying to decouple the classes beyond what I might have done in the past. Class A reads large text files and slices them into roughly evenly sized chunks. Class B is will process the these chunks, extracting specific data. B is supposed to have multiple instances of itself running in threads. That's why I want to hand off the data without passing around pointers.

    In an older version of this I had a single class manage file reading, thread dispatch and data extraction, but I thought I'd try to encapsulate these tasks more strongly this time.

    Maybe a middle way would be to have a C-array of std::vector inside A and to pass pointers to these to requesting threads through a queue. But it would certainly feel to me like A is managing something that's not its job. I'll be the first to admit that I don't have a sound concept though; I'm just trying to see if I can get rid of the multipurpose class while retaining high efficiency.

    @Yarin: The data does not need to be mutable, correct.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you are multi-threading the data processing, you are basically saying "It takes longer for me to process this data than read it from disk." If that's the case, I doubt the overhead of copying arrays of char around is going to matter whatsoever to what you are doing. Write dumb code first then optimize it if you can prove it is the copies that are killing you. But I doubt it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Guest
    Guest
    That's a fair point. I'll try this design with copies and see if it really matters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficiently scanning through a vector.
    By leeor_net in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2009, 02:13 AM
  2. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  3. vector of classes
    By te5la in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2008, 01:44 PM
  4. vector of derived classes
    By p3p in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2005, 07:28 AM
  5. Destroying a vector of classes
    By TheSquid in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2005, 08:26 PM

Tags for this Thread