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

Don't use global (external) symbols for symbols that are used in only one translation unit #16092

Merged
merged 1 commit into from Jun 18, 2019

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented May 26, 2019

Don't use global (external) symbols for symbols that are used in only one translation unit.

Before:

$ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
      REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
      N_REFERENCES=$(wc -l <<< "${REFERENCES}")
      if [[ ${N_REFERENCES} > 1 ]]; then
          continue
      fi
      echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
  done
Global symbol g_chainstate is used in only one translation unit: src/validation.cpp
Global symbol g_ui_signals is used in only one translation unit: src/ui_interface.cpp
Global symbol instance_of_cmaincleanup is used in only one translation unit: src/validation.cpp
Global symbol instance_of_cnetcleanup is used in only one translation unit: src/net.cpp
Global symbol instance_of_cnetprocessingcleanup is used in only one translation unit: src/net_processing.cpp
Global symbol pindexBestForkBase is used in only one translation unit: src/validation.cpp
Global symbol pindexBestForkTip is used in only one translation unit: src/validation.cpp
$ 

After:

$ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
      REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
      N_REFERENCES=$(wc -l <<< "${REFERENCES}")
      if [[ ${N_REFERENCES} > 1 ]]; then
          continue
      fi
      echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
  done
$ 

♻️ Think about future generations: save the global namespace from unnecessary pollution! ♻️

@JosuGZ
Copy link
Contributor

JosuGZ commented May 26, 2019

I whould say utACK b547633

@DrahtBot
Copy link
Contributor

DrahtBot commented May 26, 2019

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16194 (refactor: share blockmetadata with BlockManager by jamesob)
  • #15606 ([experimental] UTXO snapshots by jamesob)

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.

@Empact
Copy link
Member

Empact commented May 27, 2019

Concept ACK. Could be better to use an anon namespace where possible for these as that will make the variables static and enforce local-only access to the associated class as well.

@practicalswift
Copy link
Contributor Author

practicalswift commented Jun 3, 2019

@Empact The end-result would be the same as using static, right?

$ cat exporting.cpp
static int _static_int = 1;
namespace {
  int _namespace_int = 1;
  static int _namespace_static_int = 1;
}
int _int = 1;
$ clang++ -c exporting.cpp
$ nm exporting.o
0000000000000000 D _int

I don't have any strong preference -- I just want to minimise the diffs and make them super-trivial to review. Please read #15622 (comment) and other comments in that PR.

@Empact
Copy link
Member

Empact commented Jun 3, 2019

Sure, fine as is.

@practicalswift
Copy link
Contributor Author

Rebased!

@Empact
Copy link
Member

Empact commented Jun 10, 2019

ACK 0959d37

@maflcko
Copy link
Member

maflcko commented Jun 10, 2019

ACK 0959d37

@hebasto
Copy link
Member

hebasto commented Jun 18, 2019

ACK 0959d37
I have not tested the code, but I have reviewed it and it LGTM.
Ref: https://en.cppreference.com/w/cpp/language/storage_duration#Linkage

@promag
Copy link
Member

promag commented Jun 18, 2019

ACK 0959d37.

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Jun 18, 2019
…that are used in only one translation unit

0959d37 Don't use global (external) symbols for symbols that are used in only one translation unit (practicalswift)

Pull request description:

  Don't use global (external) symbols for symbols that are used in only one translation unit.

  Before:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  Global symbol g_chainstate is used in only one translation unit: src/validation.cpp
  Global symbol g_ui_signals is used in only one translation unit: src/ui_interface.cpp
  Global symbol instance_of_cmaincleanup is used in only one translation unit: src/validation.cpp
  Global symbol instance_of_cnetcleanup is used in only one translation unit: src/net.cpp
  Global symbol instance_of_cnetprocessingcleanup is used in only one translation unit: src/net_processing.cpp
  Global symbol pindexBestForkBase is used in only one translation unit: src/validation.cpp
  Global symbol pindexBestForkTip is used in only one translation unit: src/validation.cpp
  $
  ```

  After:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  $
  ```

  ♻️ Think about future generations: save the global namespace from unnecessary pollution!  ♻️

ACKs for commit 0959d3:
  Empact:
    ACK bitcoin@0959d37
  MarcoFalke:
    ACK 0959d37
  hebasto:
    ACK 0959d37
  promag:
    ACK 0959d37.

Tree-SHA512: 722f66bb50450f19b57e8a8fbe949f30cd651eb8564e5787cbb772a539bf3a288c048dc49e655fd73ece6a46f6dafade515ec4004729bf2b3ab83117b7c5d153
@maflcko maflcko merged commit 0959d37 into bitcoin:master Jun 18, 2019
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 19, 2019
…that are used in only one translation unit

