diff --git a/C/qlyr.c b/C/qlyr.c index 4bd13f121..5f339a031 100644 --- a/C/qlyr.c +++ b/C/qlyr.c @@ -379,7 +379,7 @@ CloseHash(void) import_dbref_hash_entry_t *a0 = a; #ifdef DEBUG if (!a->count) { - fprintf(stderr,"WARNING: unused reference %p\n",a); + fprintf(stderr,"WARNING: unused reference %p %p\n",a->val, a->oval); } #endif a = a->next; @@ -687,7 +687,7 @@ ReadHash(IOSTREAM *stream) RCHECK(read_tag(stream) == QLY_START_ATOMS); LOCAL_ImportAtomHashTableNum = read_uint(stream); LOCAL_ImportAtomHashTableSize = LOCAL_ImportAtomHashTableNum*2; - LOCAL_ImportAtomHashChain = (import_atom_hash_entry_t **)calloc(1, sizeof(import_atom_hash_entry_t *)* LOCAL_ImportAtomHashTableSize); + LOCAL_ImportAtomHashChain = (import_atom_hash_entry_t **)calloc(LOCAL_ImportAtomHashTableSize, sizeof(import_atom_hash_entry_t *)); for (i = 0; i < LOCAL_ImportAtomHashTableNum; i++) { Atom oat = (Atom)read_uint(stream); Atom at; @@ -729,7 +729,7 @@ ReadHash(IOSTREAM *stream) RCHECK(read_tag(stream) == QLY_START_FUNCTORS); LOCAL_ImportFunctorHashTableNum = read_uint(stream); LOCAL_ImportFunctorHashTableSize = 2*LOCAL_ImportFunctorHashTableNum; - LOCAL_ImportFunctorHashChain = (import_functor_hash_entry_t **)calloc(1, sizeof(import_functor_hash_entry_t *)* LOCAL_ImportFunctorHashTableSize); + LOCAL_ImportFunctorHashChain = (import_functor_hash_entry_t **)calloc(LOCAL_ImportFunctorHashTableSize, sizeof(import_functor_hash_entry_t *)); for (i = 0; i < LOCAL_ImportFunctorHashTableNum; i++) { Functor of = (Functor)read_uint(stream); UInt arity = read_uint(stream); @@ -746,7 +746,7 @@ ReadHash(IOSTREAM *stream) RCHECK(read_tag(stream) == QLY_START_PRED_ENTRIES); LOCAL_ImportPredEntryHashTableNum = read_uint(stream); LOCAL_ImportPredEntryHashTableSize = 2*LOCAL_ImportPredEntryHashTableNum; - LOCAL_ImportPredEntryHashChain = (import_pred_entry_hash_entry_t **)calloc(1, sizeof(import_pred_entry_hash_entry_t *)* LOCAL_ImportPredEntryHashTableSize); + LOCAL_ImportPredEntryHashChain = (import_pred_entry_hash_entry_t **)calloc( LOCAL_ImportPredEntryHashTableSize, sizeof(import_pred_entry_hash_entry_t *)); for (i = 0; i < LOCAL_ImportPredEntryHashTableNum; i++) { PredEntry *ope = (PredEntry *)read_uint(stream), *pe; UInt arity = read_uint(stream); @@ -794,7 +794,7 @@ ReadHash(IOSTREAM *stream) RCHECK(read_tag(stream) == QLY_START_DBREFS); LOCAL_ImportDBRefHashTableNum = read_uint(stream); LOCAL_ImportDBRefHashTableSize = 2*LOCAL_ImportDBRefHashTableNum; - LOCAL_ImportDBRefHashChain = (import_dbref_hash_entry_t **)calloc(1, sizeof(import_dbref_hash_entry_t *)* LOCAL_ImportDBRefHashTableSize); + LOCAL_ImportDBRefHashChain = (import_dbref_hash_entry_t **)calloc(LOCAL_ImportDBRefHashTableSize, sizeof(import_dbref_hash_entry_t *)); for (i = 0; i < LOCAL_ImportDBRefHashTableNum; i++) { LogUpdClause *ocl = (LogUpdClause *)read_uint(stream); UInt sz = read_uint(stream); diff --git a/C/qlyw.c b/C/qlyw.c index 1f8e41dca..ae435c4aa 100644 --- a/C/qlyw.c +++ b/C/qlyw.c @@ -32,6 +32,42 @@ STATIC_PROTO(void RestoreEntries, (PropEntry *, int USES_REGS)); STATIC_PROTO(void CleanCode, (PredEntry * USES_REGS)); +static void +GrowAtomTable(void) { + CACHE_REGS + UInt size = LOCAL_ExportAtomHashTableSize; + export_atom_hash_entry_t *p, *newt, *oldt = LOCAL_ExportAtomHashChain; + UInt new_size = size + (size > 1024 ? size : 1024); + UInt i; + + newt = (export_atom_hash_entry_t *)calloc(new_size,sizeof(export_atom_hash_entry_t)); + if (!newt) { + return; + } + p = oldt; + for (i = 0 ; i < size ; p++,i++) { + Atom a = p->val; + export_atom_hash_entry_t *newp; + CELL hash; + char *apt; + + + if (!a) continue; + apt = RepAtom(a)->StrOfAE; + hash = HashFunction((unsigned char *)apt)/(2*sizeof(CELL)) % new_size; + newp = newt+hash; + while (newp->val) { + newp++; + if (newp == newt+new_size) + newp = newt; + } + newp->val = a; + } + LOCAL_ExportAtomHashChain = newt; + LOCAL_ExportAtomHashTableSize = new_size; + free(oldt); +} + static void LookupAtom(Atom at) { @@ -40,54 +76,99 @@ LookupAtom(Atom at) CELL hash = HashFunction((unsigned char *)p) % LOCAL_ExportAtomHashTableSize; export_atom_hash_entry_t *a; - a = LOCAL_ExportAtomHashChain[hash]; - while (a) { + a = LOCAL_ExportAtomHashChain+hash; + while (a->val) { if (a->val == at) { return; } - a = a->next; - } - a = (export_atom_hash_entry_t *)malloc(sizeof(export_atom_hash_entry_t)); - if (!a) { - return; + a++; + if (a == LOCAL_ExportAtomHashChain+LOCAL_ExportAtomHashTableSize) + a = LOCAL_ExportAtomHashChain; + } a->val = at; - a->next = LOCAL_ExportAtomHashChain[hash]; - LOCAL_ExportAtomHashChain[hash] = a; LOCAL_ExportAtomHashTableNum++; + if (LOCAL_ExportAtomHashTableNum > + LOCAL_ExportAtomHashTableSize/2 + ) { + GrowAtomTable(); + if (!LOCAL_ExportAtomHashChain) { + return; + } + } +} + +static void +GrowFunctorTable(void) { + CACHE_REGS + UInt size = LOCAL_ExportFunctorHashTableSize; + export_functor_hash_entry_t *p, *newt, *oldt = LOCAL_ExportFunctorHashChain; + UInt new_size = size + (size > 1024 ? size : 1024); + UInt i; + + newt = (export_functor_hash_entry_t *)calloc(new_size,sizeof(export_functor_hash_entry_t)); + if (!newt) { + return; + } + p = oldt; + for (i = 0 ; i < size ; p++,i++) { + Functor f = p->val; + export_functor_hash_entry_t *newp; + CELL hash; + + if (!f) continue; + hash = ((CELL)(f))/(2*sizeof(CELL)) % new_size; + newp = newt+hash; + while (newp->val) { + newp++; + if (newp == newt+new_size) + newp = newt; + } + newp->val = p->val; + newp->arity = p->arity; + newp->name = p->name; + } + LOCAL_ExportFunctorHashChain = newt; + LOCAL_ExportFunctorHashTableSize = new_size; + free(oldt); } static void LookupFunctor(Functor fun) { CACHE_REGS - CELL hash = (CELL)(fun) % LOCAL_ExportFunctorHashTableSize; + CELL hash = ((CELL)(fun))/(2*sizeof(CELL)) % LOCAL_ExportFunctorHashTableSize; export_functor_hash_entry_t *f; Atom name = NameOfFunctor(fun); UInt arity = ArityOfFunctor(fun); - f = LOCAL_ExportFunctorHashChain[hash]; - while (f) { - if (f->name == name && f->arity == arity) { + f = LOCAL_ExportFunctorHashChain+hash; + while (f->val) { + if (f->val == fun) { return; } - f = f->next; - } - f = (export_functor_hash_entry_t *)malloc(sizeof(export_functor_hash_entry_t)); - if (!f) { - return; + f++; + if (f == LOCAL_ExportFunctorHashChain+LOCAL_ExportFunctorHashTableSize) + f = LOCAL_ExportFunctorHashChain; } LookupAtom(name); f->val = fun; f->name = name; f->arity = arity; - f->next = LOCAL_ExportFunctorHashChain[hash]; - LOCAL_ExportFunctorHashChain[hash] = f; LOCAL_ExportFunctorHashTableNum++; + if (LOCAL_ExportFunctorHashTableNum > + LOCAL_ExportFunctorHashTableSize/2 + ) { + GrowFunctorTable(); + if (!LOCAL_ExportFunctorHashChain) { + return; + } + } } static void GrowPredTable(void) { + CACHE_REGS UInt size = LOCAL_ExportPredEntryHashTableSize; export_pred_entry_hash_entry_t *p, *newt, *oldt = LOCAL_ExportPredEntryHashChain; UInt new_size = size + (size > 1024 ? size : 1024); @@ -178,31 +259,71 @@ LookupPredEntry(PredEntry *pe) } } + +static void +GrowDBRefTable(void) { + CACHE_REGS + UInt size = LOCAL_ExportDBRefHashTableSize; + export_dbref_hash_entry_t *p, *newt, *oldt = LOCAL_ExportDBRefHashChain; + UInt new_size = size + (size > 1024 ? size : 1024); + UInt i; + + newt = (export_dbref_hash_entry_t *)calloc(new_size,sizeof(export_dbref_hash_entry_t)); + if (!newt) { + return; + } + p = oldt; + for (i = 0 ; i < size ; p++,i++) { + DBRef dbr = p->val; + export_dbref_hash_entry_t *newp; + CELL hash; + + if (!dbr) continue; + hash = ((CELL)(dbr))/(2*sizeof(CELL)) % new_size; + newp = newt+hash; + while (newp->val) { + newp++; + if (newp == newt+new_size) + newp = newt; + } + newp->val = p->val; + newp->sz = p->sz; + newp->refs = p->refs; + } + LOCAL_ExportDBRefHashChain = newt; + LOCAL_ExportDBRefHashTableSize = new_size; + free(oldt); +} + static void LookupDBRef(DBRef ref) { CACHE_REGS - CELL hash = Unsigned(ref) % LOCAL_ExportDBRefHashTableSize; + CELL hash = ((CELL)(ref))/(2*sizeof(CELL)) % LOCAL_ExportDBRefHashTableSize; export_dbref_hash_entry_t *a; - a = LOCAL_ExportDBRefHashChain[hash]; - while (a) { + a = LOCAL_ExportDBRefHashChain+hash; + while (a->val) { if (a->val == ref) { a->refs++; return; } - a = a->next; - } - a = (export_dbref_hash_entry_t *)malloc(sizeof(export_dbref_hash_entry_t)); - if (!a) { - return; + a++; + if (a == LOCAL_ExportDBRefHashChain+LOCAL_ExportDBRefHashTableSize) + a = LOCAL_ExportDBRefHashChain; } a->val = ref; a->sz = ((LogUpdClause *)ref)->ClSize; a->refs = 1; - a->next = LOCAL_ExportDBRefHashChain[hash]; - LOCAL_ExportDBRefHashChain[hash] = a; LOCAL_ExportDBRefHashTableNum++; + if (LOCAL_ExportDBRefHashTableNum > + LOCAL_ExportDBRefHashTableSize/2 + ) { + GrowDBRefTable(); + if (!LOCAL_ExportDBRefHashChain) { + return; + } + } } static void @@ -211,16 +332,16 @@ InitHash(void) CACHE_REGS LOCAL_ExportFunctorHashTableNum = 0; LOCAL_ExportFunctorHashTableSize = EXPORT_FUNCTOR_TABLE_SIZE; - LOCAL_ExportFunctorHashChain = (export_functor_hash_entry_t **)calloc(1, sizeof(export_functor_hash_entry_t *)* LOCAL_ExportFunctorHashTableSize); + LOCAL_ExportFunctorHashChain = (export_functor_hash_entry_t *)calloc(LOCAL_ExportFunctorHashTableSize, sizeof(export_functor_hash_entry_t )); LOCAL_ExportAtomHashTableNum = 0; LOCAL_ExportAtomHashTableSize = EXPORT_ATOM_TABLE_SIZE; - LOCAL_ExportAtomHashChain = (export_atom_hash_entry_t **)calloc(1, sizeof(export_atom_hash_entry_t *)* LOCAL_ExportAtomHashTableSize); + LOCAL_ExportAtomHashChain = (export_atom_hash_entry_t *)calloc( LOCAL_ExportAtomHashTableSize, sizeof(export_atom_hash_entry_t *)); LOCAL_ExportPredEntryHashTableNum = 0; LOCAL_ExportPredEntryHashTableSize = EXPORT_PRED_ENTRY_TABLE_SIZE; LOCAL_ExportPredEntryHashChain = (export_pred_entry_hash_entry_t *)calloc(LOCAL_ExportPredEntryHashTableSize, sizeof(export_pred_entry_hash_entry_t)); LOCAL_ExportDBRefHashTableNum = 0; LOCAL_ExportDBRefHashTableSize = EXPORT_DBREF_TABLE_SIZE; - LOCAL_ExportDBRefHashChain = (export_dbref_hash_entry_t **)calloc(1, sizeof(export_dbref_hash_entry_t *)* LOCAL_ExportDBRefHashTableSize); + LOCAL_ExportDBRefHashChain = (export_dbref_hash_entry_t *)calloc(EXPORT_DBREF_TABLE_SIZE, sizeof(export_dbref_hash_entry_t)); } static void @@ -449,9 +570,8 @@ SaveHash(IOSTREAM *stream) CHECK(save_tag(stream, QLY_START_ATOMS)); CHECK(save_uint(stream, LOCAL_ExportAtomHashTableNum)); for (i = 0; i < LOCAL_ExportAtomHashTableSize; i++) { - export_atom_hash_entry_t *a = LOCAL_ExportAtomHashChain[i]; - while (a) { - export_atom_hash_entry_t *a0 = a; + export_atom_hash_entry_t *a = LOCAL_ExportAtomHashChain+i; + if (a->val) { Atom at = a->val; CHECK(save_uint(stream, (UInt)at)); if (IsWideAtom(at)) { @@ -463,22 +583,17 @@ SaveHash(IOSTREAM *stream) CHECK(save_uint(stream, strlen(RepAtom(at)->StrOfAE))); CHECK(save_bytes(stream, at->StrOfAE, (strlen(at->StrOfAE)+1)*sizeof(char))); } - a = a->next; - free(a0); } } save_tag(stream, QLY_START_FUNCTORS); save_uint(stream, LOCAL_ExportFunctorHashTableNum); for (i = 0; i < LOCAL_ExportFunctorHashTableSize; i++) { - export_functor_hash_entry_t *f = LOCAL_ExportFunctorHashChain[i]; - while (f) { - export_functor_hash_entry_t *f0 = f; - CHECK(save_uint(stream, (UInt)(f->val))); - CHECK(save_uint(stream, f->arity)); - CHECK(save_uint(stream, (CELL)(f->name))); - f = f->next; - free(f0); - } + export_functor_hash_entry_t *f = LOCAL_ExportFunctorHashChain+i; + if (!(f->val)) + continue; + CHECK(save_uint(stream, (UInt)(f->val))); + CHECK(save_uint(stream, f->arity)); + CHECK(save_uint(stream, (CELL)(f->name))); } save_tag(stream, QLY_START_PRED_ENTRIES); save_uint(stream, LOCAL_ExportPredEntryHashTableNum); @@ -494,14 +609,11 @@ SaveHash(IOSTREAM *stream) save_tag(stream, QLY_START_DBREFS); save_uint(stream, LOCAL_ExportDBRefHashTableNum); for (i = 0; i < LOCAL_ExportDBRefHashTableSize; i++) { - export_dbref_hash_entry_t *p = LOCAL_ExportDBRefHashChain[i]; - while (p) { - export_dbref_hash_entry_t *p0 = p; + export_dbref_hash_entry_t *p = LOCAL_ExportDBRefHashChain+i; + if (p->val) { CHECK(save_uint(stream, (UInt)(p->val))); CHECK(save_uint(stream, p->sz)); CHECK(save_uint(stream, p->refs)); - p = p->next; - free(p0); } } save_tag(stream, QLY_FAILCODE); diff --git a/H/hlocals.h b/H/hlocals.h index 92243152b..ee4d4179d 100644 --- a/H/hlocals.h +++ b/H/hlocals.h @@ -196,16 +196,16 @@ typedef struct worker_local { int do_trace_primitives_; #endif - struct export_atom_hash_entry_struct **ExportAtomHashChain_; + struct export_atom_hash_entry_struct *ExportAtomHashChain_; UInt ExportAtomHashTableSize_; UInt ExportAtomHashTableNum_; - struct export_functor_hash_entry_struct **ExportFunctorHashChain_; + struct export_functor_hash_entry_struct *ExportFunctorHashChain_; UInt ExportFunctorHashTableSize_; UInt ExportFunctorHashTableNum_; struct export_pred_entry_hash_entry_struct *ExportPredEntryHashChain_; UInt ExportPredEntryHashTableSize_; UInt ExportPredEntryHashTableNum_; - struct export_dbref_hash_entry_struct **ExportDBRefHashChain_; + struct export_dbref_hash_entry_struct *ExportDBRefHashChain_; UInt ExportDBRefHashTableSize_; UInt ExportDBRefHashTableNum_; struct import_atom_hash_entry_struct **ImportAtomHashChain_; diff --git a/H/qly.h b/H/qly.h index a004719e0..379e8b565 100644 --- a/H/qly.h +++ b/H/qly.h @@ -24,7 +24,6 @@ typedef struct export_atom_hash_entry_struct { Atom val; - struct export_atom_hash_entry_struct *next; } export_atom_hash_entry_t; typedef struct import_atom_hash_entry_struct { @@ -37,7 +36,6 @@ typedef struct export_functor_hash_entry_struct { Functor val; Atom name; UInt arity; - struct export_functor_hash_entry_struct *next; } export_functor_hash_entry_t; typedef struct import_functor_hash_entry_struct { @@ -73,7 +71,6 @@ typedef struct export_dbref_hash_entry_struct { DBRef val; UInt sz; UInt refs; - struct export_dbref_hash_entry_struct *next; } export_dbref_hash_entry_t; typedef struct import_dbref_hash_entry_struct { diff --git a/misc/LOCALS b/misc/LOCALS index 2d162c9b9..f47243e85 100644 --- a/misc/LOCALS +++ b/misc/LOCALS @@ -223,16 +223,16 @@ int do_trace_primitives =TRUE #endif //quick loader -struct export_atom_hash_entry_struct **ExportAtomHashChain =NULL +struct export_atom_hash_entry_struct *ExportAtomHashChain =NULL UInt ExportAtomHashTableSize =0 UInt ExportAtomHashTableNum =0 -struct export_functor_hash_entry_struct **ExportFunctorHashChain =NULL +struct export_functor_hash_entry_struct *ExportFunctorHashChain =NULL UInt ExportFunctorHashTableSize =0 UInt ExportFunctorHashTableNum =0 struct export_pred_entry_hash_entry_struct *ExportPredEntryHashChain =NULL UInt ExportPredEntryHashTableSize =0 UInt ExportPredEntryHashTableNum =0 -struct export_dbref_hash_entry_struct **ExportDBRefHashChain =NULL +struct export_dbref_hash_entry_struct *ExportDBRefHashChain =NULL UInt ExportDBRefHashTableSize =0 UInt ExportDBRefHashTableNum =0 struct import_atom_hash_entry_struct **ImportAtomHashChain =NULL