[vmkit-commits] [vmkit] r136599 - in /vmkit/trunk: include/mvm/UTF8.h lib/Mvm/Runtime/UTF8.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Sun Jul 31 10:57:57 PDT 2011


Author: geoffray
Date: Sun Jul 31 12:57:57 2011
New Revision: 136599

URL: http://llvm.org/viewvc/llvm-project?rev=136599&view=rev
Log:
Move UTF8Map to a DenseMap.


Modified:
    vmkit/trunk/include/mvm/UTF8.h
    vmkit/trunk/lib/Mvm/Runtime/UTF8.cpp

Modified: vmkit/trunk/include/mvm/UTF8.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/UTF8.h?rev=136599&r1=136598&r2=136599&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/UTF8.h (original)
+++ vmkit/trunk/include/mvm/UTF8.h Sun Jul 31 12:57:57 2011
@@ -85,35 +85,44 @@
 
 
 struct UTF8MapKey {
-  uint32_t hash;
-  const UTF8* data;
-  UTF8MapKey(uint32_t h, const UTF8* d) : hash(h), data(d) {}
+  ssize_t length;
+  const uint16_t* data;
+
+  UTF8MapKey(const uint16_t* d, ssize_t l) {
+    data = d;
+    length = l;
+  }
 };
 
 // Provide MvmDenseMapInfo for UTF8MapKey.
 template<>
 struct MvmDenseMapInfo<UTF8MapKey> {
   static inline const UTF8MapKey getEmptyKey() {
-    static UTF8MapKey EmptyKey(0, NULL);
+    static UTF8MapKey EmptyKey(NULL, -1);
     return EmptyKey;
   }
   static inline const UTF8MapKey getTombstoneKey() {
-    static UTF8MapKey TombstoneKey(-1, NULL);
+    static UTF8MapKey TombstoneKey(NULL, -2);
     return TombstoneKey;
   }
   static unsigned getHashValue(const UTF8MapKey& key) {
-    return key.data->hash();
+    return UTF8::readerHasher(key.data, key.length);
+  }
+  static bool isEqual(const UTF8MapKey& LHS, const UTF8MapKey& RHS) {
+    if (LHS.data == RHS.data) return true;
+    if (LHS.length != RHS.length) return false;
+    return !memcmp(LHS.data, RHS.data, RHS.length * sizeof(uint16));
   }
-  static bool isEqual(const UTF8MapKey& LHS, const UTF8MapKey& RHS) { return LHS.data->equals(RHS.data); }
 };
 
 class UTF8Map : public mvm::PermanentObject {
 private:
-  typedef std::multimap<const uint32, const UTF8*>::iterator iterator;
+  typedef MvmDenseMap<UTF8MapKey, const UTF8*>::iterator iterator;
   
-  mvm::LockNormal lock;
-  mvm::BumpPtrAllocator& allocator;
-  std::multimap<const uint32, const UTF8*> map;
+  LockNormal lock;
+  BumpPtrAllocator& allocator;
+  // TODO(ngeoffray): This should really be a set.
+  MvmDenseMap<UTF8MapKey, const UTF8*> map;
 public:
 
   const UTF8* lookupOrCreateAsciiz(const char* asciiz); 
@@ -121,7 +130,7 @@
   const UTF8* lookupAsciiz(const char* asciiz); 
   const UTF8* lookupReader(const uint16* buf, uint32 size);
   
-  UTF8Map(mvm::BumpPtrAllocator& A) : allocator(A) {}
+  UTF8Map(BumpPtrAllocator& A) : allocator(A) {}
 
   ~UTF8Map() {
     for (iterator i = map.begin(), e = map.end(); i!= e; ++i) {

Modified: vmkit/trunk/lib/Mvm/Runtime/UTF8.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/Runtime/UTF8.cpp?rev=136599&r1=136598&r2=136599&view=diff
==============================================================================
--- vmkit/trunk/lib/Mvm/Runtime/UTF8.cpp (original)
+++ vmkit/trunk/lib/Mvm/Runtime/UTF8.cpp Sun Jul 31 12:57:57 2011
@@ -34,15 +34,6 @@
   return map->lookupOrCreateReader(buf, len);
 }
 
-static uint32 asciizHasher(const char* asciiz, sint32 size) {
-  uint32 r0 = 0, r1 = 0;
-  for (sint32 i = 0; i < size; i++) {
-    char c = asciiz[i];
-    r0 += c;
-    r1 ^= c;
-  }
-  return (r1 & 255) + ((r0 & 255) << 8);
-}
 
 uint32 UTF8::readerHasher(const uint16* buf, sint32 size) {
   uint32 r0 = 0, r1 = 0;
@@ -54,117 +45,63 @@
   return (r1 & 255) + ((r0 & 255) << 8);
 }
 
-static bool asciizEqual(const UTF8* val, const char* asciiz, sint32 size) {
-  sint32 len = val->size;
-  if (len != size) return false;
-  else {
-    for (sint32 i = 0; i < len; i++) {
-      if (asciiz[i] != val->elements[i]) return false;
-    }
-    return true;
-  }
-}
-
-static bool readerEqual(const UTF8* val, const uint16* buf, sint32 size) {
-  sint32 len = val->size;
-  if (len != size) return false;
-  else return !(memcmp(val->elements, buf, len * sizeof(uint16)));
-}
 
 const UTF8* UTF8Map::lookupOrCreateAsciiz(const char* asciiz) {
   sint32 size = strlen(asciiz);
-  uint32 key = asciizHasher(asciiz, size);
-  const UTF8* res = 0;
-  lock.lock();
-  
-  std::pair<UTF8Map::iterator, UTF8Map::iterator> p = map.equal_range(key);
-  
-  for (UTF8Map::iterator i = p.first; i != p.second; i++) {
-    if (asciizEqual(i->second, asciiz, size)) {
-      res = i->second;
-      break;
-    }
-  }
-
-  if (res == 0) {
-    UTF8* tmp = new(allocator, size) UTF8(size);
-    for (sint32 i = 0; i < size; i++) {
-      tmp->elements[i] = asciiz[i];
-    }
-    res = (const UTF8*)tmp;
-    map.insert(std::make_pair(key, res));
+  ThreadAllocator tempAllocator;
+  uint16_t* data = reinterpret_cast<uint16_t*>(
+      tempAllocator.Allocate(size * sizeof(uint16_t)));
+  for (int i = 0; i < size; i++) {
+    data[i] = asciiz[i];
   }
-  
-  lock.unlock();
-  return res;
+  return lookupOrCreateReader(data, size);
 }
 
+
 const UTF8* UTF8Map::lookupOrCreateReader(const uint16* buf, uint32 len) {
   sint32 size = (sint32)len;
-  uint32 key = UTF8::readerHasher(buf, size);
-  const UTF8* res = 0;
+  UTF8MapKey key(buf, size);
   lock.lock();
-  
-  std::pair<UTF8Map::iterator, UTF8Map::iterator> p = map.equal_range(key);
-
-  for (UTF8Map::iterator i = p.first; i != p.second; i++) {
-    if (readerEqual(i->second, buf, size)) {
-      res = i->second;
-      break;
-    }
-  }
 
-  if (res == 0) {
+  const UTF8* res = map.lookup(key);
+  if (res == NULL) {
     UTF8* tmp = new(allocator, size) UTF8(size);
     memcpy(tmp->elements, buf, len * sizeof(uint16));
     res = (const UTF8*)tmp;
-    map.insert(std::make_pair(key, res));
+    key.data = res->elements;
+    map[key] = res;
   }
   
   lock.unlock();
   return res;
 }
 
+
 const UTF8* UTF8Map::lookupAsciiz(const char* asciiz) {
   sint32 size = strlen(asciiz);
-  uint32 key = asciizHasher(asciiz, size);
-  const UTF8* res = 0;
-  lock.lock();
-  
-  std::pair<UTF8Map::iterator, UTF8Map::iterator> p = map.equal_range(key);
-  
-  for (UTF8Map::iterator i = p.first; i != p.second; i++) {
-    if (asciizEqual(i->second, asciiz, size)) {
-      res = i->second;
-      break;
-    }
+  ThreadAllocator tempAllocator;
+  uint16_t* data = reinterpret_cast<uint16_t*>(
+      tempAllocator.Allocate(size * sizeof(uint16_t)));
+  for (int i = 0; i < size; i++) {
+    data[i] = asciiz[i];
   }
-
-  lock.unlock();
-  return res;
+  return lookupReader(data, size);
 }
 
+
 const UTF8* UTF8Map::lookupReader(const uint16* buf, uint32 len) {
   sint32 size = (sint32)len;
-  uint32 key = UTF8::readerHasher(buf, size);
-  const UTF8* res = 0;
+  UTF8MapKey key(buf, size);
   lock.lock();
-  
-  std::pair<UTF8Map::iterator, UTF8Map::iterator> p = map.equal_range(key);
-
-  for (UTF8Map::iterator i = p.first; i != p.second; i++) {
-    if (readerEqual(i->second, buf, size)) {
-      res = i->second;
-      break;
-    }
-  }
-
+  const UTF8* res = map.lookup(key);
   lock.unlock();
   return res;
 }
 
+
 void UTF8Map::insert(const UTF8* val) {
-  map.insert(std::make_pair(val->hash(), val));
+  UTF8MapKey key(val->elements, val->size);
+  map[key] = val;
 }
 
-}
+} // namespace mvm





More information about the vmkit-commits mailing list