0959d37 Don't use global (external) symbols for symbols that are used in only one translation unit (practicalswift)

Pull request description:

  Don't use global (external) symbols for symbols that are used in only one translation unit.

  Before:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  Global symbol g_chainstate is used in only one translation unit: src/validation.cpp
  Global symbol g_ui_signals is used in only one translation unit: src/ui_interface.cpp
  Global symbol instance_of_cmaincleanup is used in only one translation unit: src/validation.cpp
  Global symbol instance_of_cnetcleanup is used in only one translation unit: src/net.cpp
  Global symbol instance_of_cnetprocessingcleanup is used in only one translation unit: src/net_processing.cpp
  Global symbol pindexBestForkBase is used in only one translation unit: src/validation.cpp
  Global symbol pindexBestForkTip is used in only one translation unit: src/validation.cpp
  $
  ```

  After:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  $
  ```

  ♻️ Think about future generations: save the global namespace from unnecessary pollution!  ♻️

ACKs for commit 0959d3:
  Empact:
    ACK bitcoin@0959d37
  MarcoFalke:
    ACK 0959d37
  hebasto:
    ACK 0959d37
  promag:
    ACK 0959d37.

Tree-SHA512: 722f66bb50450f19b57e8a8fbe949f30cd651eb8564e5787cbb772a539bf3a288c048dc49e655fd73ece6a46f6dafade515ec4004729bf2b3ab83117b7c5d153
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 11, 2020
…e used in only one translation unit

Summary:
0959d37e3e Don't use global (external) symbols for symbols that are used in only one translation unit (practicalswift)

Pull request description:

  Don't use global (external) symbols for symbols that are used in only one translation unit.

  Before:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  Global symbol g_chainstate is used in only one translation unit: src/validation.cpp
  Global symbol g_ui_signals is used in only one translation unit: src/ui_interface.cpp
  Global symbol instance_of_cmaincleanup is used in only one translation unit: src/validation.cpp
  Global symbol instance_of_cnetcleanup is used in only one translation unit: src/net.cpp
  Global symbol instance_of_cnetprocessingcleanup is used in only one translation unit: src/net_processing.cpp
  Global symbol pindexBestForkBase is used in only one translation unit: src/validation.cpp
  Global symbol pindexBestForkTip is used in only one translation unit: src/validation.cpp
  $
  ```

  After:

  ```
  $ for SYMBOL in $(nm src/bitcoind | grep -E ' [BD] ' | c++filt | cut -f3- -d' ' | grep -v @ | grep -v : | sort | grep '[a-z]' | sort -u | grep -vE '(^_|typeinfo|vtable)'); do
        REFERENCES=$(git grep -lE "([^a-zA-Z]|^)${SYMBOL}([^a-zA-Z]|\$)" -- "*.cpp" "*.h")
        N_REFERENCES=$(wc -l <<< "${REFERENCES}")
        if [[ ${N_REFERENCES} > 1 ]]; then
            continue
        fi
        echo "Global symbol ${SYMBOL} is used in only one translation unit: ${REFERENCES}"
    done
  $
  ```

  ♻️ Think about future generations: save the global namespace from unnecessary pollution!  ♻️

ACKs for commit 0959d3:
  Empact:
    ACK bitcoin/bitcoin@0959d37
  MarcoFalke:
    ACK 0959d37e3e0f80010a78d175e3846dabf5d35919
  hebasto:
    ACK 0959d37e3e0f80010a78d175e3846dabf5d35919
  promag:
    ACK 0959d37.

Tree-SHA512: 722f66bb50450f19b57e8a8fbe949f30cd651eb8564e5787cbb772a539bf3a288c048dc49e655fd73ece6a46f6dafade515ec4004729bf2b3ab83117b7c5d153

Backport of Core [[bitcoin/bitcoin#16092 | PR16092]]

I followed the test plan in the original PR, but do not see those global symbols on master to begin with.
Regardless, this brings our codebase more inline with Core for future backporting's sake.

Test Plan: `ninja check check-functional`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6514
@practicalswift practicalswift deleted the reduce-global-namespacing branch April 10, 2021 19:38
kwvg added a commit to kwvg/dash that referenced this pull request Oct 9, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 10, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 16, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 16, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 16, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 21, 2021
…that are used in only one translation unit
kwvg added a commit to kwvg/dash that referenced this pull request Oct 22, 2021
…that are used in only one translation unit
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Nov 18, 2021
@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.

None yet

8 participants