Slight local database files reading optimization
Since we know the length of the line, we can use this all the way through and do a cheaper operation than strdup() by just invoking malloc and memcpy directly. Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
0e4946d559
commit
86cb6e1f0f
|
@ -474,7 +474,8 @@ static int local_db_populate(alpm_db_t *db)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Note: the return value must be freed by the caller */
|
/* Note: the return value must be freed by the caller */
|
||||||
char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info, const char *filename)
|
char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
|
||||||
|
const char *filename)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
char *pkgpath;
|
char *pkgpath;
|
||||||
|
@ -637,10 +638,11 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
|
||||||
while(fgets(line, sizeof(line), fp)) {
|
while(fgets(line, sizeof(line), fp)) {
|
||||||
_alpm_strip_newline(line);
|
_alpm_strip_newline(line);
|
||||||
if(strcmp(line, "%FILES%") == 0) {
|
if(strcmp(line, "%FILES%") == 0) {
|
||||||
size_t files_count = 0, files_size = 0;
|
size_t files_count = 0, files_size = 0, len;
|
||||||
alpm_file_t *files = NULL;
|
alpm_file_t *files = NULL;
|
||||||
|
|
||||||
while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line)) {
|
while(fgets(line, sizeof(line), fp) &&
|
||||||
|
(len = _alpm_strip_newline(line))) {
|
||||||
if(files_count >= files_size) {
|
if(files_count >= files_size) {
|
||||||
size_t old_size = files_size;
|
size_t old_size = files_size;
|
||||||
if(files_size == 0) {
|
if(files_size == 0) {
|
||||||
|
@ -658,8 +660,14 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
|
||||||
memset(files + old_size, 0,
|
memset(files + old_size, 0,
|
||||||
sizeof(alpm_file_t) * (files_size - old_size));
|
sizeof(alpm_file_t) * (files_size - old_size));
|
||||||
}
|
}
|
||||||
STRDUP(files[files_count].name, line, goto error);
|
/* since we know the length of the file string already,
|
||||||
/* TODO: lstat file, get mode/size */
|
* we can do malloc + memcpy rather than strdup */
|
||||||
|
files[files_count].name = malloc(len + 1);
|
||||||
|
if(files[files_count].name == NULL) {
|
||||||
|
ALLOC_FAIL(len);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
memcpy(files[files_count].name, line, len + 1);
|
||||||
files_count++;
|
files_count++;
|
||||||
}
|
}
|
||||||
/* attempt to hand back any memory we don't need */
|
/* attempt to hand back any memory we don't need */
|
||||||
|
|
Loading…
Reference in New Issue