optimize mm_allocS

This commit is contained in:
dzaima 2021-11-30 17:13:07 +02:00
parent f81b680aad
commit c8b15987b5

View File

@ -17,23 +17,29 @@ AllocInfo* al;
u64 alCap; u64 alCap;
u64 alSize; u64 alSize;
static inline void BN(guaranteeEmpty)(u8 bucket) { FORCE_INLINE void BN(splitTo)(EmptyValue* c, i64 from, i64 to, bool notEqual) {
u8 cb = bucket; c->mmInfo = MMI(to);
EmptyValue* c; #ifdef __clang__
while (true) { #pragma clang loop unroll(disable) // at least n repetitions happen with probability 2^-n, so unrolling is kind of stupid
cb++; #endif
if (buckets[cb]) { while (from != to) {
c = buckets[cb]; from--;
assert((c->mmInfo&63)==cb); EmptyValue* b = (EmptyValue*) (BSZ(from) + (u8*)c);
buckets[cb] = c->next; b->type = t_empty;
break; b->mmInfo = MMI(from);
b->next = buckets[from];
buckets[from] = b;
} }
if (cb >= ALSZ) { c->next = buckets[from];
u64 sz = BSZ(cb); buckets[from] = c;
}
static NOINLINE void* BN(allocateMore)(i64 bucket, u8 type, i64 from, i64 to) {
u64 sz = BSZ(from);
if (mm_heapAlloc+sz >= mm_heapMax) { printf("Heap size limit reached\n"); exit(1); } if (mm_heapAlloc+sz >= mm_heapMax) { printf("Heap size limit reached\n"); exit(1); }
mm_heapAlloc+= sz; mm_heapAlloc+= sz;
// gc_maybeGC(); // gc_maybeGC();
c = MMAP(sz); EmptyValue* c = MMAP(sz);
#ifdef USE_VALGRIND #ifdef USE_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED(c, sz); VALGRIND_MAKE_MEM_UNDEFINED(c, sz);
#endif #endif
@ -44,25 +50,27 @@ static inline void BN(guaranteeEmpty)(u8 bucket) {
al[BN(alSize)++] = (AllocInfo){.p = (Value*)c, .sz = sz}; al[BN(alSize)++] = (AllocInfo){.p = (Value*)c, .sz = sz};
if (c==MAP_FAILED) { printf("Failed to allocate memory\n"); exit(1); } if (c==MAP_FAILED) { printf("Failed to allocate memory\n"); exit(1); }
c->type = t_empty; c->type = t_empty;
c->mmInfo = cb; c->mmInfo = from;
c->next = 0; c->next = 0;
BN(splitTo)(c, from, to, false);
return BN(allocL)(bucket, type);
}
NOINLINE void* BN(allocS)(i64 bucket, u8 type) {
i64 to = bucket&63;
i64 from = to;
EmptyValue* c;
while (true) {
from++;
if (buckets[from]) {
c = buckets[from];
assert((c->mmInfo&63)==from);
buckets[from] = c->next;
break; break;
} }
if (from >= ALSZ) return BN(allocateMore)(bucket, type, from, to);
} }
c->mmInfo = MMI(bucket); BN(splitTo)(c, from, to, true);
while (cb != bucket) {
cb--;
EmptyValue* b = (EmptyValue*) (BSZ(cb) + (u8*)c);
b->type = t_empty;
b->mmInfo = MMI(cb);
b->next = buckets[cb];
buckets[cb] = b;
}
c->next = buckets[cb];
buckets[cb] = c;
}
NOINLINE void* BN(allocS)(i64 bucket, u8 type) {
BN(guaranteeEmpty)(bucket&63);
return BN(allocL)(bucket, type); return BN(allocL)(bucket, type);
} }