diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-12-31 02:33:43 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-12-31 02:33:43 +0000 |
commit | 3893cb63e034180eab0764ee18567a56e8307a80 (patch) | |
tree | 0792c6ba3348c3837de97cb016ec790680ad4a9b /src | |
parent | bbe8ee9de8bfbd490336d80e76148663dcbe027f (diff) | |
download | libbu++-3893cb63e034180eab0764ee18567a56e8307a80.tar.gz libbu++-3893cb63e034180eab0764ee18567a56e8307a80.tar.bz2 libbu++-3893cb63e034180eab0764ee18567a56e8307a80.tar.xz libbu++-3893cb63e034180eab0764ee18567a56e8307a80.zip |
Wow, that was a freaky bug. Turned out to not have anything to do with the
size of the table, it had to do with using non pointer types for the key (some
more complex types worked as well, probably because of lazy memory collection)
and then using the [] indexing operators. You wound up with pointers to local
variables that didn't exist by the end of the assignemnt operator.
Strange, but I didn't actually use references inside of all of the Bu::Hash
accessor functions, that means in cases where more complex variables are used
as keys (like Bu::FString) it was making several copies of them per operation
and destroying them all immediately. Now it will be even faster and use much
less memory.
Good catch, David.
Diffstat (limited to '')
-rw-r--r-- | src/hash.h | 26 | ||||
-rw-r--r-- | src/unit/hash.cpp | 4 |
2 files changed, 15 insertions, 15 deletions
@@ -55,7 +55,7 @@ namespace Bu | |||
55 | { | 55 | { |
56 | friend class Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc>; | 56 | friend class Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc>; |
57 | private: | 57 | private: |
58 | HashProxy( Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc> &h, key *k, uint32_t nPos, uint32_t hash ) : | 58 | HashProxy( Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc> &h, const key *k, uint32_t nPos, uint32_t hash ) : |
59 | hsh( h ), | 59 | hsh( h ), |
60 | pKey( k ), | 60 | pKey( k ), |
61 | nPos( nPos ), | 61 | nPos( nPos ), |
@@ -73,7 +73,7 @@ namespace Bu | |||
73 | } | 73 | } |
74 | 74 | ||
75 | Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc> &hsh; | 75 | Hash<key, _value, sizecalc, keyalloc, valuealloc, challoc> &hsh; |
76 | key *pKey; | 76 | const key *pKey; |
77 | uint32_t nPos; | 77 | uint32_t nPos; |
78 | _value *pValue; | 78 | _value *pValue; |
79 | uint32_t hash; | 79 | uint32_t hash; |
@@ -151,8 +151,8 @@ namespace Bu | |||
151 | { | 151 | { |
152 | if( bFilled ) | 152 | if( bFilled ) |
153 | { | 153 | { |
154 | hsh.va.destroy( pValue ); | 154 | hsh.va.destroy( &hsh.aValues[nPos] ); |
155 | hsh.va.construct( pValue, nval ); | 155 | hsh.va.construct( &hsh.aValues[nPos], nval ); |
156 | hsh.onUpdate(); | 156 | hsh.onUpdate(); |
157 | } | 157 | } |
158 | else | 158 | else |
@@ -380,7 +380,7 @@ namespace Bu | |||
380 | *@param k (key_type) Key of data to be retrieved. | 380 | *@param k (key_type) Key of data to be retrieved. |
381 | *@returns (HashProxy) Proxy pointing to the data. | 381 | *@returns (HashProxy) Proxy pointing to the data. |
382 | */ | 382 | */ |
383 | virtual HashProxy<key, value, sizecalc, keyalloc, valuealloc, challoc> operator[]( key k ) | 383 | virtual HashProxy<key, value, sizecalc, keyalloc, valuealloc, challoc> operator[]( const key &k ) |
384 | { | 384 | { |
385 | uint32_t hash = __calcHashCode( k ); | 385 | uint32_t hash = __calcHashCode( k ); |
386 | bool bFill; | 386 | bool bFill; |
@@ -401,7 +401,7 @@ namespace Bu | |||
401 | *@param k (key_type) Key to list the value under. | 401 | *@param k (key_type) Key to list the value under. |
402 | *@param v (value_type) Value to store in the hash table. | 402 | *@param v (value_type) Value to store in the hash table. |
403 | */ | 403 | */ |
404 | virtual void insert( key k, value v ) | 404 | virtual void insert( const key &k, const value &v ) |
405 | { | 405 | { |
406 | uint32_t hash = __calcHashCode( k ); | 406 | uint32_t hash = __calcHashCode( k ); |
407 | bool bFill; | 407 | bool bFill; |
@@ -424,7 +424,7 @@ namespace Bu | |||
424 | * Remove a value from the hash table. | 424 | * Remove a value from the hash table. |
425 | *@param k (key_type) The data under this key will be erased. | 425 | *@param k (key_type) The data under this key will be erased. |
426 | */ | 426 | */ |
427 | virtual void erase( key k ) | 427 | virtual void erase( const key &k ) |
428 | { | 428 | { |
429 | uint32_t hash = __calcHashCode( k ); | 429 | uint32_t hash = __calcHashCode( k ); |
430 | bool bFill; | 430 | bool bFill; |
@@ -477,7 +477,7 @@ namespace Bu | |||
477 | *@param k (key_type) Key pointing to the data to be retrieved. | 477 | *@param k (key_type) Key pointing to the data to be retrieved. |
478 | *@returns (value_type &) The data pointed to by (k). | 478 | *@returns (value_type &) The data pointed to by (k). |
479 | */ | 479 | */ |
480 | virtual value &get( key k ) | 480 | virtual value &get( const key &k ) |
481 | { | 481 | { |
482 | uint32_t hash = __calcHashCode( k ); | 482 | uint32_t hash = __calcHashCode( k ); |
483 | bool bFill; | 483 | bool bFill; |
@@ -502,7 +502,7 @@ namespace Bu | |||
502 | *@returns (const value_type &) A const version of the data pointed | 502 | *@returns (const value_type &) A const version of the data pointed |
503 | * to by (k). | 503 | * to by (k). |
504 | */ | 504 | */ |
505 | virtual const value &get( key k ) const | 505 | virtual const value &get( const key &k ) const |
506 | { | 506 | { |
507 | uint32_t hash = __calcHashCode( k ); | 507 | uint32_t hash = __calcHashCode( k ); |
508 | bool bFill; | 508 | bool bFill; |
@@ -526,7 +526,7 @@ namespace Bu | |||
526 | *@param k (key_type) The key to check. | 526 | *@param k (key_type) The key to check. |
527 | *@returns (bool) Whether there was an item in the hash under key (k). | 527 | *@returns (bool) Whether there was an item in the hash under key (k). |
528 | */ | 528 | */ |
529 | virtual bool has( key k ) | 529 | virtual bool has( const key &k ) |
530 | { | 530 | { |
531 | bool bFill; | 531 | bool bFill; |
532 | probe( __calcHashCode( k ), k, bFill, false ); | 532 | probe( __calcHashCode( k ), k, bFill, false ); |
@@ -534,7 +534,7 @@ namespace Bu | |||
534 | return bFill; | 534 | return bFill; |
535 | } | 535 | } |
536 | 536 | ||
537 | virtual bool has( key k ) const | 537 | virtual bool has( const key &k ) const |
538 | { | 538 | { |
539 | bool bFill; | 539 | bool bFill; |
540 | probe( __calcHashCode( k ), k, bFill ); | 540 | probe( __calcHashCode( k ), k, bFill ); |
@@ -873,7 +873,7 @@ namespace Bu | |||
873 | } | 873 | } |
874 | } | 874 | } |
875 | 875 | ||
876 | virtual void fill( uint32_t loc, key &k, value &v, uint32_t hash ) | 876 | virtual void fill( uint32_t loc, const key &k, const value &v, uint32_t hash ) |
877 | { | 877 | { |
878 | bFilled[loc/32] |= (1<<(loc%32)); | 878 | bFilled[loc/32] |= (1<<(loc%32)); |
879 | va.construct( &aValues[loc], v ); | 879 | va.construct( &aValues[loc], v ); |
@@ -945,7 +945,7 @@ namespace Bu | |||
945 | return 0; | 945 | return 0; |
946 | } | 946 | } |
947 | 947 | ||
948 | uint32_t probe( uint32_t hash, key k, bool &bFill, bool rehash=true ) | 948 | uint32_t probe( uint32_t hash, const key &k, bool &bFill, bool rehash=true ) |
949 | { | 949 | { |
950 | uint32_t nCur = hash%nCapacity; | 950 | uint32_t nCur = hash%nCapacity; |
951 | 951 | ||
diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp index 1c873fd..e04a656 100644 --- a/src/unit/hash.cpp +++ b/src/unit/hash.cpp | |||
@@ -94,11 +94,11 @@ public: | |||
94 | unitFailed("h.get(\"Number 2\") should have thrown an exception."); | 94 | unitFailed("h.get(\"Number 2\") should have thrown an exception."); |
95 | } catch( Bu::HashException &e ) { } | 95 | } catch( Bu::HashException &e ) { } |
96 | 96 | ||
97 | printf("\n"); | 97 | /* printf("\n"); |
98 | for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) | 98 | for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) |
99 | { | 99 | { |
100 | printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); | 100 | printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); |
101 | } | 101 | } */ |
102 | } | 102 | } |
103 | }; | 103 | }; |
104 | 104 | ||