Skip to content

Remove global symbols: Avoid using the global namespace if possible#15622

Merged
maflcko merged 1 commit intobitcoin:masterfrom
practicalswift:globals-1
May 25, 2019
Merged

Remove global symbols: Avoid using the global namespace if possible#15622
maflcko merged 1 commit intobitcoin:masterfrom
practicalswift:globals-1

Conversation

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Mar 19, 2019

Remove global symbols: Avoid using the global namespace if possible.

Partially resolves #15612 ("Reduce the number of global symbols used").

Change in global symbols as reported by nm bitcoind before vs after:

$ diff -u <(nm src/bitcoind-before | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
          <(nm src/bitcoind-after  | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
          | grep -E '^[+-][^+-]'
-boundSockets
-cs_warnings
-eventHTTP
-fFeeEstimatesInitialized
-fLargeWorkForkFound
-fLargeWorkInvalidChainFound
-pathHandlers
-strMiscWarning[abi:cxx11]
-threadHTTP

@promag
Copy link
Contributor

promag commented Mar 20, 2019

Concept ACK, change looks good.

@maflcko
Copy link
Member

maflcko commented Mar 20, 2019

utACK b112097f8e5f96e96db47e2eef9870d6f0431677. Also checked that the binary is 9kB smaller.

@Empact
Copy link
Contributor

Empact commented Mar 21, 2019

utACK b112097

@practicalswift
Copy link
Contributor Author

Now removing redundant static:s in unnamed namespaces as requested by @promag and @Empact.

Please re-review :-)

@promag
Copy link
Contributor

promag commented Mar 22, 2019

utACK 9638da7.

@lucayepa
Copy link
Contributor

I think we can add some language on this policy in doc/developer-notes.md. It will help new developers. I agree on a zero-globals policy, enforced with singletons, or with a "state" class that is a container of all the globals, or with a dedicated namespace. This way, every time a new global has to be added (rarely), we can see a clear change in a single place. In my mind this policy should not be enforced from now, but should be accepted, then written in doc/developer-notes.md or elsewhere, and enforced after a couple of years, when all the open PRs are already compliant.

I've just described this method of long grace periods in my comment on #15465.

@practicalswift
Copy link
Contributor Author

@lucayepa I would like to restrict the scope of this PR to these specific code changes which have already been (ut)ACK:ed.

@Empact
Copy link
Contributor

Empact commented Apr 3, 2019

utACK 9638da7

@sipa
Copy link
Member

sipa commented Apr 16, 2019

It's a bit confusing to call this"Remove globals"; a global inside a namespace is still a global.

@practicalswift practicalswift changed the title Remove globals: Avoid using the global namespace if possible Remove global symbols: Avoid using the global namespace if possible Apr 16, 2019
@practicalswift
Copy link
Contributor Author

practicalswift commented Apr 16, 2019

@sipa I've now clarified that this refers to global (external) symbols :-)

@practicalswift
Copy link
Contributor Author

utACK:s from @Empact and @promag, one stale utACK from @MarcoFalke and zero NACK:s

@MarcoFalke, are we ready to move forward with this one to get a slightly less polluted global namespace? :-)

@ajtowns
Copy link
Contributor

ajtowns commented May 22, 2019

I would have thought it would be better to write these as static ... than namespace { \n ... \n } -- you can grep for the former relatively easily, but not really the latter, and it seems more consistent with the current style... It gives a +9-9 diff which seems better than +17-9...

Either way, I don't see the point of dropping static just because it's redundant; that seems like needless churn. After stripping the binary, i don't see any size difference in bitcoind amongst these variants on Linux with clang fwiw.

@practicalswift
Copy link
Contributor Author

@ajtowns I've now reverted to the original version of the PR which is in accordance with your suggestions. Please re-review :-)

@promag @MarcoFalke @Empact Would you mind re-reviewing? :-)

@DrahtBot
Copy link
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16084 (scripted-diff: Complete the move from CCriticalSection to identical RecursiveMutex (both are AnnotatedMixinstd::recursive_mutex) by practicalswift)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko
Copy link
Member

maflcko commented May 25, 2019

Please squash the second commit, no need to have a separate commit for a single line stylistic fixup

Rename CCriticalSection to RecursiveMutex (both are AnnotatedMixin<std::recursive_mutex>)

```
$ git grep -E '(typedef|using).*(CCriticalSection|RecursiveMutex)'
src/sync.h:using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>;
src/sync.h:typedef AnnotatedMixin<std::recursive_mutex> CCriticalSection;
```
@practicalswift
Copy link
Contributor Author

@MarcoFalke Done! Please re-review :-)

@maflcko maflcko merged commit fb43415 into bitcoin:master May 25, 2019
maflcko pushed a commit that referenced this pull request May 25, 2019
… if possible

