int MLSymbolTable::getSymbolID(const char * sym) { int r = 0; bool found = false; // process characters in place. On failure, return null symbol. int len = processSymbolText(sym); if(!(len > 0)) return 0; // look up ID by symbol // This is the fast path, and how we look up symbols from char* in typical code. const std::vector<int>& bin = mHashTable[KRhash(sym)]; // there should be few collisions, so probably the first ID in the hash bin // will be the symbol we are looking for. Unfortunately to test for equality we have to // compare the entire string. // if we replace std::string with a custom char * type that stores its length, // and use SSE to do the compares, they could probably be significantly faster. { MLScopedLock lock(mLock); for(int ID : bin) { const char* symB = mSymbolsByID[ID].c_str(); int lenB = processSymbolText(symB); if(!strncmp(sym, symB, std::max(len, lenB))) { r = ID; found = true; break; } } if(!found) { r = addEntry(sym, len); } } return r; }
MLSymbol::MLSymbol(const char *sym, int maxLen) { int len = processSymbolText(sym, maxLen); mID = theSymbolTable().getSymbolID(sym, len); }
MLSymbol::MLSymbol(const std::string& str) { int len = processSymbolText(str.c_str()); mID = theSymbolTable().getSymbolID(str.c_str(), len); }