Ticket #145: cache.c.patch

File cache.c.patch, 6.7 kB (added by draqo, 2 years ago)
  • gnugo/engine/cache.c

    RCS file: /sources/gnugo/gnugo/engine/cache.c,v
    retrieving revision 1.50
    diff -u -r1.50 cache.c
     
    3939/* ---------------------------------------------------------------- */ 
    4040 
    4141static void tt_init(Transposition_table *table, int memsize); 
    42 static void tt_clear(Transposition_table *table); 
     42inline static void tt_clear(Transposition_table *table); 
    4343 
    4444/* The transposition table itself. */ 
    4545Transposition_table ttable; 
     
    7272calculate_hashval_for_tt(Hash_data *hashdata, int routine, int target1, 
    7373                         int target2, Hash_data *extra_hash) 
    7474{  
    75   *hashdata = board_hash;                /* from globals.c */ 
     75  *hashdata = board_hash;                /* from boardlib.c */ 
    7676  hashdata_xor(*hashdata, routine_hash[routine]); 
    7777  hashdata_xor(*hashdata, target1_hash[target1]); 
    7878  if (target2 != NO_MOVE) 
     
    9797  keyhash_init(); 
    9898 
    9999  if (memsize > 0) 
    100     num_entries = memsize / sizeof(table->entries[0]); 
     100    num_entries = memsize / sizeof(Hashentry); 
    101101  else 
    102102    num_entries = DEFAULT_NUMBER_OF_CACHE_ENTRIES; 
    103103 
    104104  table->num_entries = num_entries; 
    105   table->entries     = malloc(num_entries * sizeof(table->entries[0])); 
     105  table->entries     = malloc(num_entries * sizeof(Hashentry)); 
    106106 
    107107  if (table->entries == NULL) { 
    108108    perror("Couldn't allocate memory for transposition table. \n"); 
    109109    exit(1); 
    110110  } 
    111111 
    112   table->is_clean = 0; 
    113112  tt_clear(table); 
    114113} 
    115114 
    116115 
    117116/* Clear the transposition table. */ 
    118117 
    119 static void 
     118inline static void 
    120119tt_clear(Transposition_table *table) 
    121120{ 
    122   if (!table->is_clean) { 
    123     memset(table->entries, 0, table->num_entries * sizeof(table->entries[0])); 
    124     table->is_clean = 1; 
    125   } 
     121  memset(table->entries, 0, table->num_entries * sizeof(Hashentry)); 
    126122} 
    127   
    128   
     123 
     124 
    129125/* Free the transposition table. */ 
    130126 
    131 void 
     127inline void 
    132128tt_free(Transposition_table *table) 
    133129{ 
    134130  free(table->entries); 
    135131} 
    136132 
    137133 
    138 /* Get result and move. Return value: 
     134/* Get result and move (the bigger remaining depth the lower depth). 
     135 * Return value: 
    139136 *   0 if not found 
    140  *   1 if found, but depth too small to be trusted.  In this case the move 
     137 *   1 if found, but depth too big to be trusted.  In this case the move 
    141138 *     can be used for move ordering. 
    142  *   2 if found and depth is enough so that the result can be trusted. 
     139 *   2 if found and depth is small enough so that the result can be trusted. 
    143140 */ 
    144141  
    145142int 
     
    162159 
    163160  /* Get the correct entry and node. */ 
    164161  entry = &table->entries[hashdata_remainder(hashval, table->num_entries)]; 
    165   if (hashdata_is_equal(hashval, entry->deepest.key)) 
    166     node = &entry->deepest; 
     162 
     163  if (hashdata_is_equal(hashval, entry->most_reliable.key)) 
     164    node = &entry->most_reliable; 
    167165  else if (hashdata_is_equal(hashval, entry->newest.key)) 
    168166    node = &entry->newest; 
    169167  else 
    170168    return 0; 
    171169 
    172   stats.read_result_hits++; 
     170#ifndef GG_TURN_OFF_STATS 
     171  ++stats.read_result_hits; 
     172#endif 
    173173 
    174174  /* Return data.  Only set the result if remaining depth in the table 
    175175   * is big enough to be trusted.  The move can always be used for move 
     
    177177   */ 
    178178  if (move) 
    179179    *move = hn_get_move(node->data); 
     180 
    180181  if (remaining_depth <= (int) hn_get_remaining_depth(node->data)) { 
    181182    if (value1) 
    182183      *value1 = hn_get_value1(node->data); 
    183184    if (value2) 
    184185      *value2 = hn_get_value2(node->data); 
    185     stats.trusted_read_result_hits++; 
     186 
     187#ifndef GG_TURN_OFF_STATS 
     188    ++stats.trusted_read_result_hits; 
     189#endif 
     190 
    186191    return 2; 
    187192  } 
    188193 
     
    201206{ 
    202207  Hash_data hashval; 
    203208  Hashentry *entry; 
    204   Hashnode *deepest; 
     209  Hashnode *most_reliable; 
    205210  Hashnode *newest; 
    206211  unsigned int data; 
    207212  /* Get routine costs definitions from liberty.h. */ 
     
    220225 
    221226  /* Get the entry and nodes. */  
    222227  entry = &table->entries[hashdata_remainder(hashval, table->num_entries)]; 
    223   deepest = &entry->deepest; 
     228  most_reliable = &entry->most_reliable; 
    224229  newest  = &entry->newest; 
    225230  
    226231  /* See if we found an already existing node. */ 
    227   if (hashdata_is_equal(hashval, deepest->key) 
    228       && remaining_depth >= (int) hn_get_remaining_depth(deepest->data)) { 
    229  
    230     /* Found deepest */ 
    231     deepest->data = data; 
    232  
     232  if (hashdata_is_equal(hashval, most_reliable->key)) 
     233  { 
     234          /* Found shallower node (can be trusted more likely) */ 
     235          if (remaining_depth > (int) hn_get_remaining_depth(most_reliable->data)) 
     236                  most_reliable->data = data; 
    233237  } 
    234   else if (hashdata_is_equal(hashval, newest->key) 
    235            && remaining_depth >= (int) hn_get_remaining_depth(newest->data)) { 
    236  
    237     /* Found newest */ 
    238     newest->data = data; 
    239  
    240     /* If newest has become deeper than deepest, then switch them. */ 
    241     if (hn_get_remaining_depth(newest->data) 
    242         > hn_get_remaining_depth(deepest->data)) { 
    243       Hashnode temp; 
    244  
    245       temp = *deepest; 
    246       *deepest = *newest; 
    247       *newest = temp; 
    248     } 
    249  
     238  else if (hashdata_is_equal(hashval, newest->key)) 
     239  { 
     240          /* Found newest */ 
     241          if (remaining_depth > (int) hn_get_remaining_depth(newest->data)) 
     242          { 
     243        newest->data = data; 
     244 
     245        /* If newest reliability is bigger than most_reliable, switch them. */ 
     246            if (hn_get_reliability(newest->data) > 
     247                        hn_get_reliability(most_reliable->data)) 
     248                { 
     249                        Hashnode temp; 
     250 
     251                        temp = *most_reliable; 
     252                        *most_reliable = *newest; 
     253                        *newest = temp; 
     254                } 
     255          } 
     256  } 
     257  /* Have new node. */ 
     258  else if (hn_get_reliability(data) > 
     259          hn_get_reliability(most_reliable->data)) 
     260  { 
     261          /* Replace most_reliable. */ 
     262          most_reliable->key  = hashval; 
     263          most_reliable->data = data; 
    250264  } 
    251   else if (hn_get_total_cost(data) > hn_get_total_cost(deepest->data)) { 
    252     if (hn_get_total_cost(newest->data) < hn_get_total_cost(deepest->data)) 
    253       *newest = *deepest; 
    254     deepest->key  = hashval; 
    255     deepest->data = data; 
    256   }  
    257   else { 
     265  else 
     266  { 
    258267    /* Replace newest. */ 
    259268    newest->key  = hashval; 
    260269    newest->data = data; 
    261270  } 
    262271 
    263   stats.read_result_entered++; 
    264   table->is_clean = 0; 
     272#ifndef GG_TURN_OFF_STATS 
     273  ++stats.read_result_entered; 
     274#endif 
    265275} 
    266276 
    267277 
     
    270280}; 
    271281 
    272282/* Convert a routine as used in the cache table to a string. */ 
    273 const char * 
     283inline const char * 
    274284routine_id_to_string(enum routine_id routine) 
    275285{ 
    276286  return routine_names[(int) routine]; 
     
    282292 * allocate a single node or if the allocation fails, the caching is 
    283293 * disabled. 
    284294 */ 
    285 void 
     295inline void 
    286296reading_cache_init(int bytes) 
    287297{ 
    288298  tt_init(&ttable, bytes); 
     
    290300 
    291301 
    292302/* Clear the cache for read results. */ 
    293 void 
     303inline void 
    294304reading_cache_clear() 
    295305{ 
    296306  tt_clear(&ttable); 
    297307} 
    298308 
    299 float 
     309inline float 
    300310reading_cache_default_size() 
    301311{ 
    302312  return DEFAULT_NUMBER_OF_CACHE_ENTRIES * sizeof(Hashentry) / 1024.0 / 1024.0;