Explorar o código

Moved DBMap's string maxlen parameter translation (0 -> UINT16_MAX) to its initialization phase, so that it runs once instead of on every hash and compare step. This will slightly speed up most strdb operations.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@14854 54d463be-8e91-2dee-dedb-b68131a5f0ec
ultramage %!s(int64=14) %!d(string=hai) anos
pai
achega
170e322ccc
Modificáronse 3 ficheiros con 7 adicións e 19 borrados
  1. 1 0
      Changelog-Trunk.txt
  2. 5 14
      src/common/db.c
  3. 1 5
      src/common/db.h

+ 1 - 0
Changelog-Trunk.txt

@@ -1,6 +1,7 @@
 Date	Added
 
 2011/06/16
+	* Slight speedup on most strdb operations [ultramage]
 	* Fixed DBMap's db_dup_key to allocate only as much as necessary (bugreport:4969) [ultramage]
 	* Fixed char-converter not being able to compile due to both char.h being included. (caused by last commit) [FlavioJS]
 	* Merges from charmerge:

+ 5 - 14
src/common/db.c

@@ -632,14 +632,12 @@ static DBKey db_dup_key(DBMap_impl* db, DBKey key)
 {
 	char *str;
 	size_t len;
-	unsigned short maxlen;
 
 	DB_COUNTSTAT(db_dup_key);
 	switch (db->type) {
 		case DB_STRING:
 		case DB_ISTRING:
-			maxlen = ( db->maxlen != 0 ) ? db->maxlen : UINT16_MAX;
-			len = strnlen(key.str, maxlen);
+			len = strnlen(key.str, db->maxlen);
 			str = (char*)aMalloc(len + 1);
 			memcpy(str, key.str, len);
 			str[len] = '\0';
@@ -889,8 +887,6 @@ static int db_uint_cmp(DBKey key1, DBKey key2, unsigned short maxlen)
 static int db_string_cmp(DBKey key1, DBKey key2, unsigned short maxlen)
 {
 	DB_COUNTSTAT(db_string_cmp);
-	if (maxlen == 0)
-		maxlen = UINT16_MAX;
 	return strncmp((const char *)key1.str, (const char *)key2.str, maxlen);
 }
 
@@ -909,8 +905,6 @@ static int db_string_cmp(DBKey key1, DBKey key2, unsigned short maxlen)
 static int db_istring_cmp(DBKey key1, DBKey key2, unsigned short maxlen)
 {
 	DB_COUNTSTAT(db_istring_cmp);
-	if (maxlen == 0)
-		maxlen = UINT16_MAX;
 	return strncasecmp((const char *)key1.str, (const char *)key2.str, maxlen);
 }
 
@@ -952,7 +946,6 @@ static unsigned int db_uint_hash(DBKey key, unsigned short maxlen)
 
 /**
  * Default hasher for DB_STRING databases.
- * If maxlen if 0, the maximum number of maxlen is used instead.
  * @param key Key to be hashed
  * @param maxlen Maximum length of the key to hash
  * @return hash of the key
@@ -967,8 +960,6 @@ static unsigned int db_string_hash(DBKey key, unsigned short maxlen)
 	unsigned short i;
 
 	DB_COUNTSTAT(db_string_hash);
-	if (maxlen == 0)
-		maxlen = UINT16_MAX;
 
 	for (i = 0; *k; ++i) {
 		hash = (hash*33 + ((unsigned char)*k))^(hash>>24);
@@ -982,7 +973,6 @@ static unsigned int db_string_hash(DBKey key, unsigned short maxlen)
 
 /**
  * Default hasher for DB_ISTRING databases.
- * If maxlen if 0, the maximum number of maxlen is used instead.
  * @param key Key to be hashed
  * @param maxlen Maximum length of the key to hash
  * @return hash of the key
@@ -996,8 +986,6 @@ static unsigned int db_istring_hash(DBKey key, unsigned short maxlen)
 	unsigned short i;
 
 	DB_COUNTSTAT(db_istring_hash);
-	if (maxlen == 0)
-		maxlen = UINT16_MAX;
 
 	for (i = 0; *k; i++) {
 		hash = (hash*33 + ((unsigned char)TOLOWER(*k)))^(hash>>24);
@@ -2379,7 +2367,7 @@ DBReleaser db_custom_release(DBRelease which)
  * @param type Type of database
  * @param options Options of the database
  * @param maxlen Maximum length of the string to be used as key in string 
- *          databases
+ *          databases. If 0, the maximum number of maxlen is used (64K).
  * @return The interface of the database
  * @public
  * @see #DBMap_impl
@@ -2443,6 +2431,9 @@ DBMap* db_alloc(const char *file, int line, DBType type, DBOptions options, unsi
 	db->maxlen = maxlen;
 	db->global_lock = 0;
 
+	if( db->maxlen == 0 && (type == DB_STRING || type == DB_ISTRING) )
+		db->maxlen = UINT16_MAX;
+
 	return &db->vtable;
 }
 

+ 1 - 5
src/common/db.h

@@ -205,8 +205,6 @@ typedef int (*DBMatcher)(DBKey key, void* data, va_list args);
 /**
  * Format of the comparators used internally by the database system.
  * Compares key1 to key2.
- * <code>maxlen</code> is the maximum number of character used in DB_STRING and 
- * DB_ISTRING databases. If 0, the maximum number of maxlen is used (64K).
  * Returns 0 is equal, negative if lower and positive is higher.
  * @param key1 Key being compared
  * @param key2 Key we are comparing to
@@ -221,8 +219,6 @@ typedef int (*DBComparator)(DBKey key1, DBKey key2, unsigned short maxlen);
 /**
  * Format of the hashers used internally by the database system.
  * Creates the hash of the key.
- * <code>maxlen</code> is the maximum number of character used in DB_STRING and 
- * DB_ISTRING databases. If 0, the maximum number of maxlen is used (64K).
  * @param key Key being hashed
  * @param maxlen Maximum number of characters used in DB_STRING and DB_ISTRING
  *          databases.
@@ -721,7 +717,7 @@ DBReleaser db_custom_release(DBRelease which);
  * @param type Type of database
  * @param options Options of the database
  * @param maxlen Maximum length of the string to be used as key in string 
- *          databases
+ *          databases. If 0, the maximum number of maxlen is used (64K).
  * @return The interface of the database
  * @public
  * @see #DBType