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

Refactor quantum/command.{c,h} for code size & {read,maintain}ability #11842

Merged
merged 3 commits into from
Aug 7, 2021

Conversation

liyang
Copy link

@liyang liyang commented Feb 9, 2021

I accidentally a huge refactor. (This is part 1 of n.) It improves the code size & {read,maintain}ability significantly.

This PR reduces the firmware size by ~1.5kB on my ATmega32, when built with CONSOLE_ENABLE and COMMAND_ENABLE set to yes.

If possible, could this PR be merged rather than squashed?

Description

Because the last commit moves mousekey_console() to mousekey.c (verbatim, trust me :), I recommend reading the penultimate commit separately to see the changes to mousekey_console() itself.

commit 3ec12a579518e1dda45b3163d042d4d781a6850c
Author: Liyang HU <git@liyang.hu>
Date:   Sun Feb 7 16:19:52 2021 +0000

    quantum/command.c: optimise `mousekey_console()` for size & legibility

    Made various tweaks to the interface, but still functionally identical.

    Assume `KC_1`…`KC_0` to be contiguous, and removed `numkey2num(…)` entirely.
    It was exported in `command.h` by 1a0bac8bcc for no obvious reason, before
    which it was `static`. I doubt anyone uses it.

    `mousekey_console()` is now enabled regardless of `MK_3_SPEED`.
    Needs fleshing out for things other than the X11 variant though.

    This commit saves 638 bytes on my ATmega32.

commit 003835288cb193ffda71671bb379bab716c01f2f
Author: Liyang HU <git@liyang.hu>
Date:   Mon Feb 8 11:58:48 2021 +0000

    quantum/command.c: replace `print(…); print_{,val_}{dec,hex}*(…);` sequences with single `xprintf(…)`

    `print_{dec,hex}*(…)` are just `#define`s for `xprintf(…)` anyway.

    Each additional `xprintf(…)` costs ~8 bytes: the call instructions,
    plus an additional NUL terminator.

    This _really_ adds up: this commit saves 814 bytes on my ATmega32.

commit ea79897bb1e957a69e5f26ce67c30996c7882051
Author: Liyang HU <git@liyang.hu>
Date:   Sun Feb 7 13:46:23 2021 +0000

    quantum/command.c: coalesce `print()`s in `command_common_help()` & `print_version()`

    Also undo some damage by clang-format in b624f32f94

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout/userspace (addition or update)
  • Documentation

Issues Fixed or Closed by This PR

  • n/a

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@github-actions github-actions bot added the core label Feb 9, 2021
@liyang liyang marked this pull request as ready for review February 9, 2021 13:55
@liyang liyang marked this pull request as draft February 9, 2021 14:06
@github-actions github-actions bot added cli qmk cli command keymap labels Feb 9, 2021
@github-actions github-actions bot removed cli qmk cli command keymap labels Feb 9, 2021
@liyang
Copy link
Author

liyang commented Feb 10, 2021

Travis errored on something unrelated:

Making keebio/dsp40/rev1 with keymap default                                                           [ERRORS]

@liyang liyang marked this pull request as ready for review February 10, 2021 02:43
…print_version()`

Also undo some damage by clang-format in b624f32
…quences with single `xprintf(…)`

`print_{dec,hex}*(…)` are just `#define`s for `xprintf(…)` anyway.

Each additional `xprintf(…)` costs ~8 bytes: the call instructions,
plus an additional NUL terminator.

This _really_ adds up: this commit saves 814 bytes on my ATmega32.
Made various tweaks to the interface, but still functionally identical.

Assume `KC_1`…`KC_0` to be contiguous, and removed `numkey2num(…)` entirely.
It was exported in `command.h` by 1a0bac8 for no obvious reason, before
which it was `static`. I doubt anyone uses it.

`mousekey_console()` is now enabled regardless of `MK_3_SPEED`.
Needs fleshing out for things other than the X11 variant though.

This commit saves 638 bytes on my ATmega32.
@spidey3
Copy link
Contributor

spidey3 commented Feb 20, 2021

This mostly looks good, but I would have preferred if the last change a6576c5 were done in a separate PR.

@liyang
Copy link
Author

liyang commented Feb 24, 2021

[…] I would have preferred if the last change a6576c5 were done in a separate PR.

Done!

@tzarc tzarc closed this Feb 27, 2021
@tzarc tzarc deleted the branch qmk:develop February 27, 2021 20:28
@tzarc
Copy link
Member

tzarc commented Feb 27, 2021

Sorry about that, GitHub decided to delete the develop branch from upstream when we merged it, despite being told otherwise. Reopened.

@tzarc tzarc reopened this Feb 27, 2021
@stale
Copy link

stale bot commented Apr 18, 2021

Thank you for your contribution!
This pull request has been automatically marked as stale because it has not had activity in the last 45 days. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, or re-open when it's ready.
For maintainers: Please label with awaiting review, breaking_change, in progress, or on hold to prevent the issue from being re-flagged.

@drashna drashna requested a review from a team April 19, 2021 03:59
@stale stale bot removed the awaiting changes label Apr 19, 2021
Copy link
Member

@Erovia Erovia left a comment

