Closed rressi closed 7 years ago
Automated message from Dropbox CLA bot
@rressi, thanks for the pull request! It looks like you haven't yet signed the Dropbox CLA. Please sign it here.
Automated message from Dropbox CLA bot
@rressi, thanks for signing the CLA!
Looks great - thanks!
I think this operator could be improved with the following implementation:
bool Json::operator== (const Json &other) const {
return m_ptr == other.m_ptr
&& m_ptr->type() == other.m_ptr->type()
&& m_ptr->equals(other.m_ptr.get());
}
In this way the compiler would do the best in any case.
By the way, pointer comparison also speed up when nodes are both NULL.
m_ptr is always non-null, otherwise the dereferences here would crash.
I like your version for clarity, though I think it's equivalent in performance. The compiler can't reorder short-circuit operators any more than it can reorder ifs. And JsonValue is a polymorphic object, so the virtual calls can't be inlined in order to let the compiler start eliminating cases.
Story
When comparing two nodes we can speed up use cases where we reuse node instances partially or totally:
Two nodes sharing a portion of nodes:
It may be useful to apply the same concept also for
operator<
, in such case would always returnfalse
:These little changes would open memory + CPU optimisations of this sort: