Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
7.5.1 : somewhat improve c loader performance
Browse files Browse the repository at this point in the history
Signed-off-by: Ari Archer <[email protected]>
  • Loading branch information
Ari Archer committed Mar 6, 2023
1 parent 92917b0 commit c77ed65
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 43 deletions.
2 changes: 1 addition & 1 deletion baz
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[ "$BAZ_DEBUG" ] && set -x

export BAZ_VERSION='7.5.0'
export BAZ_VERSION='7.5.1'
export BAZ_DIR="$HOME/.local/share/baz"
export BAZ_CONFDIR="$HOME/.config/baz"
export BAZ_CONF="$BAZ_CONFDIR/config.env"
Expand Down
3 changes: 2 additions & 1 deletion loader/config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef _CONFIG_H
#define _CONFIG_H
static const volatile char newline = '\n';
static const char newline = '\n';
#define DIR_SEP "/"
#define CDIR_SEP '/'
#define ENVS_DIR "environments"
#define CMDS_DIR "commands"
#define FUNCS_DIR "functions"
Expand Down
10 changes: 5 additions & 5 deletions loader/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static char *read_line(const char *);
static void free_file(File *f) { free(f->content); }

static void alloc_file(const char *path, File *f) {
static volatile int fd;
static volatile size_t content_size;
static int fd;
static size_t content_size;

if ((fd = open(path, O_RDONLY)) == -1) {
f->content_size = 0;
Expand Down Expand Up @@ -46,9 +46,9 @@ static void alloc_file(const char *path, File *f) {
}

static char *read_line(const char *path) {
static volatile int fd;
static volatile ssize_t sz = 0, rb;
char *buf = NULL;
static int fd;
static ssize_t sz = 0, rb;
char *buf = NULL;

if ((fd = open(path, O_RDONLY)) == -1)
return NULL;
Expand Down
45 changes: 14 additions & 31 deletions loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static unsigned char debug_load;
typedef void (*stage_t)(char *);

static void load_envs(char *path) {
static volatile size_t envs_base;
static size_t envs_base;

struct dirent *ep;
DIR *dp;
Expand All @@ -65,8 +65,6 @@ static void load_envs(char *path) {
if (f.content_size == 0)
continue;

log(path);

if (f.content[f.content_size - 1] == '\n')
f.content[f.content_size - 1] = '\0';

Expand All @@ -83,7 +81,7 @@ static void load_envs(char *path) {
}

static void load_cmds(char *path) {
static volatile size_t cmds_base;
static size_t cmds_base;

struct dirent *ep;
DIR *dp;
Expand All @@ -106,7 +104,7 @@ static void load_cmds(char *path) {
}

static void load_functions(char *path) {
static volatile size_t funs_base;
static size_t funs_base;

struct dirent *ep;
DIR *dp;
Expand All @@ -129,7 +127,6 @@ static void load_functions(char *path) {
if (f.content_size == 0)
continue;

log(path);
printf("%s(){\n%s\n}\n", ep->d_name, f.content);

path[funs_base] = '\0';
Expand All @@ -140,7 +137,7 @@ static void load_functions(char *path) {
}

static void load_aliases(char *path) {
static volatile size_t alias_base;
static size_t alias_base;

struct dirent *ep;
DIR *dp;
Expand All @@ -162,8 +159,6 @@ static void load_aliases(char *path) {
while ((ep = readdir_visible(dp))) {
strcat(path, ep->d_name);

log(path);

alloc_file(path, pf);
escape_quotes(ps, f.content, f.content_size);

Expand All @@ -178,7 +173,7 @@ static void load_aliases(char *path) {
}

static void load_runners(char *path) {
static volatile size_t runs_base;
static size_t runs_base;

struct dirent *ep;
DIR *dp;
Expand All @@ -201,7 +196,6 @@ static void load_runners(char *path) {
if (f.content_size == 0)
continue;

log(path);
puts(f.content);

path[runs_base] = '\0';
Expand All @@ -212,13 +206,11 @@ static void load_runners(char *path) {
}

static void load_completions(char *path) {
static volatile size_t comps_base;
static size_t comps_base;

struct dirent *ep;
DIR *dp;
char *basename;

char *line;
char *basename, *line;

log("loading completions");

Expand All @@ -231,8 +223,6 @@ static void load_completions(char *path) {
while ((ep = readdir_visible(dp))) {
strcat(path, ep->d_name);

log(path);

if ((line = read_line(path)) != NULL) {
printf("complete -F %s bashdefault -o default %s\n", line,
(basename = get_base(path)));
Expand All @@ -248,7 +238,7 @@ static void load_completions(char *path) {
}

static void load_keybinds(char *path) {
static volatile size_t keys_base;
static size_t keys_base;

struct dirent *ep;
DIR *dp;
Expand Down Expand Up @@ -285,8 +275,6 @@ static void load_keybinds(char *path) {
while ((ep = readdir_visible(dp))) {
strcat(path, ep->d_name);

log(path);

printf("bind -m %s -f %s\n", (basename = get_base(path)), path);
free(basename);

Expand All @@ -304,20 +292,18 @@ static const stage_t stages[] = {
static const size_t stages_sz = sizeof(stages) / sizeof(stages[0]);

int main(int argc, char **argv) {
static volatile size_t path_base;
static volatile unsigned char stage;
char *path;
static size_t path_base;
static unsigned char stage;
static char *path;

if (argc < 2)
return 1;

path =
#ifdef ALLOW_ALLOCA
alloca(PATH_MAX)
path = alloca(PATH_MAX);
#else
malloc(PATH_MAX)
path = malloc(PATH_MAX);
#endif
;

#ifdef LOGGING
if ((debug_load = getenv(DEBUG_LOAD) != NULL))
Expand All @@ -328,10 +314,7 @@ int main(int argc, char **argv) {

while (*++argv) {
log("");

path_base = strlen(*argv);
strcpy(path, *argv);

memmove(path, *argv, (path_base = strlen(*argv)));
log(path);

for (stage = 0; stage < stages_sz; ++stage) {
Expand Down
7 changes: 4 additions & 3 deletions loader/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ static struct dirent *readdir_visible(DIR *);

#ifdef PATH_IMPL
static char *get_base(const char *path) {
char *last_slash = strrchr(path, '/');
char *buf = malloc(strlen(last_slash));
const char *last_slash = strrchr(path, CDIR_SEP) + 1;
const size_t last_slash_sz = strlen(last_slash);
char *buf = malloc(last_slash_sz);

memmove(buf, last_slash + 1, strlen(last_slash));
memmove(buf, last_slash, last_slash_sz);
return buf;
}

Expand Down
6 changes: 4 additions & 2 deletions loader/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ static void str_free(Str *);

#ifdef STR_IMPL
static void str_append(Str *str, const char chr) {
if (str->idx == str->len)
str->string = realloc(str->string, (str->len += STR_GROWTH));
if (str->idx == str->len) {
str->len += STR_GROWTH;
str->string = realloc(str->string, str->len);
}

str->string[str->idx++] = chr;
}
Expand Down

0 comments on commit c77ed65

Please sign in to comment.