Skip to content

Commit

Permalink
Happier cache lines.
Browse files Browse the repository at this point in the history
Probably the only change that actually affects something, `struct ir_state` is both important and now fits within a single cache line.

I did this by eyeballing:
```
; bmake -r -j 10 CC=clang DEBUG=1 && (for t in `cat /tmp/tags`; do pahole --class_name $t build/src/*/*.o build/src/adt/*.o ; done ) > /tmp/tt
```

This gives a ~2% improvement on total execution for rx -q /usr/share/bin/words
  • Loading branch information
katef committed Aug 24, 2024
1 parent d73682b commit ac91f5c
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/adt/edgeset.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ struct edge_set {
size_t ceil; /* nonzero */
size_t count; /* <= ceil */
struct edge_group {
fsm_state_t to; /* distinct */
uint64_t symbols[256/64];
fsm_state_t to; /* distinct */
} *groups; /* sorted by .to */
};

Expand Down
13 changes: 7 additions & 6 deletions src/libfsm/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include <fsm/fsm.h>
#include <fsm/pred.h>
Expand Down Expand Up @@ -86,7 +87,7 @@ fsm_clone(const struct fsm *fsm)

struct copy_capture_actions_env {
struct fsm *dst;
int ok;
bool ok;
};

static int
Expand All @@ -99,7 +100,7 @@ copy_capture_actions_cb(fsm_state_t state,

if (!fsm_capture_add_action(env->dst,
state, type, capture_id, to)) {
env->ok = 0;
env->ok = false;
}

return env->ok;
Expand All @@ -108,7 +109,7 @@ copy_capture_actions_cb(fsm_state_t state,
static int
copy_capture_actions(struct fsm *dst, const struct fsm *src)
{
struct copy_capture_actions_env env = { NULL, 1 };
struct copy_capture_actions_env env = { NULL, true };
env.dst = dst;

fsm_capture_action_iter(src,
Expand All @@ -120,7 +121,7 @@ struct copy_end_ids_env {
char tag;
struct fsm *dst;
const struct fsm *src;
int ok;
bool ok;
};

static int
Expand All @@ -134,7 +135,7 @@ copy_end_ids_cb(fsm_state_t state, const fsm_end_id_t id, void *opaque)
#endif

if (!fsm_endid_set(env->dst, state, id)) {
env->ok = 0;
env->ok = false;
return 0;
}

Expand All @@ -148,7 +149,7 @@ copy_end_ids(struct fsm *dst, const struct fsm *src)
env.tag = 'c'; /* for clone */
env.dst = dst;
env.src = src;
env.ok = 1;
env.ok = true;

fsm_endid_iter(src, copy_end_ids_cb, &env);

Expand Down
7 changes: 4 additions & 3 deletions src/libfsm/consolidate.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

#include <fsm/fsm.h>
Expand Down Expand Up @@ -39,7 +40,7 @@ struct consolidate_copy_capture_actions_env {
struct fsm *dst;
size_t mapping_count;
const fsm_state_t *mapping;
int ok;
bool ok;
};

static int
Expand Down Expand Up @@ -186,7 +187,7 @@ consolidate_copy_capture_actions_cb(fsm_state_t state,

if (!fsm_capture_add_action(env->dst,
s, type, capture_id, t)) {
env->ok = 0;
env->ok = false;
return 0;
}

Expand All @@ -204,7 +205,7 @@ consolidate_copy_capture_actions(struct fsm *dst, const struct fsm *src,
env.dst = dst;
env.mapping_count = mapping_count;
env.mapping = mapping;
env.ok = 1;
env.ok = true;

#if LOG_MAPPING
for (i = 0; i < mapping_count; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/libfsm/determinise_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>

Expand Down Expand Up @@ -85,7 +86,7 @@ struct det_copy_capture_actions_env {
char tag;
struct fsm *dst;
struct reverse_mapping *reverse_mappings;
int ok;
bool ok;
};

#define MAPPINGSTACK_DEF_CEIL 16
Expand Down
7 changes: 4 additions & 3 deletions src/libfsm/endids.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>

Expand Down Expand Up @@ -755,7 +756,7 @@ struct carry_env {
char tag;
struct fsm *dst;
fsm_state_t dst_state;
int ok;
bool ok;
};

static int
Expand All @@ -767,7 +768,7 @@ carry_iter_cb(fsm_state_t state, fsm_end_id_t id, void *opaque)
(void)state;

if (!fsm_endid_set(env->dst, env->dst_state, id)) {
env->ok = 0;
env->ok = false;
return 0;
}
return 1;
Expand Down Expand Up @@ -798,7 +799,7 @@ fsm_endid_carry(const struct fsm *src_fsm, const struct state_set *src_set,
env.tag = 'C';
env.dst = dst_fsm;
env.dst_state = dst_state;
env.ok = 1;
env.ok = true;

if (!fsm_isend(src_fsm, s)) {
continue;
Expand Down
15 changes: 8 additions & 7 deletions src/libfsm/epsilons.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>

#include <fsm/fsm.h>
Expand All @@ -28,9 +29,9 @@

struct remap_env {
char tag;
bool ok;
const struct fsm_alloc *alloc;
struct state_set **rmap;
int ok;

size_t count;
size_t ceil;
Expand Down Expand Up @@ -182,7 +183,7 @@ remap_capture_actions(struct fsm *nfa, struct state_set **eclosures)
struct state_set **rmap;
struct state_iter si;
fsm_state_t si_s;
struct remap_env env = { 'R', NULL, NULL, 1, 0, 0, NULL };
struct remap_env env = { 'R', true, NULL, NULL, 0, 0, NULL };
env.alloc = nfa->alloc;

/* build a reverse mapping */
Expand Down Expand Up @@ -325,17 +326,17 @@ remap_capture_action_cb(fsm_state_t state,
return 1;

fail:
env->ok = 0;
env->ok = false;
return 0;
}

struct collect_env {
char tag;
bool ok;
const struct fsm_alloc *alloc;
size_t count;
size_t ceil;
fsm_end_id_t *ids;
int ok;
};

static int
Expand All @@ -353,7 +354,7 @@ collect_cb(fsm_state_t state, fsm_end_id_t id, void *opaque)
nids = f_realloc(env->alloc, env->ids,
nceil * sizeof(*env->ids));
if (nids == NULL) {
env->ok = 0;
env->ok = false;
return 0;
}
env->ceil = nceil;
Expand Down Expand Up @@ -390,7 +391,7 @@ carry_endids(struct fsm *fsm, struct state_set *states,
if (env.ids == NULL) {
return 0;
}
env.ok = 1;
env.ok = true;

/* collect from states */
for (state_set_reset(states, &it); state_set_next(&it, &s); ) {
Expand All @@ -407,7 +408,7 @@ carry_endids(struct fsm *fsm, struct state_set *states,
/* add them */
for (i = 0; i < env.count; i++) {
if (!fsm_endid_set(fsm, dst_state, env.ids[i])) {
env.ok = 0;
env.ok = false;
goto cleanup;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/libfsm/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,24 @@ struct gen_ctx {
const struct fsm *fsm;
size_t max_length;
fsm_generate_matches_cb *cb;
void *opaque;

bool done;

size_t buf_ceil;
size_t buf_used;
char *buf;

void *opaque;

unsigned depth;
unsigned steps;

/* This is used to avoid useless cycles -- if a state
* was reached since the same match_count, then don't
* explore it again. */
unsigned match_count;
unsigned *state_counts;

unsigned depth;
unsigned steps;
bool done;

/* Shortest end distance for a state: sed[s_id] */
unsigned *sed;

Expand Down
12 changes: 6 additions & 6 deletions src/libfsm/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ struct state_array;
#define FSM_CAPTURE_MAX INT_MAX

struct fsm_edge {
fsm_state_t state; /* destination */
fsm_state_t state:24; /* destination. :24 for packing */
unsigned char symbol;
};

struct fsm_state {
struct edge_set *edges;
struct state_set *epsilons;

unsigned int end:1;

/* If 0, then this state has no need for checking
Expand All @@ -57,9 +60,6 @@ struct fsm_state {

/* meaningful within one particular transformation only */
unsigned int visited:1;

struct edge_set *edges;
struct state_set *epsilons;
};

struct fsm {
Expand All @@ -68,10 +68,10 @@ struct fsm {

size_t statealloc; /* number of elements allocated */
size_t statecount; /* number of elements populated */
size_t endcount;
size_t endcount:31; /* :31 for packing */

fsm_state_t start;
unsigned int hasstart:1;
fsm_state_t start;

struct fsm_capture_info *capture_info;
struct endid_info *endid_info;
Expand Down
6 changes: 3 additions & 3 deletions src/libfsm/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

struct copy_capture_env {
char tag;
bool ok;
struct fsm *dst;
int ok;
};

static int
Expand Down Expand Up @@ -134,7 +134,7 @@ copy_capture_cb(fsm_state_t state,

if (!fsm_capture_add_action(env->dst, state, type,
capture_id, to)) {
env->ok = 0;
env->ok = false;
return 0;
}

Expand All @@ -147,7 +147,7 @@ copy_capture_actions(struct fsm *dst, struct fsm *src)
struct copy_capture_env env;
env.tag = 'C';
env.dst = dst;
env.ok = 1;
env.ok = true;

fsm_capture_action_iter(src, copy_capture_cb, &env);

Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fsm_print(FILE *f, const struct fsm *fsm,
continue;
}

assert(ir->states[i].endids.count <= 1);
assert(ir->states[i].count <= 1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libfsm/print/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,15 @@ print_endstates(FILE *f,
fprintf(f, "\tcase S%u: ", i);

if (-1 == print_hook_accept(f, opt, hooks,
ir->states[i].endids.ids, ir->states[i].endids.count,
ir->states[i].ids, ir->states[i].count,
default_accept,
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
ir->states[i].endids.ids, ir->states[i].endids.count))
ir->states[i].ids, ir->states[i].count))
{
return -1;
}
Expand Down
10 changes: 5 additions & 5 deletions src/libfsm/print/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ make_ir(const struct fsm *fsm, const struct fsm_options *opt)
assert(i < ir->n);

ir->states[i].isend = fsm_isend(fsm, i);
ir->states[i].endids.ids = NULL;
ir->states[i].endids.count = 0;
ir->states[i].ids = NULL;
ir->states[i].count = 0;

if (fsm_isend(fsm, i)) {
fsm_end_id_t *ids;
Expand All @@ -563,8 +563,8 @@ make_ir(const struct fsm *fsm, const struct fsm_options *opt)
assert(res == 1);
}

ir->states[i].endids.ids = ids;
ir->states[i].endids.count = count;
ir->states[i].ids = ids;
ir->states[i].count = count;
}

if (make_state(fsm, i, &ir->states[i]) == -1) {
Expand Down Expand Up @@ -629,7 +629,7 @@ free_ir(const struct fsm *fsm, struct ir *ir)

for (i = 0; i < ir->n; i++) {
f_free(fsm->alloc, (void *) ir->states[i].example);
f_free(fsm->alloc, (void *) ir->states[i].endids.ids);
f_free(fsm->alloc, (void *) ir->states[i].ids);

switch (ir->states[i].strategy) {
case IR_TABLE:
Expand Down
Loading

0 comments on commit ac91f5c

Please sign in to comment.