Skip to content

Commit

Permalink
Inline optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimZhestikov committed Nov 8, 2024
1 parent 457909e commit 3810cb3
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 359 deletions.
85 changes: 0 additions & 85 deletions src/njs_atom.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,88 +136,3 @@ njs_atom_hash_init()

return;
};


/*
* value is always key: string or number or symbol.
*
* symbol always contians atom_id by construction. do nothing;
* number if short number it is atomized by "| 0x80000000";
* string if represents short number it is atomized by "| 0x80000000";
*
* for string and symbol atom_ids common range is uint32_t < 0x80000000.
*/

njs_int_t
njs_atom_atomize_key(njs_vm_t *vm, njs_value_t *value)
{
double num;
uint32_t hash_id;
njs_int_t ret;
njs_value_t val_str;
const njs_value_t *entry;

switch (value->type) {
case NJS_STRING:
num = njs_key_to_index(value);
if (njs_fast_path(njs_key_is_integer_index(num, value)) &&
((uint32_t) num) < 0x80000000)
{
value->atom_id = ((uint32_t) num) | 0x80000000;

} else {
hash_id = njs_djb_hash(value->string.data->start,
value->string.data->size);

entry = njs_lexer_keyword_find(vm, value->string.data->start,
value->string.data->size,
value->string.data->length,
hash_id);
if (njs_slow_path(entry == NULL)) {
return NJS_ERROR;
}

/* TODO: if (<<value is string>>) <<try release>>(string) */
*value = *entry;
}
break;

case NJS_NUMBER:
num = value->data.u.number;
if (njs_fast_path(njs_key_is_integer_index(num, value)) &&
((uint32_t) num) < 0x80000000)
{
value->atom_id = ((uint32_t) num) | 0x80000000;

} else {
/* convert num to string, and atomize it. */
ret = njs_number_to_string(vm, &val_str, value);
if (ret != NJS_OK) {
return ret;
}

if (val_str.atom_id == 0) {
hash_id = njs_djb_hash(val_str.string.data->start,
val_str.string.data->size);

entry = njs_lexer_keyword_find(vm, val_str.string.data->start,
val_str.string.data->size,
val_str.string.data->length,
hash_id);
if (njs_slow_path(entry == NULL)) {
return NJS_ERROR;
}

value->atom_id = entry->atom_id;

} else {
value->atom_id = val_str.atom_id;
}
}
break;
default:
/* NJS_SYMBOL: do nothing. */
}

return NJS_OK;
}
1 change: 0 additions & 1 deletion src/njs_atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ typedef struct {


void njs_atom_hash_init(void);
njs_int_t njs_atom_atomize_key(njs_vm_t *vm, njs_value_t *value);


extern njs_atom_values_t njs_atom;
Expand Down
26 changes: 2 additions & 24 deletions src/njs_flathsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,9 @@
#define NJS_FLATHSH_ELTS_MINIMUM_TO_SHRINK 8


struct njs_flathsh_descr_s {
uint32_t hash_mask;
uint32_t elts_size; /* allocated properties */
uint32_t elts_count; /* include deleted properties */
uint32_t elts_deleted_count;
};


static njs_flathsh_descr_t *njs_flathsh_alloc(njs_flathsh_query_t *fhq,
size_t hash_size, size_t elts_size);
static njs_flathsh_descr_t *njs_expand_elts(njs_flathsh_query_t *fhq,
njs_flathsh_descr_t *njs_expand_elts(njs_flathsh_query_t *fhq,
njs_flathsh_descr_t *h);


Expand Down Expand Up @@ -130,27 +122,13 @@ njs_flathsh_descr(void *chunk, size_t hash_size)
}


njs_inline uint32_t *
njs_hash_cells_end(njs_flathsh_descr_t *h)
{
return (uint32_t *) h;
}


njs_inline void *
njs_flathsh_chunk(njs_flathsh_descr_t *h)
{
return njs_hash_cells_end(h) - ((njs_int_t) h->hash_mask + 1);
}


njs_inline njs_flathsh_elt_t *
njs_hash_elts(njs_flathsh_descr_t *h)
{
return (njs_flathsh_elt_t *) ((char *) h + sizeof(njs_flathsh_descr_t));
}


/*
* Create a new empty flat hash.
*/
Expand Down Expand Up @@ -236,7 +214,7 @@ njs_flathsh_add_elt(njs_flathsh_t *fh, njs_flathsh_query_t *fhq)
}


static njs_flathsh_descr_t *
njs_flathsh_descr_t *
njs_expand_elts(njs_flathsh_query_t *fhq, njs_flathsh_descr_t *h)
{
void *chunk;
Expand Down
24 changes: 24 additions & 0 deletions src/njs_flathsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ typedef void (*njs_flathsh_free_t)(void *ctx, void *p, size_t size);
typedef struct njs_flathsh_proto_s njs_flathsh_proto_t;


struct njs_flathsh_descr_s {
uint32_t hash_mask;
uint32_t elts_size; /* allocated properties */
uint32_t elts_count; /* include deleted properties */
uint32_t elts_deleted_count;
};


struct njs_flathsh_proto_s {
uint32_t not_used;
njs_flathsh_test_t test;
Expand Down Expand Up @@ -64,6 +72,20 @@ struct njs_flathsh_query_s {
((fhl)->slot == (fhr)->slot)


njs_inline uint32_t *
njs_hash_cells_end(njs_flathsh_descr_t *h)
{
return (uint32_t *) h;
}


njs_inline njs_flathsh_elt_t *
njs_hash_elts(njs_flathsh_descr_t *h)
{
return (njs_flathsh_elt_t *) ((char *) h + sizeof(njs_flathsh_descr_t));
}


/*
* njs_flathsh_find() finds a hash element. If the element has been
* found then it is stored in the fhq->value and njs_flathsh_find()
Expand Down Expand Up @@ -128,6 +150,8 @@ NJS_EXPORT njs_flathsh_elt_t *njs_flathsh_add_elt(njs_flathsh_t *fh,

NJS_EXPORT njs_flathsh_descr_t *njs_flathsh_new(njs_flathsh_query_t *fhq);
NJS_EXPORT void njs_flathsh_destroy(njs_flathsh_t *fh, njs_flathsh_query_t *fhq);
NJS_EXPORT njs_flathsh_descr_t * njs_expand_elts(njs_flathsh_query_t *fhq,
njs_flathsh_descr_t *h);

NJS_EXPORT njs_int_t njs_flathsh_alloc_copy(njs_mp_t *mp, njs_flathsh_t *to,
njs_flathsh_t *from);
Expand Down
Loading

0 comments on commit 3810cb3

Please sign in to comment.