fb43415 Remove global symbols: Avoid using the global namespace if possible (practicalswift)

Pull request description:

  Remove global symbols: Avoid using the global namespace if possible.

  Partially resolves #15612 ("Reduce the number of global symbols used").

  Change in global symbols as reported by `nm bitcoind` before vs after:

  ```
  $ diff -u <(nm src/bitcoind-before | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            <(nm src/bitcoind-after  | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            | grep -E '^[+-][^+-]'
  -boundSockets
  -cs_warnings
  -eventHTTP
  -fFeeEstimatesInitialized
  -fLargeWorkForkFound
  -fLargeWorkInvalidChainFound
  -pathHandlers
  -strMiscWarning[abi:cxx11]
  -threadHTTP
  ```

ACKs for commit fb4341:

Tree-SHA512: d2f78f6188a992b0e0de8d107e2c494cfa0faa2de4fda634a1d3606d6515633bec86289cf2a2e78ffe467b17b795e2243cc459fb44e0dfe2fc69899506ff61c9
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 11, 2020
… if possible

Summary:
fb434159d1 Remove global symbols: Avoid using the global namespace if possible (practicalswift)

Pull request description:

  Remove global symbols: Avoid using the global namespace if possible.

  Partially resolves #15612 ("Reduce the number of global symbols used").

  Change in global symbols as reported by `nm bitcoind` before vs after:

  ```
  $ diff -u <(nm src/bitcoind-before | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            <(nm src/bitcoind-after  | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            | grep -E '^[+-][^+-]'
  -boundSockets
  -cs_warnings
  -eventHTTP
  -fFeeEstimatesInitialized
  -fLargeWorkForkFound
  -fLargeWorkInvalidChainFound
  -pathHandlers
  -strMiscWarning[abi:cxx11]
  -threadHTTP
  ```

ACKs for commit fb4341:

Tree-SHA512: d2f78f6188a992b0e0de8d107e2c494cfa0faa2de4fda634a1d3606d6515633bec86289cf2a2e78ffe467b17b795e2243cc459fb44e0dfe2fc69899506ff61c9

Backport of Core [[bitcoin/bitcoin#15622 | PR15622]]

Test Plan:
While the instructions in the summary do not appear to work, manually inspecting like:
```
diff -u <(nm src/bitcoind-before) <(nm src/bitcoind-after) | less
```
And searching for the above instances indicates they were removed as expected.

`ninja check check-functional`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6516
@practicalswift practicalswift deleted the globals-1 branch April 10, 2021 19:38
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Sep 11, 2021
…mespace if possible

fb43415 Remove global symbols: Avoid using the global namespace if possible (practicalswift)

Pull request description:

  Remove global symbols: Avoid using the global namespace if possible.

  Partially resolves bitcoin#15612 ("Reduce the number of global symbols used").

  Change in global symbols as reported by `nm bitcoind` before vs after:

  ```
  $ diff -u <(nm src/bitcoind-before | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            <(nm src/bitcoind-after  | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            | grep -E '^[+-][^+-]'
  -boundSockets
  -cs_warnings
  -eventHTTP
  -fFeeEstimatesInitialized
  -fLargeWorkForkFound
  -fLargeWorkInvalidChainFound
  -pathHandlers
  -strMiscWarning[abi:cxx11]
  -threadHTTP
  ```

ACKs for commit fb4341:

Tree-SHA512: d2f78f6188a992b0e0de8d107e2c494cfa0faa2de4fda634a1d3606d6515633bec86289cf2a2e78ffe467b17b795e2243cc459fb44e0dfe2fc69899506ff61c9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Sep 12, 2021
…mespace if possible

fb43415 Remove global symbols: Avoid using the global namespace if possible (practicalswift)

Pull request description:

  Remove global symbols: Avoid using the global namespace if possible.

  Partially resolves bitcoin#15612 ("Reduce the number of global symbols used").

  Change in global symbols as reported by `nm bitcoind` before vs after:

  ```
  $ diff -u <(nm src/bitcoind-before | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            <(nm src/bitcoind-after  | c++filt | grep -E '^[0-9a-f]+ [A-Z] ' | cut -f3- -d' ' | sort -u) \
            | grep -E '^[+-][^+-]'
  -boundSockets
  -cs_warnings
  -eventHTTP
  -fFeeEstimatesInitialized
  -fLargeWorkForkFound
  -fLargeWorkInvalidChainFound
  -pathHandlers
  -strMiscWarning[abi:cxx11]
  -threadHTTP
  ```

ACKs for commit fb4341:

Tree-SHA512: d2f78f6188a992b0e0de8d107e2c494cfa0faa2de4fda634a1d3606d6515633bec86289cf2a2e78ffe467b17b795e2243cc459fb44e0dfe2fc69899506ff61c9
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
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.

Reduce the number of global symbols used

9 participants