Skip to content

Commit 52225f0

Browse files
committed
AST: allow for more precise allocation statistics
* add `NCat` to specify number of size classes (default 1) * modify `NCat` and rebuild project to get finer statistics * must rebuild plugins because of layout incompatibility
1 parent af4ba19 commit 52225f0

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

ast/statistics.c2

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ module ast;
1919
import string;
2020
import stdio local;
2121

22+
const u32 NCat = 1;
23+
2224
type Stat struct {
23-
u32 count;
24-
u32 size;
25+
u32[NCat] count;
26+
u32[NCat] size;
2527
}
2628

2729
type Stats struct {
@@ -38,41 +40,64 @@ fn void Stats.reset(Stats* s) {
3840
string.memset(s, 0, sizeof(Stats));
3941
}
4042

43+
fn void Stat.add(Stat* ss, u32 size) {
44+
u32 cat = (size + 7) >> 3;
45+
if (cat >= NCat) cat = NCat - 1;
46+
ss.count[cat]++;
47+
ss.size[cat] += size;
48+
}
49+
4150
fn void Stats.addType(TypeKind kind, u32 size) {
42-
globals.stats.types[kind].count++;
43-
globals.stats.types[kind].size += size;
51+
globals.stats.types[kind].add(size);
4452
}
4553

4654
fn void Stats.addExpr(ExprKind kind, u32 size) {
47-
globals.stats.exprs[kind].count++;
48-
globals.stats.exprs[kind].size += size;
55+
globals.stats.exprs[kind].add(size);
4956
}
5057

5158
fn void Stats.addStmt(StmtKind kind, u32 size) {
52-
globals.stats.stmts[kind].count++;
53-
globals.stats.stmts[kind].size += size;
59+
globals.stats.stmts[kind].add(size);
5460
}
5561

5662
fn void Stats.addDecl(DeclKind kind, u32 size) {
57-
globals.stats.decls[kind].count++;
58-
globals.stats.decls[kind].size += size;
63+
globals.stats.decls[kind].add(size);
5964
}
6065

6166
const char*[] other_names = { "ArrayValue", "StaticAssert", "SwitchCase" }
6267

6368
fn void Stats.addArrayValue(u32 size) {
64-
globals.stats.others[0].count++;
65-
globals.stats.others[0].size += size;
69+
globals.stats.others[0].add(size);
6670
}
6771

6872
fn void Stats.addStaticAssert(u32 size) {
69-
globals.stats.others[1].count++;
70-
globals.stats.others[1].size += size;
73+
globals.stats.others[1].add(size);
7174
}
7275

7376
fn void Stats.addSwitchCase(u32 size) {
74-
globals.stats.others[2].count++;
75-
globals.stats.others[2].size += size;
77+
globals.stats.others[2].add(size);
78+
}
79+
80+
fn void Stat.dump(const Stat* ss, const char* name, u32* countp, u32* totalp) {
81+
u32 count = 0;
82+
u32 size = 0;
83+
for (u32 cat = 0; cat < NCat; cat++) {
84+
count += ss.count[cat];
85+
size += ss.size[cat];
86+
}
87+
printf(" %20s %6d %7d", name, count, size);
88+
bool output = false;
89+
for (u32 cat = 0; cat < NCat; cat++) {
90+
if (!ss.count[cat]) continue;
91+
if (count == ss.count[cat]) {
92+
// no repeat if single category
93+
if (NCat > 1) printf(" %d", cat * 8);
94+
break;
95+
}
96+
printf(" %d:%d/%d", cat * 8, ss.count[cat], ss.size[cat]);
97+
}
98+
printf("\n");
99+
*countp += count;
100+
*totalp += size;
76101
}
77102

78103
fn void Stats.dump(const Stats* s) {
@@ -83,10 +108,7 @@ fn void Stats.dump(const Stats* s) {
83108
u32 typesCount = 0;
84109
//for (u32 i = TypeKind.min; i <= TypeKind.max; i++) {
85110
for (u32 i=enum_min(TypeKind); i<=enum_max(TypeKind); i++) {
86-
const Stat* ss = &s.types[i];
87-
printf(" %20s %6d %7d\n", typeKind_names[i], ss.count, ss.size);
88-
typesCount += ss.count;
89-
typesTotal += ss.size;
111+
s.types[i].dump(typeKind_names[i], &typesCount, &typesTotal);
90112
}
91113
printf(" %20s %6d %7d\n", "total", typesCount, typesTotal);
92114

@@ -95,10 +117,7 @@ fn void Stats.dump(const Stats* s) {
95117
u32 exprCount = 0;
96118
//for (u32 i = ExprKind.min; i <= ExprKind.max; i++) {
97119
for (u32 i=enum_min(ExprKind); i<=enum_max(ExprKind); i++) {
98-
const Stat* ss = &s.exprs[i];
99-
printf(" %20s %6d %7d\n", exprKind_names[i], ss.count, ss.size);
100-
exprCount += ss.count;
101-
exprTotal += ss.size;
120+
s.exprs[i].dump(exprKind_names[i], &exprCount, &exprTotal);
102121
}
103122
printf(" %20s %6d %7d\n", "total", exprCount, exprTotal);
104123

@@ -107,10 +126,7 @@ fn void Stats.dump(const Stats* s) {
107126
u32 stmtCount = 0;
108127
//for (u32 i = StmtKind.min; i <= StmtKind.max; i++) {
109128
for (u32 i=enum_min(StmtKind); i<=enum_max(StmtKind); i++) {
110-
const Stat* ss = &s.stmts[i];
111-
printf(" %20s %6d %7d\n", stmtKind_names[i], ss.count, ss.size);
112-
stmtCount += ss.count;
113-
stmtTotal += ss.size;
129+
s.stmts[i].dump(stmtKind_names[i], &stmtCount, &stmtTotal);
114130
}
115131
printf(" %20s %6d %7d\n", "total", stmtCount, stmtTotal);
116132

@@ -119,21 +135,15 @@ fn void Stats.dump(const Stats* s) {
119135
u32 declCount = 0;
120136
//for (u32 i = DeclKind.min; i <= DeclKind.max; i++) {
121137
for (u32 i=enum_min(DeclKind); i<=enum_max(DeclKind); i++) {
122-
const Stat* ss = &s.decls[i];
123-
printf(" %20s %6d %7d\n", declKind_names[i], ss.count, ss.size);
124-
declCount += ss.count;
125-
declTotal += ss.size;
138+
s.decls[i].dump(declKind_names[i], &declCount, &declTotal);
126139
}
127140
printf(" %20s %6d %7d\n", "total", declCount, declTotal);
128141

129142
printf("--- Other ---\n");
130143
u32 otherTotal = 0;
131144
u32 otherCount = 0;
132145
for (u32 i=0; i<3; i++) {
133-
const Stat* ss = &s.others[i];
134-
printf(" %20s %6d %7d\n", other_names[i], ss.count, ss.size);
135-
otherCount += ss.count;
136-
otherTotal += ss.size;
146+
s.others[i].dump(other_names[i], &otherCount, &otherTotal);
137147
}
138148
printf(" %20s %6d %7d\n", "total", otherCount, otherTotal);
139149

0 commit comments

Comments
 (0)