Cache file_exists when iterating over the source files ##bin

* Speedup large loading times for files with DWARF info
* Only speedups the cases where the files doesnt exist
* Need to refactor file_slurp_random_line() into str for better times
This commit is contained in:
Sergi Àlvarez i Capilla 2022-03-08 12:44:29 +01:00 committed by pancake
parent 97f0bf300d
commit 035e68e3f1
2 changed files with 49 additions and 29 deletions

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2009-2021 - pancake */
/* radare2 - LGPL - Copyright 2009-2022 - pancake */
#include "r_anal.h"
#include "r_bin.h"
@ -10,6 +10,11 @@
char *getcommapath(RCore *core);
static R_TH_LOCAL ut64 filter_offset = UT64_MAX;
static R_TH_LOCAL int filter_format = 0;
static R_TH_LOCAL size_t filter_count = 0;
static R_TH_LOCAL Sdb *fscache = NULL;
static const char *help_msg_C[] = {
"Usage:", "C[-LCvsdfm*?][*?] [...]", " # Metadata management",
"C", "", "list meta info in human friendly form",
@ -202,10 +207,6 @@ static int print_meta_fileline(RCore *core, const char *file_line) {
}
#endif
static ut64 filter_offset = UT64_MAX;
static int filter_format = 0;
static size_t filter_count = 0;
static bool print_addrinfo_json(void *user, const char *k, const char *v) {
ut64 offset = sdb_atoi (k);
if (!offset || offset == UT64_MAX) {
@ -233,18 +234,29 @@ static bool print_addrinfo_json(void *user, const char *k, const char *v) {
int line = atoi (colonpos + 1);
ut64 addr = offset;
PJ *pj = (PJ*)user;
pj_o (pj);
pj_ks (pj, "file", file);
pj_kn (pj, "line", line);
pj_kn (pj, "addr", addr);
if (r_file_exists (file)) {
char *row = r_file_slurp_line (file, line, 0);
pj_ks (pj, "text", file);
free (row);
} else {
// eprintf ("Cannot open '%s'\n", file);
if (pj) {
pj_o (pj);
pj_ks (pj, "file", file);
pj_kn (pj, "line", line);
pj_kn (pj, "addr", addr);
const char *cached_existance = sdb_const_get (fscache, file, NULL);
bool file_exists = false;
if (cached_existance) {
file_exists = !strcmp (cached_existance, "1");
} else {
if (r_file_exists (file)) {
sdb_set (fscache, file, "1", 0);
} else {
sdb_set (fscache, file, "0", 0);
}
}
if (file_exists) {
char *row = r_file_slurp_line (file, line, 0);
pj_ks (pj, "text", file);
free (row);
}
pj_end (pj);
}
pj_end (pj);
free (subst);
return true;
}
@ -384,23 +396,25 @@ static int cmd_meta_lineinfo(RCore *core, const char *input) {
// taken from r2 // TODO: we should move this addrinfo sdb logic into RBin.. use HT
filter_offset = offset;
filter_count = 0;
fscache = sdb_new0 ();
PJ *pj = NULL;
if (use_json) {
PJ *pj = r_core_pj_new (core);
pj = r_core_pj_new (core);
pj_a (pj);
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo_json, pj);
if (filter_count == 0) {
print_meta_offset (core, offset, pj);
}
} else {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL);
}
if (filter_count == 0) {
print_meta_offset (core, offset, pj);
}
if (use_json) {
pj_end (pj);
char *s = pj_drain (pj);
r_cons_printf ("%s\n", s);
free (s);
} else {
sdb_foreach (core->bin->cur->sdb_addrinfo, print_addrinfo, NULL);
if (filter_count == 0) {
print_meta_offset (core, offset, NULL);
}
}
sdb_free (fscache);
}
free (pheap);
return 0;

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2021 - pancake */
/* radare - LGPL - Copyright 2007-2022 - pancake */
#include "r_types.h"
#include "r_util.h"
@ -43,9 +43,9 @@ static int file_stat(const char *file, struct stat* const pStat) {
int ret = _wstat (wfile, pStat);
free (wfile);
return ret;
#else // __WINDOWS__
#else
return stat (file, pStat);
#endif // __WINDOWS__
#endif
}
// r_file_new("", "bin", NULL) -> /bin
@ -195,14 +195,20 @@ R_API bool r_file_fexists(const char *fmt, ...) {
}
R_API bool r_file_exists(const char *str) {
char *absfile = r_file_abspath (str);
struct stat buf = {0};
#if 1
if (file_stat (str, &buf) == -1) {
return false;
}
#else
char *absfile = r_file_abspath (str);
r_return_val_if_fail (!R_STR_ISEMPTY (str), false);
if (file_stat (absfile, &buf) == -1) {
free (absfile);
return false;
}
free (absfile);
#endif
return S_IFREG == (S_IFREG & buf.st_mode);
}