mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-28 01:58:40 +08:00
Minor performance improvement in the variable assignment code
darcs-hash:20070118162922-ac50b-0f4bfd125e20673fdcb928e2a7aac6af50a1dec7.gz
This commit is contained in:
parent
029be823e2
commit
77a48dc252
51
env.c
51
env.c
@ -108,6 +108,8 @@ typedef struct env_node
|
|||||||
typedef struct var_entry
|
typedef struct var_entry
|
||||||
{
|
{
|
||||||
int export; /**< Whether the variable should be exported */
|
int export; /**< Whether the variable should be exported */
|
||||||
|
size_t size; /**< The maximum length (excluding the NULL) that will fit into this var_entry_t */
|
||||||
|
|
||||||
#if __STDC_VERSION__ < 199901L
|
#if __STDC_VERSION__ < 199901L
|
||||||
wchar_t val[1]; /**< The value of the variable */
|
wchar_t val[1]; /**< The value of the variable */
|
||||||
#else
|
#else
|
||||||
@ -723,14 +725,6 @@ int env_set( const wchar_t *key,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
if( val == 0 )
|
|
||||||
{
|
|
||||||
wchar_t *prev_val;
|
|
||||||
free_val = 1;
|
|
||||||
prev_val = env_get( key );
|
|
||||||
val = wcsdup( prev_val?prev_val:L"" );
|
|
||||||
}
|
|
||||||
|
|
||||||
node = env_get_node( key );
|
node = env_get_node( key );
|
||||||
if( node && &node->env != 0 )
|
if( node && &node->env != 0 )
|
||||||
{
|
{
|
||||||
@ -788,7 +782,9 @@ int env_set( const wchar_t *key,
|
|||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
New variable with unspecified scope. The default
|
New variable with unspecified scope. The default
|
||||||
scope is the innermost scope that is shadowing
|
scope is the innermost scope that is shadowing,
|
||||||
|
which will be either the current function or the
|
||||||
|
global scope.
|
||||||
*/
|
*/
|
||||||
node = top;
|
node = top;
|
||||||
while( node->next && !node->new_scope )
|
while( node->next && !node->new_scope )
|
||||||
@ -801,27 +797,50 @@ int env_set( const wchar_t *key,
|
|||||||
if( !done )
|
if( !done )
|
||||||
{
|
{
|
||||||
void *k, *v;
|
void *k, *v;
|
||||||
|
var_entry_t *old_entry;
|
||||||
|
size_t val_len = wcslen(val);
|
||||||
|
|
||||||
hash_remove( &node->env, key, &k, &v );
|
hash_remove( &node->env, key, &k, &v );
|
||||||
free( k );
|
|
||||||
|
/*
|
||||||
|
Try to reuse previous key string
|
||||||
|
*/
|
||||||
|
if( !k )
|
||||||
|
k = wcsdup(key);
|
||||||
|
|
||||||
|
old_entry = (var_entry_t *)v;
|
||||||
|
if( old_entry && old_entry->size >= val_len )
|
||||||
|
{
|
||||||
|
entry = old_entry;
|
||||||
|
|
||||||
|
if( !!(var_mode & ENV_EXPORT) || entry->export )
|
||||||
|
{
|
||||||
|
entry->export = !!(var_mode & ENV_EXPORT);
|
||||||
|
has_changed_new = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
free( v );
|
free( v );
|
||||||
|
|
||||||
entry = malloc( sizeof( var_entry_t ) +
|
entry = malloc( sizeof( var_entry_t ) +
|
||||||
sizeof(wchar_t )*(wcslen(val)+1));
|
sizeof(wchar_t )*(val_len+1));
|
||||||
|
|
||||||
if( !entry )
|
if( !entry )
|
||||||
DIE_MEM();
|
DIE_MEM();
|
||||||
|
|
||||||
|
entry->size = val_len;
|
||||||
|
|
||||||
if( var_mode & ENV_EXPORT)
|
if( var_mode & ENV_EXPORT)
|
||||||
{
|
{
|
||||||
entry->export = 1;
|
entry->export = 1;
|
||||||
has_changed_new = 1;
|
has_changed_new = 1;
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
entry->export = 0;
|
|
||||||
|
|
||||||
wcscpy( entry->val, val );
|
wcscpy( entry->val, val );
|
||||||
|
|
||||||
hash_put( &node->env, wcsdup(key), entry );
|
hash_put( &node->env, k, entry );
|
||||||
|
|
||||||
if( entry->export )
|
if( entry->export )
|
||||||
{
|
{
|
||||||
@ -1321,7 +1340,7 @@ static void export_func1( void *k, void *v, void *aux )
|
|||||||
|
|
||||||
hash_remove( h, k, 0, 0 );
|
hash_remove( h, k, 0, 0 );
|
||||||
|
|
||||||
if( val_entry->export )
|
if( val_entry->export && wcscmp( val_entry->val, ENV_NULL ) )
|
||||||
{
|
{
|
||||||
hash_put( h, k, val_entry->val );
|
hash_put( h, k, val_entry->val );
|
||||||
}
|
}
|
||||||
@ -1410,7 +1429,7 @@ char **env_export_arr( int recalc )
|
|||||||
{
|
{
|
||||||
wchar_t *key = (wchar_t *)al_get( &uni, i );
|
wchar_t *key = (wchar_t *)al_get( &uni, i );
|
||||||
wchar_t *val = env_universal_get( key );
|
wchar_t *val = env_universal_get( key );
|
||||||
if( !hash_get( &vals, key ) )
|
if( wcscmp( val, ENV_NULL) && !hash_get( &vals, key ) )
|
||||||
hash_put( &vals, key, val );
|
hash_put( &vals, key, val );
|
||||||
}
|
}
|
||||||
al_destroy( &uni );
|
al_destroy( &uni );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user