Skip to content

initialize call to ARRAY_SIZE macro outside the control part of loop #542

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

Conversation

ParthGala2k
Copy link

@ParthGala2k ParthGala2k commented Jan 31, 2020

git.c: initialize calls to ARRAY_SIZE macro outside control part of loop

Calls to ARRAY_SIZE macro are currently in test part of the loop
and are thus redundant. Initialization outside will fetch size of
commands array faster.
Any advice for improving in any area on my part would be great.

Signed-off-by: Parth Gala [email protected]

@gitgitgadget
Copy link

gitgitgadget bot commented Jan 31, 2020

Welcome to GitGitGadget

Hi @ParthGala2k, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that your Pull Request has a good description, as it will be used as cover letter.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "commit:", and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the FreeNode IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join [email protected], where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Freenode. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@dscho
Copy link
Member

dscho commented Jan 31, 2020

@ParthGala2k welcome!

Calls to ARRAY_SIZE macro are currently in test part of the loop
and are thus redundant. Initialization outside will fetch size of
commands array faster.
Any advice for improving in any area on my part would be great.

This description would make sense inside the commit message.

Note, however, that ARRAY_SIZE is a macro (as indicated by the convention that preprocessor constants and macros are all upper-case), and defined thusly:

/*
 * ARRAY_SIZE - get the number of elements in a visible array
 * @x: the array whose size you want.
 *
 * This does not work on pointers, or arrays declared as [], or
 * function parameters.  With correct compiler support, such usage
 * will cause a build error (see the build_assert_or_zero macro).
 */
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))

The "function" sizeof() can be evaluated already very early, and the expression simplified, so that the compiler already handles the ARRAY_SIZE(commands) as a constant.

To see what I mean, compile Git via make and then run objdump --disassemble-all git | grep -A34 get_builtin.:. Over here, the output looks like this:

000000000001bf10 <get_builtin>:
   1bf10:       55                      push   %rbp
   1bf11:       48 89 e5                mov    %rsp,%rbp
   1bf14:       48 83 ec 20             sub    $0x20,%rsp
   1bf18:       48 89 7d e8             mov    %rdi,-0x18(%rbp)
   1bf1c:       c7 45 f4 00 00 00 00    movl   $0x0,-0xc(%rbp)
   1bf23:       eb 48                   jmp    1bf6d <get_builtin+0x5d>
   1bf25:       8b 45 f4                mov    -0xc(%rbp),%eax
   1bf28:       48 63 d0                movslq %eax,%rdx
   1bf2b:       48 89 d0                mov    %rdx,%rax
   1bf2e:       48 01 c0                add    %rax,%rax
   1bf31:       48 01 d0                add    %rdx,%rax
   1bf34:       48 c1 e0 03             shl    $0x3,%rax
   1bf38:       48 89 c2                mov    %rax,%rdx
   1bf3b:       48 8d 05 de 10 57 00    lea    0x5710de(%rip),%rax        # 58d020 <commands>
   1bf42:       48 01 d0                add    %rdx,%rax
   1bf45:       48 89 45 f8             mov    %rax,-0x8(%rbp)
   1bf49:       48 8b 45 f8             mov    -0x8(%rbp),%rax
   1bf4d:       48 8b 10                mov    (%rax),%rdx
   1bf50:       48 8b 45 e8             mov    -0x18(%rbp),%rax
   1bf54:       48 89 d6                mov    %rdx,%rsi
   1bf57:       48 89 c7                mov    %rax,%rdi
   1bf5a:       e8 c9 e2 ff ff          callq  1a228 <strcmp@plt>
   1bf5f:       85 c0                   test   %eax,%eax
   1bf61:       75 06                   jne    1bf69 <get_builtin+0x59>
   1bf63:       48 8b 45 f8             mov    -0x8(%rbp),%rax
   1bf67:       eb 13                   jmp    1bf7c <get_builtin+0x6c>
   1bf69:       83 45 f4 01             addl   $0x1,-0xc(%rbp)
   1bf6d:       8b 45 f4                mov    -0xc(%rbp),%eax
   1bf70:       3d 82 00 00 00          cmp    $0x82,%eax
   1bf75:       76 ae                   jbe    1bf25 <get_builtin+0x15>
   1bf77:       b8 00 00 00 00          mov    $0x0,%eax
   1bf7c:       c9                      leaveq
   1bf7d:       c3                      retq

See that jmp 1bf6d in the 7th line of the output? That's the jump to the condition at the end of the loop. After mov -0xc(%rbp),%eax (which loads the value of i into the register eax), you see cmp $0x82,%eax, i.e. it compares it with the verbatim constant 0x82, i.e. 130 in decimal notation, which is the number of built-ins currently stored in the commands array.

In short: I do not think that this PR's change does what you think it does, the compiler already optimizes out that expression.

@ParthGala2k
Copy link
Author

This description would make sense inside the commit message.

Alright.

See that jmp 1bf6d in the 7th line of the output? That's the jump to the condition at the end of the loop. After mov -0xc(%rbp),%eax (which loads the value of i into the register eax), you see cmp $0x82,%eax, i.e. it compares it with the verbatim constant 0x82, i.e. 130 in decimal notation, which is the number of built-ins currently stored in the commands array.

This makes it pretty clear, thanks.

@ParthGala2k ParthGala2k deleted the initialize-call-to-ARRAY_SIZE branch January 31, 2020 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants