Skip to content

Commit

Permalink
[mycpp/runtime] Add unused hash_value_ field to Str
Browse files Browse the repository at this point in the history
Let's see what the runtime impact is

It should show up in benchmarks2/uftrace
  • Loading branch information
Andy C committed Aug 10, 2023
1 parent 0a38258 commit 145073a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions mycpp/gc_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ inline Str* NewStr(int len) {
#if defined(MARK_SWEEP) || defined(BUMP_LEAK)
s->data_[len] = '\0'; // NUL terminate
s->len_ = len;
s->hash_value_ = -1; // this could be a valid hash value
#else
// reversed in len() to derive string length
header->obj_len = kStrHeaderSize + len + 1;
Expand Down Expand Up @@ -176,6 +177,8 @@ inline Str* OverAllocatedStr(int len) {
#endif
ObjHeader* header = new (place) ObjHeader(Str::obj_header());
auto s = new (header->ObjectAddress()) Str();
s->hash_value_ = -1; // this could be a valid hash value

#if MARK_SWEEP
header->obj_id = obj_id;
#ifndef NO_POOL_ALLOC
Expand Down
1 change: 1 addition & 0 deletions mycpp/gc_heap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
static_assert(offsetof(Str, field) == offsetof(GlobalStr<1>, field), \
"Str and GlobalStr should be consistent");
ASSERT_GLOBAL_STR(len_);
ASSERT_GLOBAL_STR(hash_value_);
ASSERT_GLOBAL_STR(data_);

static_assert(offsetof(Slab<int>, items_) ==
Expand Down
12 changes: 8 additions & 4 deletions mycpp/gc_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Str {
}

int len_;
int hash_value_;
char data_[1]; // flexible array

private:
Expand Down Expand Up @@ -147,6 +148,7 @@ class GlobalStr {
// buffer of size N). For initializing global constant instances.
public:
int len_;
int hash_value_;
const char data_[N];

DISALLOW_COPY_AND_ASSIGN(GlobalStr)
Expand All @@ -159,11 +161,13 @@ class GlobalStr {
//
// https://old.reddit.com/r/cpp_questions/comments/j0khh6/how_to_constexpr_initialize_class_member_thats/
// https://stackoverflow.com/questions/10422487/how-can-i-initialize-char-arrays-in-a-constructor
//
// TODO: Can we hash values at compile time so they can be in the intern table?

#define GLOBAL_STR(name, val) \
GcGlobal<GlobalStr<sizeof(val)>> _##name = { \
ObjHeader::Global(TypeTag::Str), \
{.len_ = sizeof(val) - 1, .data_ = val}}; \
#define GLOBAL_STR(name, val) \
GcGlobal<GlobalStr<sizeof(val)>> _##name = { \
ObjHeader::Global(TypeTag::Str), \
{.len_ = sizeof(val) - 1, .hash_value_ = 0, .data_ = val}}; \
Str* name = reinterpret_cast<Str*>(&_##name.obj);

#endif // MYCPP_GC_STR_H

0 comments on commit 145073a

Please sign in to comment.