Skip to content

Commit 5b167cf

Browse files
leifericfclaude
andcommitted
prim: tally reduce_step by reducer prim under MINO_REDUCE_STEP_COUNTS
Inert at the default build; -DMINO_REDUCE_STEP_COUNTS=1 enables a per-process bucketed tally (prim_add / prim_mul / prim_sub / bit ops / other-prim / user-fn) that dumps to stderr from atexit. Used to gate the inline-reducer fast lane. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3d4c8b4 commit 5b167cf

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

src/prim/sequences.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,68 @@
1010
#include "runtime/host_threads.h"
1111
#include "collections/internal.h" /* make_fn */
1212

13+
#ifdef MINO_REDUCE_STEP_COUNTS
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
17+
/* Tally of reduce_step invocations bucketed by reducer prim identity.
18+
* Populated when the binary is built with -DMINO_REDUCE_STEP_COUNTS=1.
19+
* Dumped to stderr at process exit. Used to gate item A of the pre-JIT
20+
* sweep: confirm canonical numeric reducers (+, *, etc.) dominate
21+
* real-workload reduce_step traffic. Not for production. */
22+
static size_t g_rstep_add;
23+
static size_t g_rstep_mul;
24+
static size_t g_rstep_sub;
25+
static size_t g_rstep_band;
26+
static size_t g_rstep_bor;
27+
static size_t g_rstep_bxor;
28+
static size_t g_rstep_other_prim;
29+
static size_t g_rstep_user_fn;
30+
static int g_rstep_atexit_done;
31+
32+
static void rstep_counts_dump(void)
33+
{
34+
size_t total = g_rstep_add + g_rstep_mul + g_rstep_sub
35+
+ g_rstep_band + g_rstep_bor + g_rstep_bxor
36+
+ g_rstep_other_prim + g_rstep_user_fn;
37+
fprintf(stderr, "reduce-step-counts: total = %zu\n", total);
38+
if (total == 0) return;
39+
#define ROW(name, c) \
40+
fprintf(stderr, " %-20s %12zu %6.2f%%\n", (name), (c), \
41+
100.0 * (double)(c) / (double)total)
42+
ROW("prim_add", g_rstep_add);
43+
ROW("prim_mul", g_rstep_mul);
44+
ROW("prim_sub", g_rstep_sub);
45+
ROW("prim_bit_and", g_rstep_band);
46+
ROW("prim_bit_or", g_rstep_bor);
47+
ROW("prim_bit_xor", g_rstep_bxor);
48+
ROW("other_prim", g_rstep_other_prim);
49+
ROW("user_fn", g_rstep_user_fn);
50+
#undef ROW
51+
}
52+
53+
static void rstep_count(mino_val_t *fn)
54+
{
55+
if (!g_rstep_atexit_done) {
56+
atexit(rstep_counts_dump);
57+
g_rstep_atexit_done = 1;
58+
}
59+
if (fn != NULL && mino_type_of(fn) == MINO_PRIM
60+
&& fn->as.prim.fn != NULL) {
61+
mino_prim_fn p = fn->as.prim.fn;
62+
if (p == prim_add) g_rstep_add++;
63+
else if (p == prim_mul) g_rstep_mul++;
64+
else if (p == prim_sub) g_rstep_sub++;
65+
else if (p == prim_bit_and) g_rstep_band++;
66+
else if (p == prim_bit_or) g_rstep_bor++;
67+
else if (p == prim_bit_xor) g_rstep_bxor++;
68+
else g_rstep_other_prim++;
69+
} else {
70+
g_rstep_user_fn++;
71+
}
72+
}
73+
#endif
74+
1375
/* ------------------------------------------------------------------------- */
1476
/* seq and realized? */
1577
/* ------------------------------------------------------------------------- */
@@ -505,6 +567,9 @@ static int reduce_step(mino_state_t *S, mino_val_t *fn, mino_val_t **acc_io,
505567
mino_val_t *elem, mino_env_t *env)
506568
{
507569
mino_val_t *acc = *acc_io;
570+
#ifdef MINO_REDUCE_STEP_COUNTS
571+
rstep_count(fn);
572+
#endif
508573
if (fn != NULL && mino_type_of(fn) == MINO_PRIM
509574
&& fn->as.prim.fn != NULL
510575
&& acc != NULL && mino_val_int_p(acc)

0 commit comments

Comments
 (0)