Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved efficiency in COutPoint constructors #10292

Merged
merged 1 commit into from May 1, 2017
Merged

Conversation

mm-s
Copy link
Contributor

@mm-s mm-s commented Apr 28, 2017

memset(0) executed twice, first in uint256_t default constructor and then in SetNull.

Remake of #10277

COutPoint() { SetNull(); }
COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
COutPoint(): n( (uint32_t) -1 ) { }
COutPoint(const uint256& hash, uint32_t n): hash(hash), n(n) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave the names as hashIn and nIn so we don't get hash(hash), n(n)?

Copy link
Contributor

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK with @kallewoof 's suggestion

@@ -22,8 +22,8 @@ class COutPoint
uint256 hash;
uint32_t n;

COutPoint() { SetNull(); }
COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
COutPoint(): n( (uint32_t) -1 ) { }
Copy link
Contributor

@dcousens dcousens Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing, n((uint32_t) -1) is fine

@mm-s
Copy link
Contributor Author

mm-s commented Apr 28, 2017

Fixed both suggestions. @dcousens & @kallewoof,
although I'd like to state my opinion on these reviews:
1.- "In" suffix does not add any value and makes the sentence more verbose and, in my opinion, less readable.
2.- Consistency rules often carry problems related to lack of diversity.

Thanks

Copy link
Contributor

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 4fbae77

@kallewoof
Copy link
Member

I would argue that foo(foo) is less ambiguous despite semantic equivalence. It's also the standard that's been adhered to in the code thus far so it'd require some amount of merit to break.

@mm-s
Copy link
Contributor Author

mm-s commented Apr 28, 2017

it is just that c(p):p(p) is a better abstraction than c(pIn):p(pIn) since In does not add any meaning to the code, apart from remarking that it is an input parameter which is obvious.

@kallewoof
Copy link
Member

That's all fine and dandy until you start doing things like

class foo {
    int x;
    foo(int x) { x = x * 48; } 
};

which, crazy as it may seem, is not very far away from your suggestion.

@JeremyRubin
Copy link
Contributor

Weak Concept Ack.

Most likely this gets inlined and optimized out so this shouldn't impact the generated code?

If that isn't the case, I have a slight preference to make this happen by exposing an uninitialized constructor for uin256_t and then calling SetNull.

@mm-s
Copy link
Contributor Author

mm-s commented Apr 30, 2017

@JeremyRubin Not easy decision: The default constructor is reseting to 0, adding a constructor that leaves the object uninitalized looks like having an ugly prototype; on the other hand changing the behavior by leaving the object uninitalized in the default constructor and adding another constructor accepting a uint8[] looks like it is the right design, but at this point it may confuse to whom is already assuming that the default constructor is reseting the memory, apart from checking every single call done in the codebase.

@JeremyRubin
Copy link
Contributor

I'm not suggesting that it has to/should be the default constructor.

Also the main part is that this likely does not impact the actual code generated; you should try outputting optimized assembly for

#include <memory>
int main() {
    char x[32];
    memset((void*)*x, 0, 32);
}

and

#include <memory>
int main() {
    char x[32];
    memset((void*)*x, 0, 32);
    memset((void*)*x, 0, 32);
}

I didn't see a difference even at O1.

@sipa
Copy link
Member

sipa commented May 1, 2017

@JeremyRubin Also when the code is split over different modules?

@mm-s
Copy link
Contributor Author

mm-s commented May 1, 2017

@JeremyRubin The compiler can resolve programmer's errors automatically, but it is preferably to have the high level code well designed before the optimizer checks ; with so many compiler vendors , so many flags and so many archs out there you can never say for sure the compiler will end up optimizing.
Also i commented on the possibility of not using the default constructor, and seemed to me ugly, which prototype would you use for a constructor that does not initialize?

@JeremyRubin
Copy link
Contributor

JeremyRubin commented May 1, 2017

@sipa I think the memset occurs in the header so it's my understanding it would be inlined.

edit: notice that memset is defined in a different header in my example.

@mm-s while this is true, your PR is not fixing an error it is an optimization, which introduces a dependency on the 0 initialization of uint256 that might introduce bugs later. Perhaps one easy way is:

class X {
    public:
        X(bool initialized=true) {
        }
};

@sipa
Copy link
Member

sipa commented May 1, 2017

utACK 4fbae77

I think this change is perfectly reasonable. There is a slight duplication between the SetNull function and the constructor, but it comes at almost no extra complexity. Please let's not introduce explicit non-initializing constructors.

@sipa sipa merged commit 4fbae77 into bitcoin:master May 1, 2017
sipa added a commit that referenced this pull request May 1, 2017
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 12, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 14, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 15, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 19, 2019
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
barrystyle pushed a commit to PACGlobalOfficial/PAC that referenced this pull request Jan 22, 2020
4fbae77 Improved efficiency in COutPoint constructors (Marcos Mayorga)

Tree-SHA512: 1e402d5021a47724b6159af90955f1a5932c383f48e3e704f1c9a52daa18d2dce5d8e1fcd02fae6977eab04ab83fa22872110b821d4c6593d940d9642abc9bcd
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants