Skip to content

Commit

Permalink
Merge pull request #491 from katef/kate/happy-cache-lines
Browse files Browse the repository at this point in the history
Happier cache lines
  • Loading branch information
katef authored Sep 5, 2024
2 parents dc9721f + f1ca8e8 commit 81b14f8
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 84 deletions.
3 changes: 2 additions & 1 deletion include/adt/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ struct fsm_state;

struct path {
fsm_state_t state;
struct path *next;
char c;

struct path *next;
};

struct path *
Expand Down
12 changes: 6 additions & 6 deletions include/fsm/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ struct fsm_options {
*/
unsigned int group_edges:1;

/* for generated code, what kind of I/O API to generate */
enum fsm_io io;

/* for generated code, how to handle multiple endids on an accepting state */
enum fsm_ambig ambig;

/* a prefix for namespacing generated identifiers. NULL if not required. */
const char *prefix;

/* the name of the enclosing package; NULL to use `prefix` (default). */
const char *package_prefix;

/* for generated code, what kind of I/O API to generate */
enum fsm_io io;

/* for generated code, how to handle multiple endids on an accepting state */
enum fsm_ambig ambig;
};

#endif
Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -121,7 +121,7 @@ dump_edge_set(const struct edge_set *set)
static struct edge_set *
init_empty(const struct fsm_alloc *alloc)
{
struct edge_set *set = f_calloc(alloc, 1, sizeof(*set));
struct edge_set *set = f_malloc(alloc, sizeof(*set));
if (set == NULL) {
return NULL;
}
Expand Down Expand Up @@ -719,7 +719,7 @@ edge_set_copy(struct edge_set **dst, const struct fsm_alloc *alloc,
return 1;
}

set = f_calloc(alloc, 1, sizeof(*set));
set = f_malloc(alloc, sizeof(*set));
if (set == NULL) {
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/adt/idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ grow_bucket_values(struct idmap *m, unsigned old_words, unsigned new_words)
return 0;
}

// TODO: memcpy
for (size_t w_i = 0; w_i < old_words; w_i++) {
nv[w_i] = b->values[w_i];
}
Expand Down
6 changes: 3 additions & 3 deletions src/adt/internedstateset.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ interned_state_set_pool_alloc(const struct fsm_alloc *a)
fsm_state_t *buf = NULL;
uint32_t *buckets = NULL;

res = f_calloc(a, 1, sizeof(*res));
res = f_malloc(a, sizeof(*res));
if (res == NULL) { goto cleanup; }

sets = f_malloc(a, DEF_SETS * sizeof(sets[0]));
Expand All @@ -93,7 +93,7 @@ interned_state_set_pool_alloc(const struct fsm_alloc *a)
buckets[i] = NO_ID;
}

struct interned_state_set_pool p = {
*res = (struct interned_state_set_pool) {
.alloc = a,
.sets = {
.ceil = DEF_SETS,
Expand All @@ -108,7 +108,7 @@ interned_state_set_pool_alloc(const struct fsm_alloc *a)
.buckets = buckets,
},
};
memcpy(res, &p, sizeof(p));

return res;

cleanup:
Expand Down
4 changes: 3 additions & 1 deletion src/adt/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ queue_new(const struct fsm_alloc *a, size_t max_capacity)
if (max_capacity == 0) { return NULL; }
alloc_size = sizeof(*q) + (max_capacity - 1) * sizeof(q->q[0]);

q = f_calloc(a, 1, alloc_size);
q = f_malloc(a, alloc_size);
if (q == NULL) { return NULL; }

q->alloc = a;
q->capacity = max_capacity;
q->rd = 0;
q->wr = 0;

return q;
}
Expand Down
9 changes: 7 additions & 2 deletions src/libfsm/capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ fsm_capture_init(struct fsm *fsm)
struct fsm_capture_info *ci = NULL;
size_t i;

ci = f_calloc(fsm->alloc,
1, sizeof(*ci));
ci = f_malloc(fsm->alloc, sizeof(*ci));
if (ci == NULL) {
goto cleanup;
}

ci->max_capture_id = 0;
ci->bucket_count = 0;
ci->buckets_used = 0;
ci->buckets = NULL;

