From b045436c23a7607a3cd808da0fe9279909fd16ea Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 18 Feb 2022 23:51:52 -0600 Subject: [PATCH] Added size-sort options to scripts/code.py Now with -s/--sort and -S/--reverse-sort for sorting the functions by size. You may wonder why add reverse-sort, since its utility doesn't seem worth the cost to implement (these are just helper scripts after all), the reason is that reverse-sort is quite useful on the command-line, where scrollback may be truncated, and you only care about the larger entries. Outside of the command-line, normal sort is prefered. Fortunately the difference is just the sign in the sort key. Note this conflicts with the short --summary flag, so that has been removed. --- .github/workflows/release.yml | 12 ++++++------ .github/workflows/test.yml | 4 ++-- Makefile | 2 +- scripts/code.py | 27 +++++++++++++++++++++++---- scripts/coverage.py | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a1a1a43..cf856c9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -84,7 +84,7 @@ jobs: select(.context == "results / code").description | capture("Code size is (?[0-9]+)").result' \ prev-results.json || echo 0)" - ./scripts/code.py -u results/code-thumb.csv -s | awk ' + ./scripts/code.py -u results/code-thumb.csv --summary | awk ' NR==2 {printf "Code size,%d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]} @@ -95,7 +95,7 @@ jobs: select(.context == "results / code (readonly)").description | capture("Code size is (?[0-9]+)").result' \ prev-results.json || echo 0)" - ./scripts/code.py -u results/code-thumb-readonly.csv -s | awk ' + ./scripts/code.py -u results/code-thumb-readonly.csv --summary | awk ' NR==2 {printf "Code size
(readonly),%d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]} @@ -106,7 +106,7 @@ jobs: select(.context == "results / code (threadsafe)").description | capture("Code size is (?[0-9]+)").result' \ prev-results.json || echo 0)" - ./scripts/code.py -u results/code-thumb-threadsafe.csv -s | awk ' + ./scripts/code.py -u results/code-thumb-threadsafe.csv --summary | awk ' NR==2 {printf "Code size
(threadsafe),%d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]} @@ -117,7 +117,7 @@ jobs: select(.context == "results / code (migrate)").description | capture("Code size is (?[0-9]+)").result' \ prev-results.json || echo 0)" - ./scripts/code.py -u results/code-thumb-migrate.csv -s | awk ' + ./scripts/code.py -u results/code-thumb-migrate.csv --summary | awk ' NR==2 {printf "Code size
(migrate),%d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]} @@ -128,7 +128,7 @@ jobs: select(.context == "results / code (error-asserts)").description | capture("Code size is (?[0-9]+)").result' \ prev-results.json || echo 0)" - ./scripts/code.py -u results/code-thumb-error-asserts.csv -s | awk ' + ./scripts/code.py -u results/code-thumb-error-asserts.csv --summary | awk ' NR==2 {printf "Code size
(error-asserts),%d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]} @@ -139,7 +139,7 @@ jobs: select(.context == "results / coverage").description | capture("Coverage is (?[0-9\\.]+)").result' \ prev-results.json || echo 0)" - ./scripts/coverage.py -u results/coverage.csv -s | awk -F '[ /%]+' ' + ./scripts/coverage.py -u results/coverage.csv --summary | awk -F '[ /%]+' ' NR==2 {printf "Coverage,%.1f%% of %d lines",$4,$3} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",$4-ENVIRON["PREV"]} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6d633f8..44dcf8b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -229,7 +229,7 @@ jobs: | select(.context == env.CONTEXT).description | capture("Code size is (?[0-9]+)").result' \ || echo 0)" - export DESCRIPTION="$(./scripts/code.py -u $f -s | awk ' + export DESCRIPTION="$(./scripts/code.py -u $f --summary | awk ' NR==2 {printf "Code size is %d B",$2} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",100*($2-ENVIRON["PREV"])/ENVIRON["PREV"]}')" @@ -427,7 +427,7 @@ jobs: | capture("Coverage is (?[0-9\\.]+)").result' \ || echo 0)" export DESCRIPTION="$( - ./scripts/coverage.py -u results/coverage.csv -s | awk -F '[ /%]+' ' + ./scripts/coverage.py -u results/coverage.csv --summary | awk -F '[ /%]+' ' NR==2 {printf "Coverage is %.1f%% of %d lines",$4,$3} NR==2 && ENVIRON["PREV"]+0 != 0 { printf " (%+.1f%%)",$4-ENVIRON["PREV"]}')" diff --git a/Makefile b/Makefile index 763a0ce..4ac223e 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,7 @@ tags: .PHONY: code code: $(OBJ) - ./scripts/code.py $^ $(CODEFLAGS) + ./scripts/code.py -S $^ $(CODEFLAGS) .PHONY: test test: diff --git a/scripts/code.py b/scripts/code.py index 08b33a1..574b4e5 100755 --- a/scripts/code.py +++ b/scripts/code.py @@ -126,6 +126,22 @@ def main(**args): diff[name] = (old, new, new-old, (new-old)/old if old else 1.0) return diff + def sorted_entries(entries): + if args.get('size_sort'): + return sorted(entries.items(), key=lambda x: (-x[1], x)) + elif args.get('reverse_size_sort'): + return sorted(entries.items(), key=lambda x: (+x[1], x)) + else: + return sorted(entries.items()) + + def sorted_diff_entries(entries): + if args.get('size_sort'): + return sorted(entries.items(), key=lambda x: (-x[1][1], x)) + elif args.get('reverse_size_sort'): + return sorted(entries.items(), key=lambda x: (+x[1][1], x)) + else: + return sorted(entries.items(), key=lambda x: (-x[1][3], x)) + def print_header(by=''): if not args.get('diff'): print('%-36s %7s' % (by, 'size')) @@ -137,7 +153,7 @@ def main(**args): if not args.get('diff'): print_header(by=by) - for name, size in sorted(entries.items()): + for name, size in sorted_entries(entries): print("%-36s %7d" % (name, size)) else: prev_entries = dedup_entries(prev_results, by=by) @@ -145,8 +161,7 @@ def main(**args): print_header(by='%s (%d added, %d removed)' % (by, sum(1 for old, _, _, _ in diff.values() if not old), sum(1 for _, new, _, _ in diff.values() if not new))) - for name, (old, new, diff, ratio) in sorted(diff.items(), - key=lambda x: (-x[1][3], x)): + for name, (old, new, diff, ratio) in sorted_diff_entries(diff): if ratio or args.get('all'): print("%-36s %7s %7s %+7d%s" % (name, old or "-", @@ -196,10 +211,14 @@ if __name__ == "__main__": help="Specify CSV file to diff code size against.") parser.add_argument('-a', '--all', action='store_true', help="Show all functions, not just the ones that changed.") + parser.add_argument('-s', '--size-sort', action='store_true', + help="Sort by size.") + parser.add_argument('-S', '--reverse-size-sort', action='store_true', + help="Sort by size, but backwards.") parser.add_argument('--files', action='store_true', help="Show file-level code sizes. Note this does not include padding! " "So sizes may differ from other tools.") - parser.add_argument('-s', '--summary', action='store_true', + parser.add_argument('--summary', action='store_true', help="Only show the total code size.") parser.add_argument('-q', '--quiet', action='store_true', help="Don't show anything, useful with -o.") diff --git a/scripts/coverage.py b/scripts/coverage.py index 6f1f54f..7abdedb 100755 --- a/scripts/coverage.py +++ b/scripts/coverage.py @@ -247,7 +247,7 @@ if __name__ == "__main__": help="Show all functions, not just the ones that changed.") parser.add_argument('--files', action='store_true', help="Show file-level coverage.") - parser.add_argument('-s', '--summary', action='store_true', + parser.add_argument('--summary', action='store_true', help="Only show the total coverage.") parser.add_argument('-q', '--quiet', action='store_true', help="Don't show anything, useful with -o.")