[bitstream] Copy stream buffer or pass pointer?
While working on a bitstream library I proposed a while back (http://article.gmane.org/gmane.comp.lib.boost.devel/242574), I came across a design decision for which I'd like to get some feedback. The proposed bitstream library (https://github.com/dplong/bitstream) mimics the std::stringstream class hierarchy and semantics as close as possible. In general, when the consumer of the std::stringstream/stringbuf classes provides a string for the stream buffer, a copy gets made; likewise, when it retrieves the stream buffer, it gets a copy of the string. For example, the following snippet prints "hello, goodbye". char c[BUFSIZ] = "hello"; istringstream iss(c); strcpy(c, "goodbye"); cout << iss.str() << ", " << c; However, the library I'm working on doesn't copy. Instead, pointers are passed in and out. So, for example, this code prints "goodbye, goodbye". char c[BUFSIZ] = "hello"; ibitstream ibs(c); strcpy(c, "goodbye"); cout << ibs.data() << ", " << c; I don't think either way is more correct than the other. The reason I don't copy is for memory and CPU performance, thinking that the streams a bit-stream library typically processes are larger than the streams that a string-stream library processes and are more time critical. I very well may be mistaken, though. Anyway, what do you people think? Copy or don't copy? Maybe I should do copies just to follow the semantics of std::stringstream. Oh, also, am I being presumptuous by including "[bitstream]" in the Subject header of this post since it's just a _proposed_ library? IOW, should I stop doing that? Paul
Date: Wed, 17 Jul 2013 22:09:08 -0500 From: plong@packetizer.com
For example, the following snippet prints "hello, goodbye".
char c[BUFSIZ] = "hello"; istringstream iss(c); strcpy(c, "goodbye"); cout << iss.str() << ", " << c;
However, the library I'm working on doesn't copy. Instead, pointers are passed in and out. So, for example, this code prints "goodbye, goodbye".
char c[BUFSIZ] = "hello"; ibitstream ibs(c); strcpy(c, "goodbye"); cout << ibs.data() << ", " << c;
I don't think either way is more correct than the other. The reason I don't copy is for memory and CPU performance, thinking that the streams a bit-stream library typically processes are larger than the streams that a string-stream library processes and are more time critical. I very well may be mistaken, though. Anyway, what do you people think? Copy or don't copy? Maybe I should do copies just to follow the semantics of std::stringstream.
I would suggest copying. Here you have two sources; what happens with three? The reference method doesn't scale. By-reference also has lifetime issues. Daryle W.
on Wed Jul 17 2013, Daryle Walker
Date: Wed, 17 Jul 2013 22:09:08 -0500 From: plong@packetizer.com
For example, the following snippet prints "hello, goodbye".
char c[BUFSIZ] = "hello"; istringstream iss(c); strcpy(c, "goodbye"); cout << iss.str() << ", " << c;
However, the library I'm working on doesn't copy. Instead, pointers are passed in and out. So, for example, this code prints "goodbye, goodbye".
char c[BUFSIZ] = "hello"; ibitstream ibs(c); strcpy(c, "goodbye"); cout << ibs.data() << ", " << c;
I don't think either way is more correct than the other. The reason I don't copy is for memory and CPU performance, thinking that the streams a bit-stream library typically processes are larger than the streams that a string-stream library processes and are more time critical. I very well may be mistaken, though. Anyway, what do you people think? Copy or don't copy? Maybe I should do copies just to follow the semantics of std::stringstream.
I would suggest copying.
But when the source is an rvalue, move instead. That handles the efficiency issues. -- Dave Abrahams
On Thu, 18 Jul 2013 00:27:37 -0700, Dave Abrahams wrote:
on Wed Jul 17 2013, Daryle Walker
wrote: Date: Wed, 17 Jul 2013 22:09:08 -0500 From: plong@packetizer.com
For example, the following snippet prints "hello, goodbye".
char c[BUFSIZ] = "hello"; istringstream iss(c); strcpy(c, "goodbye"); cout << iss.str() << ", " << c;
However, the library I'm working on doesn't copy. Instead, pointers are passed in and out. So, for example, this code prints "goodbye, goodbye".
char c[BUFSIZ] = "hello"; ibitstream ibs(c); strcpy(c, "goodbye"); cout << ibs.data() << ", " << c;
Copy or don't copy?
I would suggest copying.
But when the source is an rvalue, move instead. That handles the efficiency issues.
I don't understand? Can you elaborate? Wouldn't a move also require that bytes get copied? Also how does one write code that is overloaded for rvalues vs lvalues? Paul
participants (3)
-
Daryle Walker
-
Dave Abrahams
-
Paul Long