Choose a reason for hiding this comment

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

__attribute__ ((weak))

@Erovia Erovia requested a review from a team May 7, 2021 18:34
@stale
Copy link

stale bot commented Jun 22, 2021

Thank you for your contribution!
This pull request has been automatically marked as stale because it has not had activity in the last 45 days. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, or re-open when it's ready.
For maintainers: Please label with awaiting review, breaking_change, in progress, or on hold to prevent the issue from being re-flagged.

@@ -28,8 +28,7 @@ bool command_extra(uint8_t code);
bool command_console_extra(uint8_t code);

#ifdef COMMAND_ENABLE
uint8_t numkey2num(uint8_t code);
bool command_proc(uint8_t code);
bool command_proc(uint8_t code);
Copy link
Member

Choose a reason for hiding this comment

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

I think clang-format will disagree with this (at least, the version of clang-format that the Docker image uses).

@stale stale bot removed the awaiting changes label Jun 22, 2021
@tzarc tzarc merged commit 383fae5 into qmk:develop Aug 7, 2021
cadusk pushed a commit to cadusk/qmk_firmware that referenced this pull request Aug 9, 2021
* qmk/develop: (175 commits)
  Add support for STM32F407x MCUs. (qmk#13718)
  [Keyboard] Add Pancake v2 (qmk#13839)
  [Keyboard] Added CapsLED and ScrollLock LEDs (qmk#13837)
  [Keymap] Drashna split transport improvement (qmk#13905)
  [Keyboard] a1200 converter minor changes (qmk#13848)
  [Keyboard] Gorthage Truck - New PCB (qmk#13909)
  [Keyboard] Clean up lfkpad and add keymap (qmk#13881)
  [Keyboard] Adding my Nyquist keymap (qmk#13858)
  [Keyboard] Fix matrix_output_unselect_delay for handwired/xealousbrown (qmk#13913)
  [Keyboard] fixes for KBD67 rev2 (qmk#13906)
  Clean up remaining RGB_DISABLE_WHEN_USB_SUSPENDED defines Part 2 (qmk#13912)
  Refactor `quantum/command.{c,h}` for code size & {read,maintain}ability (qmk#11842)
  Remove Full Bootmagic (qmk#13846)
  [Keyboard] Added 67mk_E PCB (qmk#13869)
  [Keyboard] Modify key drive pins for mojo68 (qmk#13863)
  [Keyboard] Use new matrix_output_select_delay api (qmk#13861)
  [Keyboard] add handwired/oem_ansi_fullsize (qmk#13857)
  [Keymap] JackKenney's keymap for GMMK Pro (qmk#13853)
  [Keyboard] Fix oled_task_user for chocolatebar (qmk#13911)
  clean up CRLF instances (qmk#13910)
  ...
nhongooi pushed a commit to nhongooi/qmk_firmware that referenced this pull request Dec 5, 2021
…ty (qmk#11842)

* quantum/command.c: coalesce `print()`s in `command_common_help()` & `print_version()`

Also undo some damage by clang-format in b624f32

* quantum/command.c: replace `print(…); print_{,val_}{dec,hex}*(…);` sequences with single `xprintf(…)`

`print_{dec,hex}*(…)` are just `#define`s for `xprintf(…)` anyway.

Each additional `xprintf(…)` costs ~8 bytes: the call instructions,
plus an additional NUL terminator.

This _really_ adds up: this commit saves 814 bytes on my ATmega32.

* quantum/command.c: optimise `mousekey_console()` for size & legibility

Made various tweaks to the interface, but still functionally identical.

Assume `KC_1`…`KC_0` to be contiguous, and removed `numkey2num(…)` entirely.
It was exported in `command.h` by 1a0bac8 for no obvious reason, before
which it was `static`. I doubt anyone uses it.

`mousekey_console()` is now enabled regardless of `MK_3_SPEED`.
Needs fleshing out for things other than the X11 variant though.

This commit saves 638 bytes on my ATmega32.
BorisTestov pushed a commit to BorisTestov/qmk_firmware that referenced this pull request May 23, 2024
…ty (qmk#11842)

* quantum/command.c: coalesce `print()`s in `command_common_help()` & `print_version()`

Also undo some damage by clang-format in c3e062c

* quantum/command.c: replace `print(…); print_{,val_}{dec,hex}*(…);` sequences with single `xprintf(…)`

`print_{dec,hex}*(…)` are just `#define`s for `xprintf(…)` anyway.

Each additional `xprintf(…)` costs ~8 bytes: the call instructions,
plus an additional NUL terminator.

This _really_ adds up: this commit saves 814 bytes on my ATmega32.

* quantum/command.c: optimise `mousekey_console()` for size & legibility

Made various tweaks to the interface, but still functionally identical.

Assume `KC_1`…`KC_0` to be contiguous, and removed `numkey2num(…)` entirely.
It was exported in `command.h` by ffc1b68 for no obvious reason, before
which it was `static`. I doubt anyone uses it.

`mousekey_console()` is now enabled regardless of `MK_3_SPEED`.
Needs fleshing out for things other than the X11 variant though.

This commit saves 638 bytes on my ATmega32.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants