mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-23 06:38:05 +01:00
WINGs: optimisations to 'WMReadPropListFromPipe'
The function did spend its time re-copying data and searching the end of the read buffer, which is not really efficient. The new code reads directly from file to the end of previous data, avoiding unneccesary duplication, and keeps track of the end of data to avoid searching it for next read. Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
committed by
Carlos R. Mafra
parent
654dcfeb28
commit
af059d408b
@@ -1566,8 +1566,10 @@ WMPropList *WMReadPropListFromPipe(const char *command)
|
|||||||
FILE *file;
|
FILE *file;
|
||||||
WMPropList *plist;
|
WMPropList *plist;
|
||||||
PLData *pldata;
|
PLData *pldata;
|
||||||
char line[1024];
|
char *read_buf, *read_ptr;
|
||||||
char *read_buf;
|
size_t remain_size, line_size;
|
||||||
|
const size_t block_read_size = 4096;
|
||||||
|
const size_t block_read_margin = 512;
|
||||||
|
|
||||||
file = popen(command, "r");
|
file = popen(command, "r");
|
||||||
|
|
||||||
@@ -1576,28 +1578,33 @@ WMPropList *WMReadPropListFromPipe(const char *command)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pldata = (PLData *) wmalloc(sizeof(PLData));
|
|
||||||
pldata->ptr = NULL;
|
|
||||||
pldata->filename = command;
|
|
||||||
pldata->lineNumber = 1;
|
|
||||||
|
|
||||||
/* read from file till EOF or OOM and fill proplist buffer*/
|
/* read from file till EOF or OOM and fill proplist buffer*/
|
||||||
read_buf = NULL;
|
remain_size = block_read_size;
|
||||||
while (fgets(line, sizeof(line), file) != NULL) {
|
read_buf = wmalloc(remain_size);
|
||||||
if (read_buf == NULL) {
|
read_ptr = read_buf;
|
||||||
read_buf = wmalloc(strlen(line)+1);
|
while (fgets(read_ptr, remain_size, file) != NULL) {
|
||||||
read_buf[0] = '\0';
|
line_size = strlen(read_ptr);
|
||||||
} else {
|
|
||||||
read_buf = wrealloc(read_buf,
|
|
||||||
strlen(line) + strlen(read_buf) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
read_buf = strncat(read_buf, line, strlen(line));
|
remain_size -= line_size;
|
||||||
|
read_ptr += line_size;
|
||||||
|
|
||||||
|
if (remain_size < block_read_margin) {
|
||||||
|
size_t read_length;
|
||||||
|
|
||||||
|
read_length = read_ptr - read_buf;
|
||||||
|
read_buf = wrealloc(read_buf, read_length + block_read_size);
|
||||||
|
read_ptr = read_buf + read_length;
|
||||||
|
remain_size = block_read_size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pldata->ptr = read_buf;
|
|
||||||
|
|
||||||
pclose(file);
|
pclose(file);
|
||||||
|
|
||||||
|
pldata = (PLData *) wmalloc(sizeof(PLData));
|
||||||
|
pldata->ptr = read_buf;
|
||||||
|
pldata->filename = command;
|
||||||
|
pldata->lineNumber = 1;
|
||||||
|
|
||||||
plist = getPropList(pldata);
|
plist = getPropList(pldata);
|
||||||
|
|
||||||
if (getNonSpaceChar(pldata) != 0 && plist) {
|
if (getNonSpaceChar(pldata) != 0 && plist) {
|
||||||
|
|||||||
Reference in New Issue
Block a user