Recommended Posts

I really need to add some copy-on-write functionality to this class I have, and I find myself needing to do this every once in a while. So it occurred to me to make some generic COW stuff that any class can use...
Anyway I was wondering if this seemed like a good approach before modifying a very large project with it. If not, what is a better approach, and why? On a side note, doing a search on "COW" in gamedev.net turns up many many posts on actual cows, like the animal.
The COW base class:

Share this post

Link to post

Share on other sites

@visitor - Yes that would work... I just figured it'd be easier to add a call to COW() to the beginning of each function that needed the functionality, rather than going through and hand testing uniqueness etc. Of course then you could just make a function "COW" in each class that needs it does the whole "if (!Data.unique)" thing, but why do that when it can just be given to you ready to go? Of course than I have to wrap everything in an else statement if it IS unique. Consider the more likely case where there is

void SetW(int W) {COW();Data->Width = W;}

Here "H" retains the previous value. What about when we have a BUNCH of data members?

On a side note... I could of sworn I didn't put my "test case" in the code I put in here... and it turns out I was right. I didn't put my test case in here. You just so happened to use "10, 20", and then "5, 6" respectively, as did I. That's kinda freaky.

Cheers-Scott

0

Share this post

Link to post

Share on other sites

@chairthrower - Yeah I thought about this. Unfortunately this means I would have to edit code in hundreds of places, or at least anywhere I declare an object of the "Image" type (in this case). I was looking for a way that I could just edit the Image class and not have to search through 2000+ files if I didn't need to. Do you think the cow-pointer way is beneficial enough to be worth doing that?

Also, and more importantly, if I have a function, say Image::CopyFrom and under most circumstances it makes a complete copy, but under some circumstances under the right conditions it would be better to just reference the "From", until written to, I'm back to putting the smart pointer inside the class and intruding. Then I'm back to making multiple calls per non-const function, and then I'm back to how do I make this easier? Which brings me to my original post. Hmmmm...

Cheers-Scott

[Edited by - popsoftheyear on October 22, 2008 6:33:09 PM]

0

Share this post

Link to post

Share on other sites

I just figured it'd be easier to add a call to COW() to the beginning of each function that needed the functionality, rather than going through and hand testing uniqueness etc. Of course then you could just make a function "COW" in each class that needs it does the whole "if (!Data.unique)" thing, but why do that when it can just be given to you ready to go? Of course than I have to wrap everything in an else statement if it IS unique. Consider the more likely case where there is

In the simplest case you could probably provide a free templated function to do the COW. I think the test for uniqueness is quite important though, as otherwise you'd reallocate each object every time a mutator is accessed, even though no data is shared.

0

Share this post

Link to post

Share on other sites

I was looking for a way that I could just edit the Image class and not have to search through 2000+ files if I didn't need to. Do you think the cow-pointer way is beneficial enough to be worth doing that?

Theoretically it should only require a simple refactor, the changing or retyping of Image type and then use of operator -> as the accessor rather than dot notation for member method select.

Then again if its really spread across 2000 lines of code, then these sorts of changes (small but everywhere) can make a mess in source control when trying to diff revisions for changes if the global change occurs somewhere between revisions.

Additionally as a one off use-case (as anything to do with COW is likely to be) maybe its better to adapt the behaviour so that it occurs behind the interface (interface rather than generic good programming practice).

I am trying to remember why i didnt continue using it - it's possible that I was trying to work with code (legacy/3rd party api's)that didnt enforce const correctness. its a nice trick of template ahckery - to extract performance if the const stuff is correctly propagated through the code,

Quote:

Also, and more importantly, if I have a function, say Image::CopyFrom and under most circumstances it makes a complete copy, but under some circumstances under the right conditions it would be better to just reference the "From", until written to, I'm back to putting the smart pointer inside the class and intruding. Then I'm back to making multiple calls per non-const function, and then I'm back to how do I make this easier? Which brings me to my original post. Hmmmm...

I am not quite sure I understand this - copy or assignment operations should just transmit the underlying object around unaffected. for operations that change the data a test is done to determine if the object is unique in which case it isnt copied (I am slowly remembering how cow works - thanks Visitor).

pause, mnnn,

I think I see what you are getting at, if performance is overiding factor and operations such as resizing /cropping /selecting a subarea are all common, then this could potentially done just by specifying the additional data needed such as offsets and strides which would avoid copying the underlying pixel buffer. clearly a basic non-intrusive cow cannot do this.

0

Share this post

Link to post

Share on other sites

@visitor - Yeah, my version tests for uniqueness (see source in the first post). My only point was that to do the test explicitly for EVERY scenerio that might need it, as opposed to calling some COW() function that does it for you would be much better. By the time I get there, I'm half-way to writing my own.

@chairthrower - And yeah the basic non-intrusive version just won't cut it for me. I did consider using a smart pointer with copy-on-write semantics to control the "ImageData" though... but at that point it seemed just a little bit simpler to do it like this.

Anyway thanks for the input. I'm certain I feel more confident about it now.

Cheers-Scott

0

Share this post

Link to post

Share on other sites

This topic is 3339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.