Make dmenu reading very marginally faster

A slight reduction in use of realloc and avoidance of 3 or 4 strlens for
a string we know the length of
This commit is contained in:
Tom Hinton 2015-10-01 11:41:44 +01:00
parent 0e60aaf235
commit 574bf2da82
3 changed files with 23 additions and 11 deletions

View File

@ -16,7 +16,7 @@ int helper_parse_setup ( char * string, char ***output, int *length, ... );
/** /**
* Implementation of fgets with custom separator. * Implementation of fgets with custom separator.
*/ */
char* fgets_s ( char* s, int n, FILE *iop, char sep ); char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep );
/** /**
* @param token The string for which we want a collation key. * @param token The string for which we want a collation key.

View File

@ -59,28 +59,40 @@ typedef struct _DmenuModePrivateData
static char **get_dmenu ( unsigned int *length ) static char **get_dmenu ( unsigned int *length )
{ {
char buffer[1024]; const unsigned int buf_size = 1024;
char buffer[buf_size];
char **retv = NULL; char **retv = NULL;
char *buffer_end = NULL;
unsigned int rvlength = 1;
*length = 0; *length = 0;
while ( fgets_s ( buffer, 1024, stdin, (char) config.separator ) != NULL ) { while ( ( buffer_end = fgets_s ( buffer, buf_size, stdin, (char) config.separator ) ) != NULL ) {
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); if (rvlength < (*length + 2)) {
retv[( *length )] = g_strdup ( buffer ); rvlength *= 2;
retv[( *length ) + 1] = NULL; retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) );
}
size_t blength = buffer_end - &(buffer[0]);
char *copy = g_malloc( blength + 1 );
memcpy(copy, buffer, blength);
// Filter out line-end. // Filter out line-end.
if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) { if ( copy[blength] == '\n' ) {
retv[( *length )][strlen ( buffer ) - 1] = '\0'; copy[blength] = '\0';
} }
retv[( *length )] = copy;
retv[( *length ) + 1] = NULL;
( *length )++; ( *length )++;
// Stop when we hit 2³¹ entries. // Stop when we hit 2³¹ entries.
if ( ( *length ) == INT_MAX ) { if ( ( *length ) == INT_MAX ) {
return retv; return retv;
} }
} }
retv = g_realloc ( retv, ( *length + 1 ) * sizeof ( char* ) );
return retv; return retv;
} }

View File

@ -52,7 +52,7 @@ void cmd_set_arguments ( int argc, char **argv )
/** /**
* `fgets` implementation with custom separator. * `fgets` implementation with custom separator.
*/ */
char* fgets_s ( char* s, int n, FILE *iop, char sep ) char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep )
{ {
// Map these to registers. // Map these to registers.
register int c = EOF; register int c = EOF;
@ -72,7 +72,7 @@ char* fgets_s ( char* s, int n, FILE *iop, char sep )
*cs = '\0'; *cs = '\0';
// if last read was end of file and current index is start, we are done: // if last read was end of file and current index is start, we are done:
// Return NULL. // Return NULL.
return ( c == EOF && cs == s ) ? NULL : s; return ( c == EOF && cs == s ) ? NULL : cs;
} }
/** /**