Skip to content

Commit 1430356

Browse files
Inline optimizations.
Performance (bench4.js) for reference: after this patch: Richards: 686 Crypto: 1028 Splay: 2023 NavierStokes: 1652 ---- Score (version 7): 1239
1 parent 3acfd2f commit 1430356

10 files changed

+548
-382
lines changed

src/njs_atom.c

-114
Original file line numberDiff line numberDiff line change
@@ -131,117 +131,3 @@ njs_atom_hash_init(njs_vm_t *vm)
131131

132132
return NJS_OK;
133133
};
134-
135-
136-
/*
137-
* value is always key: string or number or symbol.
138-
*
139-
* symbol always contians atom_id by construction. do nothing;
140-
* number if short number it is atomized by "| 0x80000000";
141-
* string if represents short number it is atomized by "| 0x80000000";
142-
*
143-
* for string and symbol atom_ids common range is uint32_t < 0x80000000.
144-
*/
145-
146-
njs_int_t
147-
njs_atom_atomize_key(njs_vm_t *vm, njs_value_t *value)
148-
{
149-
double num;
150-
uint32_t hash_id;
151-
njs_int_t ret;
152-
njs_value_t val_str;
153-
const njs_value_t *entry;
154-
155-
switch (value->type) {
156-
case NJS_STRING:
157-
num = njs_key_to_index(value);
158-
if (njs_fast_path(njs_key_is_integer_index(num, value)) &&
159-
((uint32_t) num) < 0x80000000)
160-
{
161-
value->atom_id = ((uint32_t) num) | 0x80000000;
162-
163-
} else {
164-
hash_id = njs_djb_hash(value->string.data->start,
165-
value->string.data->size);
166-
167-
entry = njs_lexer_keyword_find(vm, value->string.data->start,
168-
value->string.data->size,
169-
value->string.data->length,
170-
hash_id);
171-
if (njs_slow_path(entry == NULL)) {
172-
return NJS_ERROR;
173-
}
174-
175-
/* TODO: if (<<value is string>>) <<try release>>(string) */
176-
*value = *entry;
177-
}
178-
break;
179-
180-
case NJS_NUMBER:
181-
num = value->data.u.number;
182-
if (njs_fast_path(njs_key_is_integer_index(num, value)) &&
183-
((uint32_t) num) < 0x80000000)
184-
{
185-
value->atom_id = ((uint32_t) num) | 0x80000000;
186-
187-
} else {
188-
/* convert num to string, and atomize it. */
189-
ret = njs_number_to_string(vm, &val_str, value);
190-
if (ret != NJS_OK) {
191-
return ret;
192-
}
193-
194-
if (val_str.atom_id == 0) {
195-
hash_id = njs_djb_hash(val_str.string.data->start,
196-
val_str.string.data->size);
197-
198-
entry = njs_lexer_keyword_find(vm, val_str.string.data->start,
199-
val_str.string.data->size,
200-
val_str.string.data->length,
201-
hash_id);
202-
if (njs_slow_path(entry == NULL)) {
203-
return NJS_ERROR;
204-
}
205-
206-
value->atom_id = entry->atom_id;
207-
208-
} else {
209-
value->atom_id = val_str.atom_id;
210-
}
211-
}
212-
break;
213-
default:
214-
/* NJS_SYMBOL: do nothing. */
215-
}
216-
217-
return NJS_OK;
218-
}
219-
220-
221-
njs_int_t
222-
njs_atom_atomize_key_s(njs_vm_t *vm, njs_value_t *value)
223-
{
224-
njs_int_t ret;
225-
njs_flathsh_query_t lhq;
226-
227-
lhq.replace = 0;
228-
lhq.proto = &njs_lexer_hash_proto;
229-
lhq.pool = vm->atom_hash_mem_pool;
230-
231-
232-
value->string.atom_id = (*vm->atom_hash_atom_id)++;
233-
234-
if (value->type == NJS_SYMBOL) {
235-
lhq.key_hash = value->string.atom_id;
236-
237-
lhq.value = (void *) value;
238-
239-
ret = njs_flathsh_insert(vm->atom_hash, &lhq);
240-
if (njs_slow_path(ret != NJS_OK)) {
241-
njs_internal_error(vm, "flathsh insert/replace failed");
242-
return NJS_ERROR;
243-
}
244-
}
245-
246-
return NJS_OK;
247-
}

