Skip to content

Meter mode code refactor#1454

Merged
BenBE merged 12 commits intohtop-dev:mainfrom
Explorer09:meter-mode-refactor
Apr 20, 2024
Merged

Meter mode code refactor#1454
BenBE merged 12 commits intohtop-dev:mainfrom
Explorer09:meter-mode-refactor

Conversation

@Explorer09
Copy link
Copy Markdown
Contributor

The main goal of this set of patches is to remove the use of CUSTOM_METERMODE in defaultMode values, and to adjust Meter_setMode() code in preparation of the supportedModes feature (PR #1387).

@BenBE BenBE added enhancement Extension or improvement to existing feature code quality ♻️ Code quality enhancement labels Apr 15, 2024
@BenBE BenBE added this to the 3.4.0 milestone Apr 15, 2024
@BenBE
Copy link
Copy Markdown
Member

BenBE commented Apr 15, 2024

Any reason, why you dropped 2538896 from this series?

@Explorer09
Copy link
Copy Markdown
Contributor Author

@BenBE It's better for 2538896 to merge with your PR and that change is slightly irrelevant to the other parts of code refactoring here. Besides the function name nextSupportedMode would make less sense until the real supportedMode field is implemented.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Apr 16, 2024

Sure, just noticed when comparing your branches.

LGTM.

@Explorer09 Explorer09 force-pushed the meter-mode-refactor branch 3 times, most recently from 0437114 to 45d779a Compare April 16, 2024 09:56
@Explorer09 Explorer09 marked this pull request as draft April 16, 2024 10:01
@Explorer09 Explorer09 force-pushed the meter-mode-refactor branch from 45d779a to a4a08bc Compare April 16, 2024 13:36
@Explorer09 Explorer09 marked this pull request as ready for review April 16, 2024 13:38
@Explorer09 Explorer09 changed the title Meter mode code refactor (obsoleting CUSTOM_METERMODE) Meter mode code refactor Apr 16, 2024
@Explorer09
Copy link
Copy Markdown
Contributor Author

More code refactoring, and thus it's good to re-review this.

  • Make Meter_modes array private to Meter.c
  • Define type MeterModeId to uint8_t and make a separate header for it. (If I put that typedef in Meter.h it could cause a header dependency hell that I had a hard time resolving it.)

@Explorer09 Explorer09 force-pushed the meter-mode-refactor branch 2 times, most recently from 47d1c3b to 63b776c Compare April 16, 2024 16:30
Comment thread Settings.c Outdated
@Explorer09 Explorer09 force-pushed the meter-mode-refactor branch from 63b776c to e5bcc4f Compare April 17, 2024 08:56
@BenBE
Copy link
Copy Markdown
Member

BenBE commented Apr 17, 2024

  • In e5bcc4f there's unrelated changes in Header.c.
  • In e5bcc4f for MeterMode.h do typedef int MeterModeId; (saving on including stdint.h and several casts)
  • In e5bcc4f for Settings.c inside Settings_defaultMeters are unrelated changes
  • In dab8ebf in MeterMode.h consider typedef enum MeterModeId_ { … } MeterModeId;
  • In dab8ebf in MeterMode.h there should be two blank lines before declarations
  • In 2461e6e changes for code structure and functional changes are mixed

@Explorer09
Copy link
Copy Markdown
Contributor Author

@BenBE

  • In dab8ebf in MeterMode.h there should be two blank lines before declarations

Will do.

  • In e5bcc4f there's unrelated changes in Header.c.
  • In 2461e6e changes for code structure and functional changes are mixed
  • In e5bcc4f for Settings.c inside Settings_defaultMeters are unrelated changes

All of these are style changes being mixed with changes in behavior. Do you mean to drop the style changes or split them into seperate commits?

  • In e5bcc4f for MeterMode.h do typedef int MeterModeId; (saving on including stdint.h and several casts)
  • In dab8ebf in MeterMode.h consider typedef enum MeterModeId_ { … } MeterModeId;

I think you're interested in some size reports:

gcc-11 with options -O2 -flto -Wl,--as-needed

    text    data     bss     dec    hex
  359500   26248    6520  392268  5fc4c (a) MeterModeId in uint8_t
  359460   26248    6520  392228  5fc24 (b) MeterModeId in signed int
  359452   26248    6520  392220  5fc1c (c) MeterModeId in unsigned int
  359500   26248    6520  392268  5fc4c (d) uint8_t MeterModeId with reorder
                                            of Meter & MeterClass structures

gcc-11 with options -Os -flto -Wl,--as-needed

    text    data     bss     dec    hex
  282899   26320    6552  315771  4d17b (a) MeterModeId in uint8_t
  282867   26320    6552  315739  4d15b (b) MeterModeId in signed int
  282865   26320    6552  315737  4d159 (c) MeterModeId in unsigned int
  282899   26320    6552  315771  4d17b (d) uint8_t MeterModeId with reorder 
                                            of Meter & MeterClass structures

clang-16 with options -Os -flto -Wl,--as-needed

    text    data     bss     dec    hex
  291703   25380    6368  323451  4ef7b (a) MeterModeId in uint8_t
  291693   25380    6368  323441  4ef71 (b) MeterModeId in signed int
  291692   25380    6368  323440  4ef70 (c) MeterModeId in unsigned int
  291703   25004    6368  323075  4ee03 (d) uint8_t MeterModeId with reorder
                                            of Meter & MeterClass structures

I gathered these from GitHub Actions CI. And you are right in that using uint8_t for MeterModeId doesn't help with the code size, but interestingly making MeterModeId an unsigned int (the (c) case) can reduce a few bytes of the compiled binary.
The (d) case is interesting and I didn't figure out the cause yet (I moved the mode member to close to the maxItems and curItems members of the MeterClass and Meter structures, thus kills the unnecessary padding). I think I'll pick the (c) case.

@Explorer09
Copy link
Copy Markdown
Contributor Author

  • In 2461e6e changes for code structure and functional changes are mixed

I'm a little confused what you mean by "code structure and functional changes are mixed" and how should I split the commits.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Apr 18, 2024

@BenBE

  • In e5bcc4f there's unrelated changes in Header.c.
  • In 2461e6e changes for code structure and functional changes are mixed
  • In e5bcc4f for Settings.c inside Settings_defaultMeters are unrelated changes

All of these are style changes being mixed with changes in behavior. Do you mean to drop the style changes or split them into seperate commits?

In the first commit, there's this sizeof change in the commit, that's not purely code formatting, but a distinct change (although no functional difference). Similar goes for the other changes I listed. Just split those changes (you can merge the two sizeof changes in one commit afterwards).

  • In e5bcc4f for MeterMode.h do typedef int MeterModeId; (saving on including stdint.h and several casts)
  • In dab8ebf in MeterMode.h consider typedef enum MeterModeId_ { … } MeterModeId;

I think you're interested in some size reports:

gcc-11 with options -O2 -flto -Wl,--as-needed

    text    data     bss     dec    hex
  359500   26248    6520  392268  5fc4c (a) MeterModeId in uint8_t
  359460   26248    6520  392228  5fc24 (b) MeterModeId in signed int
  359452   26248    6520  392220  5fc1c (c) MeterModeId in unsigned int
  359500   26248    6520  392268  5fc4c (d) uint8_t MeterModeId with reorder
                                            of Meter & MeterClass structures

gcc-11 with options -Os -flto -Wl,--as-needed

    text    data     bss     dec    hex
  282899   26320    6552  315771  4d17b (a) MeterModeId in uint8_t
  282867   26320    6552  315739  4d15b (b) MeterModeId in signed int
  282865   26320    6552  315737  4d159 (c) MeterModeId in unsigned int
  282899   26320    6552  315771  4d17b (d) uint8_t MeterModeId with reorder 
                                            of Meter & MeterClass structures

clang-16 with options -Os -flto -Wl,--as-needed

    text    data     bss     dec    hex
  291703   25380    6368  323451  4ef7b (a) MeterModeId in uint8_t
  291693   25380    6368  323441  4ef71 (b) MeterModeId in signed int
  291692   25380    6368  323440  4ef70 (c) MeterModeId in unsigned int
  291703   25004    6368  323075  4ee03 (d) uint8_t MeterModeId with reorder
                                            of Meter & MeterClass structures

I gathered these from GitHub Actions CI. And you are right in that using uint8_t for MeterModeId doesn't help with the code size, but interestingly making MeterModeId an unsigned int (the (c) case) can reduce a few bytes of the compiled binary. The (d) case is interesting and I didn't figure out the cause yet (I moved the mode member to close to the maxItems and curItems members of the MeterClass and Meter structures, thus kills the unnecessary padding). I think I'll pick the (c) case.

(b) and (c) are fine. Basically we have (b) right now. Be sure to update the format strings accordingly and you'll be able to skip that one cast.

The use of CUSTOM_METERMODE value in meter default mode was a bad
design. There are no meter that really has a "custom" mode to work with
and currently that value serves as a useless placeholder that hides the
real default mode for a meter.

Replace CUSTOM_METERMODE in `defaultMode` of all meters with the real
intended default modes. Currently only CPU meters and MemorySwapMeter
used this, and their real defaults are BAR_METERMODE.

In Meter_setMode(), remove the special treatment of `defaultMode ==
CUSTOM_METERMODE`, Meter_setMode() still calls the `updateMode` function
for custom treatment when it's present for a meter class.

As CUSTOM_METERMODE is obsolete from `defaultMode`, the init functions
of CPU meters and MemorySwapMeter need to be adjusted to avoid
incomplete initialization (Meter.draw function bring NULL).

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Let the respective `MeterClass.updateMode` functions initialize the
meter mode instead. The `updateMode` function is always called after the
`MeterClass.init` function by `Meter_new()`.

This not only simplifies the init functions of meter subclasses, but
avoids the use of the global `Meter_modes` array when it's unnecessary.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Meter mode ID 0 will now be reserved.
The Meter objects have their own 'h' properties.
Avoid access to the `Meter_modes` array.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
The Meter objects have their own 'h' properties.
Avoid access to the `Meter_modes` array.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
This section reordering is a prerequisite to privatizing the
`Meter_modes` array.
All client codes that access this global `Meter_modes` array have been
replaced in previous commits, thus it's no longer necessary to keep
this internal information (draw functions, default heights, etc.)
public. It was also a bad idea when the client codes need to avoid
accessing `Meter_modes[0]` (which is reserved and contains null
information) by themselves.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
This is a prerequisite to using MeterModeId type in more headers, to
avoid header dependency hell.
All uses of meter drawing "mode" numbers now have the type
`MeterModeId`, including the uses in structures and arrays.
`MeterModeId` is defined as `unsigned int`, as (currently) it doesn't
save any code size by defining it to any smaller type.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
@Explorer09 Explorer09 force-pushed the meter-mode-refactor branch from e77a3d4 to e2fd54c Compare April 20, 2024 10:33
Copy link
Copy Markdown
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

LGTM.

@BenBE BenBE force-pushed the meter-mode-refactor branch from e2fd54c to a2ab9ad Compare April 20, 2024 17:07
@BenBE BenBE merged commit 6f14269 into htop-dev:main Apr 20, 2024
@Explorer09 Explorer09 deleted the meter-mode-refactor branch April 20, 2024 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code quality ♻️ Code quality enhancement enhancement Extension or improvement to existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants