Skip to content

Commit

Permalink
Merge pull request #1405 from linas/comment-fixes
Browse files Browse the repository at this point in the history
Assorted fixes for recently introduced issues.
  • Loading branch information
linas authored Feb 4, 2023
2 parents 7951ed8 + 7c6c1d6 commit f64048b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions link-grammar/parse/count.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ struct count_context_s
/* Wikipedia claims that load factors of 0.6 to 0.75 are OK, and that
* a load factor of less than 0.25 is pointless. We set it to 0.333.
*/
#define INV_LOAD_FACTOR 3 /* 1.0 / load factor. */
#define INV_LOAD_FACTOR 3 /* One divided by load factor. */

/* Avoid pathological cases leading to failure */
#define MAX_LOG2_TABLE_SIZE ((sizeof(size_t)==4) ? 25 : 34)

/**
* Provide an estimate for the number of Table_tracon entries that will
Expand Down Expand Up @@ -195,8 +198,6 @@ static void table_alloc(count_context_t *ctxt, unsigned int logsz)
size_t reqsz = 1ULL << logsz;
if (0 < logsz && reqsz <= ctxt->table_size) return; // It's big enough, already.

lgdebug(+D_COUNT, "Connector table size %lu\n", reqsz);

#if HAVE_THREADS_H && !__EMSCRIPTEN__
// Install a thread-exit handler, to free kept_table on thread-exit.
static once_flag flag = ONCE_FLAG_INIT;
Expand All @@ -211,6 +212,11 @@ static void table_alloc(count_context_t *ctxt, unsigned int logsz)
else
ctxt->table_size = reqsz;

if ((1ULL << MAX_LOG2_TABLE_SIZE) <= ctxt->table_size)
ctxt->table_size = (1ULL << MAX_LOG2_TABLE_SIZE);

lgdebug(+D_COUNT, "Tracon table size %lu\n", ctxt->table_size);

/* Keep the table indefinitely (until thread-exit), so that it can
* be reused. This avoids a large overhead in malloc/free when
* large memory blocks are allocated. Large blocks in Linux trigger
Expand Down Expand Up @@ -470,6 +476,14 @@ static void table_stat(count_context_t *ctxt)

static void table_grow(count_context_t *ctxt)
{
// If we somehow hit the max size, disallow further growth.
// Reset the `table_available_count` so we don't come here again.
if ((1ULL << MAX_LOG2_TABLE_SIZE) <= ctxt->table_size)
{
ctxt->table_available_count = UINT_MAX;
return;
}

table_alloc(ctxt, 0);

/* Rehash. */
Expand Down Expand Up @@ -1631,16 +1645,16 @@ count_context_t * alloc_count_context(Sentence sent, Tracon_sharing *ts)
* lower bound is 12 times smaller. So, start with that. If
* this isn't enough, each new alloc will grab another chunk
* of this size. Thus, a typical sentence will expand the
* tracon pool six times. (... Assuming we're not reusing
* them. Otherwise, the delta becomes whatever size the first
* sentence came up with. Hmmmm ... Maybe this size should be
* hard-coded? Maybe 16K is an OK starter size?)
* tracon pool six times. But this logic holds only if a new
* pool is allocated each time. When re-using, with sentences
* of highly variable sizes, if seems best to fix this value.
*
* size_t num_elts = estimate_tracon_entries(sent);
* num_elts /= 12;
*/
size_t num_elts = estimate_tracon_entries(sent);
num_elts /= 12;
sent->Table_tracon_pool =
pool_new(__func__, "Table_tracon",
num_elts, sizeof(Table_tracon),
16382 /* num_elts */, sizeof(Table_tracon),
/*zero_out*/false, /*align*/false, /*exact*/false);
}

Expand Down

0 comments on commit f64048b

Please sign in to comment.