streamfiles: improve performance on name copy

This commit is contained in:
bnnm 2021-09-16 00:18:52 +02:00
parent 3b0bbb25bf
commit 391bfe9d4d
2 changed files with 40 additions and 15 deletions

View File

@ -24,7 +24,8 @@ typedef struct {
bool m_file_opened; /* if foobar IO service opened the file */ bool m_file_opened; /* if foobar IO service opened the file */
service_ptr_t<file> m_file; /* foobar IO service */ service_ptr_t<file> m_file; /* foobar IO service */
abort_callback* p_abort; /* foobar error stuff */ abort_callback* p_abort; /* foobar error stuff */
char* name; /* IO filename */ /*const*/ char* name; /* IO filename */
int name_len; /* cache */
offv_t offset; /* last read offset (info) */ offv_t offset; /* last read offset (info) */
offv_t buf_offset; /* current buffer data start */ offv_t buf_offset; /* current buffer data start */
uint8_t* buf; /* data buffer */ uint8_t* buf; /* data buffer */
@ -117,14 +118,21 @@ static offv_t foo_get_offset(FOO_STREAMFILE* sf) {
return sf->offset; return sf->offset;
} }
static void foo_get_name(FOO_STREAMFILE* sf, char* name, size_t name_size) { static void foo_get_name(FOO_STREAMFILE* sf, char* name, size_t name_size) {
/* Most crap only cares about the filename itself */ int copy_size = sf->name_len + 1;
size_t ourlen = strlen(sf->name); if (copy_size > name_size)
if (ourlen > name_size) { copy_size = name_size;
if (name_size) strcpy(name, sf->name + ourlen - name_size + 1);
} memcpy(name, sf->name, copy_size);
else { name[copy_size - 1] = '\0';
/*
//TODO: check again (looks like a truncate-from-the-end copy, probably a useless remnant of olden times)
if (sf->name_len > name_size) {
strcpy(name, sf->name + sf->name_len - name_size + 1);
} else {
strcpy(name, sf->name); strcpy(name, sf->name);
} }
*/
} }
static void foo_close(FOO_STREAMFILE* sf) { static void foo_close(FOO_STREAMFILE* sf) {
sf->m_file.release(); //release alloc'ed ptr sf->m_file.release(); //release alloc'ed ptr
@ -178,8 +186,11 @@ static STREAMFILE* open_foo_streamfile_buffer_by_file(service_ptr_t<file> m_file
this_sf->buf_size = buf_size; this_sf->buf_size = buf_size;
this_sf->buf = buf; this_sf->buf = buf;
//TODO: foobar filenames look like "file://C:\path\to\file.adx"
// maybe should hide the internal protocol and restore on open?
this_sf->name = strdup(filename); this_sf->name = strdup(filename);
if (!this_sf->name) goto fail; if (!this_sf->name) goto fail;
this_sf->name_len = strlen(this_sf->name);
/* cache file_size */ /* cache file_size */
if (this_sf->m_file_opened) if (this_sf->m_file_opened)

View File

@ -61,6 +61,7 @@ typedef struct {
FILE* infile; /* actual FILE */ FILE* infile; /* actual FILE */
char name[PATH_LIMIT]; /* FILE filename */ char name[PATH_LIMIT]; /* FILE filename */
int name_len; /* cache */
offv_t offset; /* last read offset (info) */ offv_t offset; /* last read offset (info) */
offv_t buf_offset; /* current buffer data start */ offv_t buf_offset; /* current buffer data start */
uint8_t* buf; /* data buffer */ uint8_t* buf; /* data buffer */
@ -168,8 +169,12 @@ static offv_t stdio_get_offset(STDIO_STREAMFILE* sf) {
return sf->offset; return sf->offset;
} }
static void stdio_get_name(STDIO_STREAMFILE* sf, char* name, size_t name_size) { static void stdio_get_name(STDIO_STREAMFILE* sf, char* name, size_t name_size) {
strncpy(name, sf->name, name_size); int copy_size = sf->name_len + 1;
name[name_size - 1] = '\0'; if (copy_size > name_size)
copy_size = name_size;
memcpy(name, sf->name, copy_size);
name[copy_size - 1] = '\0';
} }
static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename, size_t buf_size) { static STREAMFILE* stdio_open(STDIO_STREAMFILE* sf, const char* const filename, size_t buf_size) {
@ -232,8 +237,11 @@ static STREAMFILE* open_stdio_streamfile_buffer_by_file(FILE* infile, const char
this_sf->buf_size = buf_size; this_sf->buf_size = buf_size;
this_sf->buf = buf; this_sf->buf = buf;
strncpy(this_sf->name, filename, sizeof(this_sf->name)); this_sf->name_len = strlen(filename);
this_sf->name[sizeof(this_sf->name)-1] = '\0'; if (this_sf->name_len >= sizeof(this_sf->name))
goto fail;
memcpy(this_sf->name, filename, this_sf->name_len);
this_sf->name[this_sf->name_len] = '\0';
/* cache file_size */ /* cache file_size */
if (infile) { if (infile) {
@ -689,6 +697,7 @@ typedef struct {
STREAMFILE* inner_sf; STREAMFILE* inner_sf;
char fakename[PATH_LIMIT]; char fakename[PATH_LIMIT];
int fakename_len;
} FAKENAME_STREAMFILE; } FAKENAME_STREAMFILE;
static size_t fakename_read(FAKENAME_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) { static size_t fakename_read(FAKENAME_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
@ -701,8 +710,11 @@ static offv_t fakename_get_offset(FAKENAME_STREAMFILE* sf) {
return sf->inner_sf->get_offset(sf->inner_sf); /* default */ return sf->inner_sf->get_offset(sf->inner_sf); /* default */
} }
static void fakename_get_name(FAKENAME_STREAMFILE* sf, char* name, size_t name_size) { static void fakename_get_name(FAKENAME_STREAMFILE* sf, char* name, size_t name_size) {
strncpy(name,sf->fakename, name_size); int copy_size = sf->fakename_len + 1;
name[name_size - 1] = '\0'; if (copy_size > name_size)
copy_size = name_size;
memcpy(name, sf->fakename, copy_size);
name[copy_size - 1] = '\0';
} }
static STREAMFILE* fakename_open(FAKENAME_STREAMFILE* sf, const char* const filename, size_t buf_size) { static STREAMFILE* fakename_open(FAKENAME_STREAMFILE* sf, const char* const filename, size_t buf_size) {
@ -760,6 +772,8 @@ STREAMFILE* open_fakename_streamfile(STREAMFILE* sf, const char* fakename, const
strcat(this_sf->fakename, fakeext); strcat(this_sf->fakename, fakeext);
} }
this_sf->fakename_len = strlen(this_sf->fakename);
return &this_sf->vt; return &this_sf->vt;
} }
STREAMFILE* open_fakename_streamfile_f(STREAMFILE* sf, const char* fakename, const char* fakeext) { STREAMFILE* open_fakename_streamfile_f(STREAMFILE* sf, const char* fakename, const char* fakeext) {