src/njs_atom.h

-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ enum {
4545
#include <njs_atom_map.h>
4646

4747
njs_int_t njs_atom_hash_init(njs_vm_t *vm);
48-
njs_int_t njs_atom_atomize_key(njs_vm_t *vm, njs_value_t *value);
49-
njs_int_t njs_atom_atomize_key_s(njs_vm_t *vm, njs_value_t *value);
5048

5149
extern const njs_atom_values_t njs_atom;
5250
extern const njs_flathsh_proto_t njs_atom_hash_proto;

src/njs_flathsh.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
static njs_flathsh_descr_t *njs_flathsh_alloc(njs_flathsh_query_t *fhq,
8080
size_t hash_size, size_t elts_size);
81-
static njs_flathsh_descr_t *njs_expand_elts(njs_flathsh_query_t *fhq,
81+
njs_flathsh_descr_t *njs_expand_elts(njs_flathsh_query_t *fhq,
8282
njs_flathsh_descr_t *h);
8383

8484

@@ -122,13 +122,6 @@ njs_flathsh_descr(void *chunk, size_t hash_size)
122122
}
123123

124124

125-
njs_inline uint32_t *
126-
njs_hash_cells_end(njs_flathsh_descr_t *h)
127-
{
128-
return (uint32_t *) h;
129-
}
130-
131-
132125
njs_inline void *
133126
njs_flathsh_chunk(njs_flathsh_descr_t *h)
134127
{
@@ -221,7 +214,7 @@ njs_flathsh_add_elt(njs_flathsh_t *fh, njs_flathsh_query_t *fhq)
221214
}
222215

223216

224-
static njs_flathsh_descr_t *
217+
njs_flathsh_descr_t *
225218
njs_expand_elts(njs_flathsh_query_t *fhq, njs_flathsh_descr_t *h)
226219
{
227220
void *chunk;

src/njs_flathsh.h

+17-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ typedef void (*njs_flathsh_free_t)(void *ctx, void *p, size_t size);
2929
typedef struct njs_flathsh_proto_s njs_flathsh_proto_t;
3030

3131

32-
struct njs_flathsh_proto_s {
33-
uint32_t not_used;
34-
njs_flathsh_test_t test;
35-
njs_flathsh_alloc_t alloc;
36-
njs_flathsh_free_t free;
37-
};
38-
39-
4032
struct njs_flathsh_descr_s {
4133
uint32_t hash_mask;
4234
uint32_t elts_size; /* allocated properties */
@@ -45,6 +37,14 @@ struct njs_flathsh_descr_s {
4537
};
4638

4739

40+
struct njs_flathsh_proto_s {
41+
uint32_t not_used;
42+
njs_flathsh_test_t test;
43+
njs_flathsh_alloc_t alloc;
44+
njs_flathsh_free_t free;
45+
};
46+
47+
4848
struct njs_flathsh_query_s {
4949
uint32_t key_hash;
5050
njs_str_t key;
@@ -79,6 +79,13 @@ njs_hash_elts(njs_flathsh_descr_t *h)
7979
}
8080

8181

82+
njs_inline uint32_t *
83+
njs_hash_cells_end(njs_flathsh_descr_t *h)
84+
{
85+
return (uint32_t *) h;
86+
}
87+
88+
8289
/*
8390
* njs_flathsh_find() finds a hash element. If the element has been
8491
* found then it is stored in the fhq->value and njs_flathsh_find()
@@ -143,6 +150,8 @@ NJS_EXPORT njs_flathsh_elt_t *njs_flathsh_add_elt(njs_flathsh_t *fh,
143150

144151
NJS_EXPORT njs_flathsh_descr_t *njs_flathsh_new(njs_flathsh_query_t *fhq);
145152
NJS_EXPORT void njs_flathsh_destroy(njs_flathsh_t *fh, njs_flathsh_query_t *fhq);
153+
NJS_EXPORT njs_flathsh_descr_t * njs_expand_elts(njs_flathsh_query_t *fhq,
154+
njs_flathsh_descr_t *h);
146155

147156
NJS_EXPORT njs_int_t njs_flathsh_alloc_copy(njs_mp_t *mp, njs_flathsh_t *to,
148157
njs_flathsh_t *from);

0 commit comments

Comments
 (0)