int32 SharedVariant::getSpaceUsage() {
  int32 size = sizeof(SharedVariant);
  if (!IS_REFCOUNTED_TYPE(m_type)) return size;
  switch (m_type) {
  case KindOfObject:
    if (getIsObj()) {
      return size + m_data.obj->getSpaceUsage();
    }
    // fall through
  case KindOfString:
    size += sizeof(StringData) + m_data.str->size();
    break;
  default:
    ASSERT(is(KindOfArray));
    if (getSerializedArray()) {
      size += sizeof(StringData) + m_data.str->size();
    } else if (getIsVector()) {
      size += sizeof(VectorData) +
              sizeof(SharedVariant*) * m_data.vec->size;
      for (size_t i = 0; i < m_data.vec->size; i++) {
        size += m_data.vec->vals[i]->getSpaceUsage();
      }
    } else {
      ImmutableMap *map = m_data.map;
      size += map->getStructSize();
      for (int i = 0; i < map->size(); i++) {
        size += map->getKeyIndex(i)->getSpaceUsage();
        size += map->getValIndex(i)->getSpaceUsage();
      }
    }
    break;
  }
  return size;
}
void SharedVariant::getStats(SharedVariantStats *stats) const {
  stats->initStats();
  stats->variantCount = 1;
  switch (m_type) {
  case KindOfUninit:
  case KindOfNull:
  case KindOfBoolean:
  case KindOfInt64:
  case KindOfDouble:
  case KindOfStaticString:
    stats->dataSize = sizeof(m_data.dbl);
    stats->dataTotalSize = sizeof(SharedVariant);
    break;
  case KindOfObject:
    if (getIsObj()) {
      SharedVariantStats childStats;
      m_data.obj->getSizeStats(&childStats);
      stats->addChildStats(&childStats);
      break;
    }
    // fall through
  case KindOfString:
    stats->dataSize = m_data.str->size();
    stats->dataTotalSize = sizeof(SharedVariant) + sizeof(StringData) +
                           stats->dataSize;
    break;
  default:
    assert(is(KindOfArray));
    if (getSerializedArray()) {
      stats->dataSize = m_data.str->size();
      stats->dataTotalSize = sizeof(SharedVariant) + sizeof(StringData) +
                             stats->dataSize;
      break;
    }
    if (getIsVector()) {
      stats->dataTotalSize = sizeof(SharedVariant) + sizeof(VectorData);
      stats->dataTotalSize += sizeof(SharedVariant*) * m_data.vec->m_size;
      for (size_t i = 0; i < m_data.vec->m_size; i++) {
        SharedVariant *v = m_data.vec->vals()[i];
        SharedVariantStats childStats;
        v->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    } else {
      ImmutableMap *map = m_data.map;
      stats->dataTotalSize = sizeof(SharedVariant) + map->getStructSize();
      for (int i = 0; i < map->size(); i++) {
        SharedVariantStats childStats;
        map->getKeyIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
        map->getValIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    }
    break;
  }
}
void ThreadSharedVariant::getStats(SharedVariantStats *stats) {
  stats->initStats();
  stats->variantCount = 1;
  switch (m_type) {
  case KindOfBoolean:
  case KindOfInt64:
    stats->dataSize = sizeof(m_data.num);
    stats->dataTotalSize = sizeof(ThreadSharedVariant);
    break;
  case KindOfDouble:
    stats->dataSize = sizeof(m_data.dbl);
    stats->dataTotalSize = sizeof(ThreadSharedVariant);
    break;
  case KindOfString:
  case KindOfObject:
    if (m_data.str->isStatic()) {
      stats->dataSize = 0;
      stats->dataTotalSize = sizeof(ThreadSharedVariant);
      break;
    }
    stats->dataSize = m_data.str->size();
    stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(StringData) +
                           stats->dataSize;
    break;
  default:
    ASSERT(is(KindOfArray));
    if (getSerializedArray()) {
      stats->dataSize = m_data.str->size();
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(StringData) +
                             stats->dataSize;
      break;
    }
    if (getIsVector()) {
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(VectorData);
      stats->dataTotalSize += sizeof(ThreadSharedVariant*) * m_data.vec->size;
      for (size_t i = 0; i < m_data.vec->size; i++) {
        ThreadSharedVariant *v = m_data.vec->vals[i];
        SharedVariantStats childStats;
        v->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    } else {
      ImmutableMap *map = m_data.map;
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + map->getStructSize();
      for (int i = 0; i < map->size(); i++) {
        SharedVariantStats childStats;
        map->getKeyIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
        map->getValIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    }
    break;
  }
}
int32 ThreadSharedVariant::getSpaceUsage() {
  int32 size = sizeof(ThreadSharedVariant);
  if (m_type <= KindOfDouble) return size;
  switch (m_type) {
  case KindOfObject:
    if (getIsObj()) {
      return size + m_data.obj->getSpaceUsage();
    }
    // fall through
  case KindOfString:
    if (m_data.str->isStatic()) {
      break;
    }
    size += sizeof(StringData) + m_data.str->size();
    break;
  default:
    ASSERT(is(KindOfArray));
    if (getSerializedArray()) {
      size += sizeof(StringData) + m_data.str->size();
    } else if (getIsVector()) {
      size += sizeof(VectorData) +
              sizeof(ThreadSharedVariant*) * m_data.vec->size;
      for (size_t i = 0; i < m_data.vec->size; i++) {
        size += m_data.vec->vals[i]->getSpaceUsage();
      }
    } else if (RuntimeOption::ApcUseGnuMap) {
      // Not accurate
      size += sizeof(MapData);
    } else {
      ImmutableMap *map = m_data.map;
      size += map->getStructSize();
      for (int i = 0; i < map->size(); i++) {
        size += map->getKeyIndex(i)->getSpaceUsage();
        size += map->getValIndex(i)->getSpaceUsage();
      }
    }
    break;
  }
  return size;
}
void ThreadSharedVariant::getStats(SharedVariantStats *stats) {
  stats->initStats();
  stats->variantCount = 1;
  switch (m_type) {
  case KindOfNull:
  case KindOfBoolean:
  case KindOfInt64:
  case KindOfDouble:
    stats->dataSize = sizeof(m_data.dbl);
    stats->dataTotalSize = sizeof(ThreadSharedVariant);
    break;
  case KindOfObject:
    if (getIsObj()) {
      SharedVariantStats childStats;
      m_data.obj->getSizeStats(&childStats);
      stats->addChildStats(&childStats);
      break;
    }
    // fall through
  case KindOfString:
   if (m_data.str->isStatic()) {
      stats->dataSize = 0;
      stats->dataTotalSize = sizeof(ThreadSharedVariant);
      break;
    }
    stats->dataSize = m_data.str->size();
    stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(StringData) +
                           stats->dataSize;
    break;
  default:
    ASSERT(is(KindOfArray));
    if (getSerializedArray()) {
      stats->dataSize = m_data.str->size();
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(StringData) +
                             stats->dataSize;
      break;
    }
    if (getIsVector()) {
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + sizeof(VectorData);
      stats->dataTotalSize += sizeof(ThreadSharedVariant*) * m_data.vec->size;
      for (size_t i = 0; i < m_data.vec->size; i++) {
        ThreadSharedVariant *v = m_data.vec->vals[i];
        SharedVariantStats childStats;
        v->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    } else if (RuntimeOption::ApcUseGnuMap) {
      // There is no way to calculate this accurately, and this should be only
      // used if ImmutableMap is seriously broken, when profiling is less
      // important. Just not do basic thing here:
      stats->dataTotalSize = sizeof(MapData);
    } else {
      ImmutableMap *map = m_data.map;
      stats->dataTotalSize = sizeof(ThreadSharedVariant) + map->getStructSize();
      for (int i = 0; i < map->size(); i++) {
        SharedVariantStats childStats;
        map->getKeyIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
        map->getValIndex(i)->getStats(&childStats);
        stats->addChildStats(&childStats);
      }
    }
    break;
  }
}