C++ Logo

std-proposals

Advanced search

[std-proposals] Optimise stringstream to plunder a string

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 15 Dec 2022 12:02:22 +0000
Let's say we have a massive string in memory that's 1.7 GB in size,
which we made something like:

    string massive;

    for ( size_t i = 0u; i < 1825361100u; ++i ) massive += "a";

And now let's say we want to use the "istringstream" interface to
manipulate this string so that we can do:

    string line;
    while ( std::getline(the_sstream_object, line) )
    {
        // processing goes here
    }

Well we can simply construct the istringstream object as follows:

    istringstream the_sstream_object(massive);

but of course we're copying 1.7 GB if we do this.

I think istringstream should have an optimised method to assimilate an
Rvalue string:

class istringstream {
public:
    void plunder(string &&arg)
    {
        this->rdbuf() = arg;
    }
};

The above implementation of 'plunder' would require that the class
"stringbuf" is given a new move-assignment operator that accepts a
string parameter, however this particular implementation is just one
possibility. Whatever way it's implemented, whether it involves also
editing the "stringbuf" class, I think an istringstream should be able
to take over a string without copying it.

Received on 2022-12-15 12:02:34