Skip to content

Commit

Permalink
Do pathHashMod without pathString to not tax the GC so much
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18039 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 6, 2013
1 parent eb95a44 commit b7b1556
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
22 changes: 21 additions & 1 deletion Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2760,9 +2760,29 @@ public function pathHashMod "Hashes a path."
input Integer mod;
output Integer hash;
algorithm
hash := System.stringHashDjb2Mod(pathString(path),mod);
// hash := valueHashMod(path,mod);
// print(pathString(path) +& " => " +& intString(hash) +& "\n");
// hash := System.stringHashDjb2Mod(pathString(path),mod);
// TODO: stringHashDjb2 is missing a default value for the seed; add this once we bootstrapped omc so we can use that function instead of our own hack
hash := intAbs(intMod(pathHashModWork(path,5381),mod));
end pathHashMod;

protected function pathHashModWork "Hashes a path."
input Path path;
input Integer acc;
output Integer hash;
algorithm
hash := match (path,acc)
local
Path p;
String s;
Integer i,i2;
case (FULLYQUALIFIED(p),_) then pathHashModWork(p, acc*31 + 46 /* '.' */);
case (QUALIFIED(s,p),_) equation i = stringHashDjb2(s); i2 = acc*31+46; then pathHashModWork(p, i2*31 + i);
case (IDENT(s),_) equation i = stringHashDjb2(s); i2 = acc*31+46; then i2*31 + i;
end match;
end pathHashModWork;

public function optPathString "Returns a path converted to string or an empty string if nothing exist"
input Option<Path> inPathOption;
output String outString;
Expand Down
44 changes: 27 additions & 17 deletions SimulationRuntime/c/meta/meta_modelica.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,59 +787,69 @@ void changeStdStreamBuffer(void) {
setbuf(stderr, NULL);
}

unsigned long mmc_prim_hash(void *p)
static inline unsigned long djb2_hash_iter(const unsigned char *str /* data; not null-terminated */, int len, unsigned long hash /* start at 5381 */)
{
int c,i;
for (i=0; i<len; i++) {
hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */
}
return hash;
}

unsigned long mmc_prim_hash(void *p,unsigned long hash /* start at 5381 */)
{
unsigned long hash = 0;
void **pp = NULL;
mmc_uint_t phdr = 0;
mmc_uint_t slots = 0;

mmc_prim_hash_tail_recur:
if ((0 == ((mmc_sint_t)p & 1)))
if (MMC_IS_INTEGER(p))
{
return hash + (unsigned long)MMC_UNTAGFIXNUM(p);
unsigned long l = (unsigned long)MMC_UNTAGFIXNUM(p);
return djb2_hash_iter((char*)&l, sizeof(unsigned long), hash);
}

phdr = MMC_GETHDR(p);
hash += (unsigned long)phdr;

if( phdr == MMC_REALHDR )
{
return hash + (unsigned long)mmc_unbox_real(p);
{
double d = mmc_unbox_real(p);
return djb2_hash_iter((char*)&d, sizeof(double), hash);
}

if( MMC_HDRISSTRING(phdr) )
{
return hash + (unsigned long)stringHashDjb2(p);
return djb2_hash_iter(MMC_STRINGDATA(p),MMC_STRLEN(p),hash);
}

if( MMC_HDRISSTRUCT(phdr) )
{
slots = MMC_HDRSLOTS(phdr);
int i;
int slots = MMC_HDRSLOTS(phdr);
int ctor = MMC_HDRCTOR(phdr);
int is_record = slots>0 && ctor > 1 ? 1 : 0;
pp = MMC_STRUCTDATA(p);
hash += MMC_HDRCTOR(phdr);
hash = djb2_hash_iter((char*)&ctor, sizeof(int), hash);
if (slots == 0)
return hash;

while ( --slots > 0)
{
hash += mmc_prim_hash(*pp++);
for (i=2; i<slots; i++) {
hash = mmc_prim_hash(MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(p),i)),hash);
}
p = *pp;
p = MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(p),slots));
goto mmc_prim_hash_tail_recur;
}
return hash;
}

modelica_integer valueHashMod(void *p, modelica_integer mod)
{
modelica_integer res = mmc_prim_hash(p) % (unsigned long) mod;
modelica_integer res = mmc_prim_hash(p,5381) % (unsigned long) mod;
return res;
}

void* boxptr_valueHashMod(threadData_t *threadData,void *p, void *mod)
{
return mmc_mk_icon(mmc_prim_hash(p) % (unsigned long) mmc_unbox_integer(mod));
return mmc_mk_icon(mmc_prim_hash(p,5381) % (unsigned long) mmc_unbox_integer(mod));
}

pthread_once_t mmc_init_once = PTHREAD_ONCE_INIT;
Expand Down

0 comments on commit b7b1556

Please sign in to comment.