Skip to content

Commit 0a55c81

Browse files
committed
demo how to poolize hash memory
1 parent a1fcf11 commit 0a55c81

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
test_ubuntu:
1919
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"
2020
runs-on: ${{ matrix.os }}
21-
env:
22-
ROCKSPEC: rockspecs/lua-protobuf-scm-1.rockspec
2321
strategy:
2422
matrix:
2523
os: ["ubuntu-latest"]

amoeba.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ static int am_writefloat(am_DumpCtx *ctx, am_Num n) {
12651265
if (sizeof(am_Num) == sizeof(float))
12661266
u.f32 = n;
12671267
else
1268-
u.f64 = n, u.f32 = u.f64;
1268+
u.f64 = n, u.f32 = (float)u.f64;
12691269
amE(am_writechar(ctx, 0xCA));
12701270
amE(am_writeraw(ctx, u.u32, 32));
12711271
return AM_OK;

enaml_like_benchmark.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,25 @@ int main() {
219219
am_Solver *S;
220220

221221
// demo how to use a memory pool across solvers.
222-
am_MemPool mempool;
223-
am_initpool(&mempool, sizeof(am_Constraint));
224-
auto alloc = [&mempool](void *ptr, size_t ns, am_AllocType ty) {
222+
am_MemPool conspool; am_initpool(&conspool, sizeof(am_Constraint));
223+
const size_t hash_size = (sizeof(am_Num) + sizeof(am_Key)) * 8;
224+
am_MemPool hashpool; am_initpool(&hashpool, hash_size);
225+
auto alloc = [&](void *ptr, size_t ns, size_t os, am_AllocType ty) {
225226
if (ty == am_AllocConstraint) {
226-
if (ns) return am_poolalloc(&mempool);
227-
return am_poolfree(&mempool, ptr), (void*)0;
227+
if (ns) return am_poolalloc(&conspool);
228+
return am_poolfree(&conspool, ptr), (void*)0;
229+
}
230+
if (ty == am_AllocHash) {
231+
if (ns == hash_size && os == 0) return am_poolalloc(&hashpool);
232+
if (os == hash_size && ns == 0) return am_poolfree(&hashpool, ptr), (void*)0;
228233
}
229234
if (ns) return realloc(ptr, ns);
230235
return free(ptr), (void*)0;
231236
};
232-
auto alloc_func = [](void** pud, void *ptr, size_t ns, size_t, am_AllocType ty) {
237+
auto alloc_func = [](void** pud, void *ptr, size_t ns, size_t os, am_AllocType ty) {
233238
const auto func = &decltype(alloc)::operator();
234239
auto alloc_ptr = *(decltype(alloc)**)(pud);
235-
return (alloc_ptr->*func)(ptr, ns, ty);
240+
return (alloc_ptr->*func)(ptr, ns, os, ty);
236241
};
237242

238243
#if !DISABLE_BUILD
@@ -276,7 +281,7 @@ int main() {
276281
am_delsolver(S);
277282
}
278283

279-
ankerl::nanobench::Bench().minEpochIterations(2000).run("load solver", [&] {
284+
ankerl::nanobench::Bench().minEpochIterations(10000).run("load solver", [&] {
280285
struct MyLoader {
281286
am_Loader base;
282287
am_Num values[37];
@@ -302,7 +307,6 @@ int main() {
302307
am_Solver *S = am_newsolver(alloc_func, &alloc);
303308
int ret = am_load(S, &l.base);
304309
assert(ret == AM_OK), (void)ret;
305-
ankerl::nanobench::doNotOptimizeAway(S); //< prevent the compiler to optimize away the S
306310
am_delsolver(S);
307311
});
308312
#endif /* !DISABLE_LOAD */
@@ -329,8 +333,8 @@ int main() {
329333
build_solver(S, widthVar, heightVar, values);
330334

331335
for (const Size& size : sizes) {
332-
am_Num width = size.width;
333-
am_Num height = size.height;
336+
am_Num width = (am_Num)size.width;
337+
am_Num height = (am_Num)size.height;
334338

335339
ankerl::nanobench::Bench().minEpochIterations(100000).run("suggest value " + std::to_string(size.width) + "x" + std::to_string(size.height), [&] {
336340
am_suggest(S, widthVar, width);
@@ -339,10 +343,14 @@ int main() {
339343
});
340344
}
341345

346+
ankerl::nanobench::doNotOptimizeAway(width);
347+
ankerl::nanobench::doNotOptimizeAway(height);
348+
342349
am_delsolver(S);
343350
#endif /* !DISABLE_SUGGEST */
344351

345-
am_freepool(&mempool);
352+
am_freepool(&conspool);
353+
am_freepool(&hashpool);
346354
return 0;
347355
}
348356

test.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,9 @@ static void build_solver(am_Solver* S, am_Id width, am_Id height, am_Num *values
965965
#ifdef __GNUC__
966966
# pragma GCC diagnostic push
967967
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
968-
# pragma GCC diagnostic ignored "-Wc99-extensions"
968+
# if __STDC_VERSION__ < 199901L
969+
# pragma GCC diagnostic ignored "-Wc99-extensions"
970+
# endif
969971
#endif
970972

971973
/* Add the constraints */
@@ -1207,7 +1209,7 @@ static void test_dumpload(void) {
12071209
printf("after dump: ret=%d\n", ret);
12081210
assert(ret == AM_OK);
12091211
printf("dumpped len=%d\n", (int)len);
1210-
assert(len == 10779);
1212+
assert(len == 10709 || len == 10779);
12111213
}
12121214

12131215
{
@@ -1255,6 +1257,6 @@ int main(void) {
12551257
return 0;
12561258
}
12571259

1258-
/* cc: flags='-ggdb -Wall -fsanitize=address -fprofile-arcs -ftest-coverage -O0 -Wextra -pedantic -std=c89'
1260+
/* cc: flags='-ggdb -Wall -fsanitize=address -fprofile-arcs -ftest-coverage -O3 -Wextra -pedantic -std=c89'
12591261
* cc: run='!rm -f *.gcda; $executable $args' */
12601262

0 commit comments

Comments
 (0)