src/share/vm/ci/ciConstantPoolCache.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/ci

src/share/vm/ci/ciConstantPoolCache.cpp

Print this page
rev 10204 : 8007986: GrowableArray should implement binary search
Summary: binary search method for GrowableArray
Reviewed-by:


  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciConstantPoolCache.hpp"
  27 #include "ci/ciUtilities.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 
  31 // ciConstantPoolCache
  32 //
  33 // This class caches indexed constant pool lookups.
  34 
  35 // ------------------------------------------------------------------
  36 // ciConstantPoolCache::ciConstantPoolCache
  37 ciConstantPoolCache::ciConstantPoolCache(Arena* arena,
  38                                  int expected_size) {
  39   _elements =
  40     new (arena) GrowableArray<void*>(arena, expected_size, 0, 0);
  41   _keys = new (arena) GrowableArray<int>(arena, expected_size, 0, 0);
  42 }
  43 






  44 // ------------------------------------------------------------------
  45 // ciConstantPoolCache::get
  46 //
  47 // Get the entry at some index
  48 void* ciConstantPoolCache::get(int index) {
  49   ASSERT_IN_VM;
  50   int pos = find(index);
  51   if (pos >= _keys->length() ||
  52       _keys->at(pos) != index) {
  53     // This element is not present in the cache.
  54     return NULL;
  55   }
  56   return _elements->at(pos);
  57 }
  58 
  59 // ------------------------------------------------------------------
  60 // ciConstantPoolCache::find
  61 //
  62 // Use binary search to find the position of this index in the cache.
  63 // If there is no entry in the cache corresponding to this oop, return
  64 // the position at which the index would be inserted.
  65 int ciConstantPoolCache::find(int key) {
  66   int min = 0;
  67   int max = _keys->length()-1;
  68 
  69   while (max >= min) {
  70     int mid = (max + min) / 2;
  71     int value = _keys->at(mid);
  72     if (value < key) {
  73       min = mid + 1;
  74     } else if (value > key) {
  75       max = mid - 1;
  76     } else {
  77       return mid;
  78     }
  79   }
  80   return min;
  81 }
  82 
  83 // ------------------------------------------------------------------
  84 // ciConstantPoolCache::insert
  85 //
  86 // Insert a ciObject into the table at some index.
  87 void ciConstantPoolCache::insert(int index, void* elem) {
  88   int i;
  89   int pos = find(index);
  90   for (i = _keys->length()-1; i >= pos; i--) {
  91     _keys->at_put_grow(i+1, _keys->at(i));
  92     _elements->at_put_grow(i+1, _elements->at(i));
  93   }
  94   _keys->at_put_grow(pos, index);
  95   _elements->at_put_grow(pos, elem);
  96 }
  97 
  98 // ------------------------------------------------------------------
  99 // ciConstantPoolCache::print
 100 //
 101 // Print debugging information about the cache.
 102 void ciConstantPoolCache::print() {
 103   Unimplemented();
 104 }


  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciConstantPoolCache.hpp"
  27 #include "ci/ciUtilities.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 
  31 // ciConstantPoolCache
  32 //
  33 // This class caches indexed constant pool lookups.
  34 
  35 // ------------------------------------------------------------------
  36 // ciConstantPoolCache::ciConstantPoolCache
  37 ciConstantPoolCache::ciConstantPoolCache(Arena* arena,
  38                                  int expected_size) {
  39   _elements =
  40     new (arena) GrowableArray<void*>(arena, expected_size, 0, 0);
  41   _keys = new (arena) GrowableArray<int>(arena, expected_size, 0, 0);
  42 }
  43 
  44 int ciConstantPoolCache::key_compare(const int& key, const int& elt) {
  45   if (key < elt)      return -1;
  46   else if (key > elt) return 1;
  47   else                  return 0;
  48 }
  49 
  50 // ------------------------------------------------------------------
  51 // ciConstantPoolCache::get
  52 //
  53 // Get the entry at some index
  54 void* ciConstantPoolCache::get(int index) {
  55   ASSERT_IN_VM;
  56   bool found = false;
  57   int pos = _keys->find_sorted<int, ciConstantPoolCache::key_compare>(index, found);
  58   if (!found) {
  59     // This element is not present in the cache.
  60     return NULL;
  61   }
  62   return _elements->at(pos);
  63 }
  64 
  65 // ------------------------------------------------------------------
























  66 // ciConstantPoolCache::insert
  67 //
  68 // Insert a ciObject into the table at some index.
  69 void ciConstantPoolCache::insert(int index, void* elem) {
  70   int i;
  71   bool found = false;
  72   int pos = _keys->find_sorted<int, ciConstantPoolCache::key_compare>(index, found);
  73   assert(!found, "duplicate");
  74   _keys->insert_before(pos, index);
  75   _elements->insert_before(pos, elem);


  76 }
  77 
  78 // ------------------------------------------------------------------
  79 // ciConstantPoolCache::print
  80 //
  81 // Print debugging information about the cache.
  82 void ciConstantPoolCache::print() {
  83   Unimplemented();
  84 }
src/share/vm/ci/ciConstantPoolCache.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File