Skip to content

Commit

Permalink
class and vector types now hashed by reference rather than value
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed May 20, 2024
1 parent 64e02f2 commit 7f7061c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
10 changes: 0 additions & 10 deletions dev/src/lobster/vmdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,6 @@ struct LObject : RefObj {
return true;
}

uint64_t Hash(VM &vm) {
auto hash = SplitMix64Hash((uint64_t)Len(vm));
for (iint i = 0; i < Len(vm); i++) {
hash = hash * 31 + AtS(i).Hash(vm, ElemTypeS(vm, i).t);
}
return hash;
}

void CopyElemsShallow(Value *from, iint len) {
t_memcpy(Elems(), from, len);
}
Expand Down Expand Up @@ -838,8 +830,6 @@ struct LVector : RefObj {
return true;
}

uint64_t Hash(VM &vm);

void CopyElemsShallow(Value *from) {
t_memcpy(v, from, len * width);
}
Expand Down
28 changes: 3 additions & 25 deletions dev/src/vmdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,37 +379,15 @@ void Value::ToLobsterBinary(VM &vm, vector<uint8_t> &buf, ValueType t) const {


uint64_t RefObj::Hash(VM &vm) {
switch (ti(vm).t) {
case V_STRING: return ((LString *)this)->Hash();
case V_VECTOR: return ((LVector *)this)->Hash(vm);
case V_CLASS: return ((LObject *)this)->Hash(vm);
default: return SplitMix64Hash((uint64_t)this);
}
return ti(vm).t == V_STRING
? ((LString *)this)->Hash()
: SplitMix64Hash((uint64_t)this);
}

uint64_t LString::Hash() {
return FNV1A64(strv());
}

uint64_t LVector::Hash(VM &vm) {
auto &eti = ElemType(vm);
auto hash = SplitMix64Hash((uint64_t)(len * width));
if (IsStruct(eti.t)) {
for (iint i = 0; i < len; i++) {
for (int j = 0; j < width; j++) {
assert(width == eti.len);
auto &ti = vm.GetTypeInfo(eti.elemtypes[j].type);
hash = hash * 31 + AtSub(i, j).Hash(vm, ti.t);
}
}
} else {
for (iint i = 0; i < len; i++) {
hash = hash * 31 + At(i).Hash(vm, eti.t);
}
}
return hash;
}

uint64_t Value::Hash(VM &vm, ValueType vtype) {
switch (vtype) {
case V_INT:
Expand Down

0 comments on commit 7f7061c

Please sign in to comment.