mirror of
				https://github.com/Z3Prover/z3
				synced 2025-10-26 17:29:21 +00:00 
			
		
		
		
	reduce contention around the symbol table #2842
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
		
							parent
							
								
									88fc4c82aa
								
							
						
					
					
						commit
						670e8f8d67
					
				
					 1 changed files with 44 additions and 11 deletions
				
			
		|  | @ -23,7 +23,7 @@ Revision History: | |||
| #include "util/string_buffer.h" | ||||
| #include <cstring> | ||||
| 
 | ||||
| static DECLARE_MUTEX(g_symbol_lock); | ||||
| 
 | ||||
| 
 | ||||
| symbol symbol::m_dummy(TAG(void*, nullptr, 2)); | ||||
| const symbol symbol::null; | ||||
|  | @ -34,11 +34,21 @@ const symbol symbol::null; | |||
| class internal_symbol_table { | ||||
|     region        m_region; //!< Region used to store symbol strings.
 | ||||
|     str_hashtable m_table;  //!< Table of created symbol strings.
 | ||||
|     DECLARE_MUTEX(lock); | ||||
|      | ||||
| public: | ||||
| 
 | ||||
|     internal_symbol_table() { | ||||
|         ALLOC_MUTEX(lock); | ||||
|     } | ||||
| 
 | ||||
|     ~internal_symbol_table() { | ||||
|         DEALLOC_MUTEX(lock); | ||||
|     } | ||||
| 
 | ||||
|     char const * get_str(char const * d) { | ||||
|         const char * result; | ||||
|         lock_guard lock(*g_symbol_lock); | ||||
|         lock_guard _lock(*lock); | ||||
|         str_hashtable::entry * e; | ||||
|         if (m_table.insert_if_not_there_core(d, e)) { | ||||
|             // new entry
 | ||||
|  | @ -60,30 +70,53 @@ public: | |||
|     } | ||||
| }; | ||||
| 
 | ||||
| static internal_symbol_table* g_symbol_table = nullptr; | ||||
| struct internal_symbol_tables { | ||||
|     unsigned sz; | ||||
|     internal_symbol_table** tables; | ||||
| 
 | ||||
|     internal_symbol_tables(unsigned sz): sz(sz), tables(alloc_vect<internal_symbol_table*>(sz)) { | ||||
|         for (unsigned i = 0; i < sz; ++i) { | ||||
|             tables[i] = alloc(internal_symbol_table); | ||||
|         } | ||||
|     } | ||||
|     ~internal_symbol_tables() { | ||||
|         for (unsigned i = 0; i < sz; ++i) { | ||||
|             dealloc(tables[i]); | ||||
|         } | ||||
|         dealloc_vect<internal_symbol_table*>(tables, sz); | ||||
|     } | ||||
| 
 | ||||
|     char const * get_str(char const * d) { | ||||
|         auto* table = tables[string_hash(d, static_cast<unsigned>(strlen(d)), 251) % sz]; | ||||
|         return table->get_str(d); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| static internal_symbol_tables* g_symbol_tables = nullptr; | ||||
| 
 | ||||
| void initialize_symbols() { | ||||
|     if (!g_symbol_table) { | ||||
|         ALLOC_MUTEX(g_symbol_lock); | ||||
|         g_symbol_table = alloc(internal_symbol_table); | ||||
|     if (!g_symbol_tables) { | ||||
|         unsigned num_tables = 2 * std::min((unsigned) std::thread::hardware_concurrency(), 64u); | ||||
|         g_symbol_tables = alloc(internal_symbol_tables, num_tables); | ||||
|          | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void finalize_symbols() { | ||||
|     DEALLOC_MUTEX(g_symbol_lock); | ||||
|     dealloc(g_symbol_table); | ||||
|     g_symbol_table = nullptr; | ||||
|     dealloc(g_symbol_tables); | ||||
|     g_symbol_tables = nullptr; | ||||
| } | ||||
| 
 | ||||
| symbol::symbol(char const * d) { | ||||
|     if (d == nullptr) | ||||
|         m_data = nullptr; | ||||
|     else | ||||
|         m_data = g_symbol_table->get_str(d); | ||||
|         m_data = g_symbol_tables->get_str(d); | ||||
| } | ||||
| 
 | ||||
| symbol & symbol::operator=(char const * d) { | ||||
|     m_data = g_symbol_table->get_str(d); | ||||
|     m_data = g_symbol_tables->get_str(d); | ||||
|     return *this; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue