Avoid allocations for empty maps

Empty FixedStringMaps would do a malloc of at least 9 bytes,
and IndexedStringMap would do a 1 byte malloc, and a malloc of at least 9
bytes.

We have a lot of typically empty maps in our data structures.
Esse commit está contido em:
mwilliams
2013-06-14 17:46:43 -07:00
commit de Sara Golemon
commit 1626223e97
3 arquivos alterados com 24 adições e 2 exclusões
+16 -1
Ver Arquivo
@@ -14,8 +14,8 @@
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/core_types.h"
#include "hphp/runtime/vm/fixed_string_map.h"
#include "hphp/runtime/vm/core_types.h"
#include "hphp/runtime/base/macros.h"
namespace HPHP {
@@ -26,6 +26,8 @@ TRACE_SET_MOD(runtime);
class Func;
static const StringData* null_key;
inline bool strEqual(bool case_sensitive,
const StringData* sd1, const StringData* sd2) {
if (sd1 == sd2) return true;
@@ -35,8 +37,21 @@ inline bool strEqual(bool case_sensitive,
bstrcaseeq(sd1->data(), sd2->data(), sd1->size());
}
template <typename V, bool case_sensitive>
FixedStringMap<V, case_sensitive>::~FixedStringMap() {
if (m_table != (Elm*)&null_key) {
free(m_table);
}
}
template <typename V, bool case_sensitive>
void FixedStringMap<V, case_sensitive>::init(int num) {
if (!num) {
m_table = (Elm*)&null_key;
m_mask = 0;
return;
}
static const double kLoadFactor = 0.80;
int capac = 1;
while (num >= kLoadFactor * capac) {
+1 -1
Ver Arquivo
@@ -27,7 +27,7 @@ template <typename V, bool case_sensitive> class FixedStringMap {
public:
explicit FixedStringMap(int num) : m_table(0) { init(num); }
FixedStringMap() : m_mask(0), m_table(0) {}
~FixedStringMap() { free(m_table); }
~FixedStringMap();
void init(int num);
void add(const StringData* s, const V& v);
+7
Ver Arquivo
@@ -56,6 +56,13 @@ struct IndexedStringMap {
void create(const Builder& b) {
assert(!m_size && "IndexedStringMap::create called more than once");
m_map.init(b.size());
if (!b.size()) {
assert(!m_vec);
// note that we have to initialize m_map even though its zero
// sized (an empty FixedStringMap isn't quite empty - see
// FixedStringMap::init).
return;
}
m_vec = new T[b.size()];
m_size = b.size();