Value::swap does not swap some members
Looks like it fails to swap itemIsUsed_, memberNameIsStatic_, and comments_. If this is intentional, it deserves a comment.
Feel free to add whatever comment you consider appropriate. The main author of swap and the comment-related features does not have time to work on this project right now. (I avoid all that myself.) I'll leave this issue open for now.
Found this comment in old code at sourceforge: /// Swap values. /// \note Currently, comments are intentionally not swapped, for /// both logic and efficiency.
@datadiode, that comment is still here. Maybe that just needs to be expanded.
If it were up to me, I would like the swap to be changed to swap everything, and not leave the comments behind. The comments are part of the object's state.
I mean, if I have a std::vector
On Wed, Jan 7, 2015 at 10:42 AM, Christopher Dunn [email protected] wrote:
@datadiode https://github.com/datadiode, that comment is still here https://github.com/open-source-parsers/jsoncpp/blob/master/include/json/value.h#L241. Maybe that just needs to be expanded.
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/47#issuecomment-69039646 .
Value::operator=() relies on Value::swap() to not swap the comments, because it is designed to leave comments in place.
The proposed fix is to not implement Value::operator=() in terms of Value::swap(), but in terms of Value::swapPayload(). The latter leaves comments (as well as source offsets) in place, while the former is revised to follow standard semantics.
operator= should not preserve old comments in the destination. It is extremely surprising if an algorithm relying on swap produces different results from an algorithm relying on std::swap, which uses operator=.
sounds like a bug factory.
On Sunday, January 18, 2015, datadiode [email protected] wrote:
Value::operator=() relies on Value::swap() to not swap the comments, because it is designed to leave comments in place.
The proposed fix is to not implement Value::operator=() in terms of Value::swap(), but in terms of Value::swapPayload(). The latter leaves comments (as well as source offsets) in place, while the former is revised to follow standard semantics.
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/47#issuecomment-70401362 .
ǝnɥɐuop ʎllıq
@BillyGoto, You make a good point. But @datadiode has provided swapPayload() to match operator= (pull #118) which should at least make a bug less likely.
swapPayload doesn't really answer my point, though.
It's dangerous to have a value type for which an in-place sort and an out-of-place sort produce different results.
On Sun, Jan 18, 2015 at 12:37 PM, Christopher Dunn <[email protected]
wrote:
@BillyGoto https://github.com/BillyGoto, You make a good point. But @datadiode https://github.com/datadiode has provided swapPayload() to match operator= (pull #118 https://github.com/open-source-parsers/jsoncpp/pull/118) which should at least make a bug less likely.
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/47#issuecomment-70417220 .
ǝnɥɐuop ʎllıq
swap() is just a method. I don't think of it as a name with special significance. But we could rename it if you want. What would be a good name for it?
Or it is really just operator=() that bothers you? Personally, I am not a fan of operators in general, so I would have no problem with reverting to its prior behavior. I just want @cout's and @datadiode's suggested functionality to be available somewhere.
ok. The name of the nonmember "swap" function really is a name with special significance in C++, mentioned in the standard under "Swappable".
Most operators it is understandable to hold on some skepticism. But operator= is special in that it is implicitly defined.
If we need unusual optimized copying functions that propagate comments in a way that's counter to the expectations for logically regular value-semantics, then let's give those names other than swap() and operator=, which have expected semantics.
If this is a breaking change, v1.0 might be a good time to make it. Before v1.0, breaking changes are hopefully forgiveable, as it's implicitly "beta" anyway.
I don't well understand the motivaton for leaving comments behind in these optimized variants, so I can't really venture to naming them without looking into it a little more.
On Monday, January 19, 2015, Christopher Dunn <[email protected] javascript:_e(%7B%7D,'cvml','[email protected]');> wrote:
swap() is just a method. I don't think of it as a name with special significance. But we could rename it if you want. What would be a good name for it?
Or it is really just operator=() that bothers you? Personally, I am not a fan of operators in general, so I would have no problem with reverting to its prior behavior. I just want @cout https://github.com/cout's and @datadiode https://github.com/datadiode's suggested functionality to be available somewhere.
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/47#issuecomment-70513493 .
ǝnɥɐuop ʎllıq
Hmm. This seems to recommend a member swap(T& other) when private data are needed, but std::swap(T& a, T&b) does not call the member by default, right? But consistency is probably a good idea. Ok.
If I understand correctly, you're suggesting that our overloaded std::swap() should call our member Value::swap(), and that Value::operator=() should be consistent with both. @datadiode and @cout want comments to be retained. And you're ok with changing the behavior of operator=() as part of v1.x.
Ok. Seem sensible. We'll call it 1.2.0, and everybody should be happy.
Ugh! I see the issue with operator=() now. It performs a shallow "copy". But who owns the "copy"? Since there is no reference-counting, the previous owner disowns the "copy".
That operator is problematic. If we "fix" it with a deep-copy, then we will cause existing code to perform very inefficiently, and accidents would be common.
The solution seems to be a reference-counted value. I guess we can keep the current ValueHolder union, but someday we might switch to a base class with virtual methods.
I'm not 100% sure the deep copy would be so bad if we had a move assignment. The accidents would be less of a problem then, and copying would only happen where it's intentional. The existing shallow copy semantics are only really correct for move operations. The refcounting has its own costs. If pointers to Value objects are passed between threads, then the refcounted object can be more expensive than a deeply copied object. Refcounted std::string are now considered to have been a dead end because of the multithreaded performance problem and a dereferencing performance hit. If deep copy can be avoided by moves, and if deep copy makes Value faster in multithread usage, then maybe refcounting isn't called for yet. Benchmarks are needed. Do we have any?
I agree 100%. And there is another wrinkle: memory-management. Both arrays and maps have their own memory-management, and strings currently at least allow something special. Using standard new for ref-counted objects seems inconsistent.
Here are the choices for operator=(), in my mind:
- Reference-counting.
- Deep-copy.
- Swap. (The current semantics.)
- Disallow. (I.e. hide it.)
At my last start-up, we found that shared_ptr<> -- even between threads -- is ok with jemalloc, but pretty bad with standard linux malloc (ptmalloc2, I think) or tcmalloc.
Could you explain what you mean by "move assignment" for deep-copy? Doesn't the whole sub-tree need to be re-created?
At any rate, deep-copy really is the most natural thing for operator=() in C++. I'm for it. People can always use swap() if that's what they want. And @datadiode's version of swapPayload() is a valuable addition.
There is some misunderstanding here. Value::operator=() does perform a deep copy, not a shallow copy. It uses copy-and-swap idiom, and as usual in that case, takes a by-value rather than a by-reference argument.
If it is acceptable to break v0.x based code, then I'd prefer to align Value::operator=(Value) behavior with regular expectations, and add a method Value::setValue(Value) which only affects the value but leaves comments and offsets in place. Value::operator=(Value) would use swap(), and Value::setValue(Value) would use swapPayload().
@datadiode, Oh, I read this wrong.
Value& Value::operator=(Value other) {
swapPayload(other);
return *this;
}
void Value::swapPayload(Value& other) {
ValueType temp = type_;
type_ = other.type_;
other.type_ = temp;
std::swap(value_, other.value_);
int temp2 = allocated_;
allocated_ = other.allocated_;
other.allocated_ = temp2;
}
There is no ampersand in the operator, and Value::Value(Value const&) is a deep-copy.
Ok. That settles that.
And the copy-ctor copies comments. So why doesn't operator=()? I think we should just change it and bump the minor version.
ok it's all clear now. Yes. Assign should copy exactly what the copy ctor copies. Everything. We all agree.
I was responding to your post, Chris, but didn't look at the code. Oops. Thanks data diode for clearing everything up.
On Tuesday, January 20, 2015, Christopher Dunn [email protected] wrote:
@datadiode https://github.com/datadiode, Oh, I read this wrong.
Value& Value::operator=(Value other) { swapPayload(other); return *this; }
void Value::swapPayload(Value& other) { ValueType temp = type_; type_ = other.type_; other.type_ = temp; std::swap(value_, other.value_); int temp2 = allocated_; allocated_ = other.allocated_; other.allocated_ = temp2; }
There is no ampersand in the operator, and Value::Value(Value const&) is a deep-copy.
Ok. That settles that.
And the copy-ctor copies comments. So why doesn't operator=()? I think we should just change it and bump the minor version.
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/47#issuecomment-70618253 .
itemIsUsed_,memberNameIsStatic_
These are used only when JSON_VALUE_USE_INTERNAL_MAP=1, which is only for @blep's own experiments.