fsm->capture_info = ci;

for (i = 0; i < fsm->statealloc; i++) {
Expand Down
17 changes: 11 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 @@ -117,10 +118,12 @@ copy_capture_actions(struct fsm *dst, const struct fsm *src)
}

struct copy_end_ids_env {
#ifndef NDEBUG
char tag;
#endif
struct fsm *dst;
const struct fsm *src;
int ok;
bool ok;
};

static int
Expand All @@ -134,7 +137,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 @@ -145,10 +148,12 @@ static int
copy_end_ids(struct fsm *dst, const struct fsm *src)
{
struct copy_end_ids_env env;
#ifndef NDEBUG
env.tag = 'c'; /* for clone */
#endif
env.dst = dst;
env.src = src;
env.ok = 1;
env.ok = true;

fsm_endid_iter(src, copy_end_ids_cb, &env);

Expand Down
13 changes: 10 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 All @@ -35,11 +36,13 @@ struct mapping_closure {
};

struct consolidate_copy_capture_actions_env {
#ifndef NDEBUG
char tag;
#endif
struct fsm *dst;
size_t mapping_count;
const fsm_state_t *mapping;
int ok;
bool ok;
};

static int
Expand Down Expand Up @@ -186,7 +189,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 @@ -200,11 +203,13 @@ consolidate_copy_capture_actions(struct fsm *dst, const struct fsm *src,
size_t i;

struct consolidate_copy_capture_actions_env env;
#ifndef NDEBUG
env.tag = 'C';
#endif
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 Expand Up @@ -248,7 +253,9 @@ consolidate_end_ids(struct fsm *dst, const struct fsm *src,
struct consolidate_end_ids_env env;
int ret;

#ifndef NDEBUG
env.tag = 'C'; /* for Consolidate */
#endif
env.dst = dst;
env.src = src;
env.mapping = mapping;
Expand Down
7 changes: 6 additions & 1 deletion src/libfsm/determinise.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,12 @@ static int
det_copy_capture_actions(struct reverse_mapping *reverse_mappings,
struct fsm *dst, struct fsm *src)
{
struct det_copy_capture_actions_env env = { 'D', NULL, NULL, 1 };
struct det_copy_capture_actions_env env = {
#ifndef NDEBUG
'D',
#endif
NULL, NULL, 1
};
env.dst = dst;
env.reverse_mappings = reverse_mappings;

Expand Down
5 changes: 4 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 @@ -82,10 +83,12 @@ struct reverse_mapping {
};

struct det_copy_capture_actions_env {
#ifndef NDEBUG
char tag;
#endif
struct fsm *dst;
struct reverse_mapping *reverse_mappings;
int ok;
bool ok;
};

#define MAPPINGSTACK_DEF_CEIL 16
Expand Down
13 changes: 9 additions & 4 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 @@ -88,7 +89,7 @@ fsm_endid_init(struct fsm *fsm)
{
struct endid_info_bucket *buckets = NULL;
size_t i;
struct endid_info *res = f_calloc(fsm->alloc, 1, sizeof(*res));
struct endid_info *res = f_malloc(fsm->alloc, sizeof(*res));
if (res == NULL) {
return 0;
}
Expand Down Expand Up @@ -752,10 +753,12 @@ fsm_endid_get(const struct fsm *fsm, fsm_state_t end_state,
}

struct carry_env {
#ifndef NDEBUG
char tag;
#endif
struct fsm *dst;
fsm_state_t dst_state;
int ok;
bool ok;
};

static int
Expand All @@ -767,7 +770,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 @@ -795,10 +798,12 @@ fsm_endid_carry(const struct fsm *src_fsm, const struct state_set *src_set,

for (state_set_reset(src_set, &it); state_set_next(&it, &s); ) {
struct carry_env env;
#ifndef NDEBUG
env.tag = 'C';
#endif
env.dst = dst_fsm;
env.dst_state = dst_state;
env.ok = 1;
env.ok = true;

if (!fsm_isend(src_fsm, s)) {
continue;
Expand Down
Loading

0 comments on commit 81b14f8

Please sign in to comment.