in-place ∾ for fillarrs & fillslices

This commit is contained in:
dzaima 2023-05-20 14:03:34 +03:00
parent bc19e7372d
commit 1e22b48225
2 changed files with 44 additions and 2 deletions

View File

@ -99,6 +99,37 @@ B vec_join(B w, B x) {
}
NOINLINE JoinFillslice fillslice_getJoin(B w, B x, usz ria) {
usz wia = IA(w);
assert(TY(w)==t_fillslice);
FillSlice* s = c(FillSlice,w);
if (!fillEqualsGetFill(s->fill, x)) goto no;
Arr* p = s->p;
if (p->refc!=1) goto no;
if (p->type!=t_fillarr && p->type!=t_harr) goto no;
void* rp = p->type==t_fillarr? fillarr_ptr(p) : ((HArr*)p)->a;
if (rp != s->a) goto no;
if (PIA(p)!=wia) goto no;
usz wsz = mm_sizeUsable((Value*)p);
if (!(p->type==t_fillarr? fsizeof(FillArr,a,B,ria)<wsz : fsizeof(HArr,a,B,ria)<wsz)) goto no;
ur sr = PRNK(s);
if (sr<=1) {
arr_shErase(p, sr);
} else {
usz* ssh = PSH(s);
if (ssh != PSH(p)) arr_shReplace(p, sr, ptr_inc(shObjS(ssh)));
}
B w2 = taga(ptr_inc(p));
value_free((Value*)s);
return (JoinFillslice){w2, rp};
no: return (JoinFillslice){m_f64(0), NULL};
}
#define CHK(REQ,NEL,N,...) if (!(REQ)) mut_to(m, NEL); m->fns->m_##N##G(m, __VA_ARGS__);
#define CHK_S(REQ,X,N,...) CHK(REQ, el_or(m->fns->elType, selfElType(X)), N, __VA_ARGS__)

View File

@ -152,9 +152,13 @@ SHOULD_INLINE void bit_cpy(u64* r, usz rs, u64* x, usz xs, usz l) {
typedef struct { B w2; void* rp; } JoinFillslice;
JoinFillslice fillslice_getJoin(B w, B x, usz ria); // either returns NULL in rp, or consumes w
// if `consume==true`, consumes w,x and expects both args to be vectors
// else, doesn't consume x, and decrements refcount of w iif *reusedW (won't free because the result will be w)
FORCE_INLINE B arr_join_inline(B w, B x, bool consume, bool* reusedW) {
assert(isArr(w) && isArr(x));
usz wia = IA(w);
usz xia = IA(x);
u64 ria = wia+xia;
@ -162,7 +166,6 @@ FORCE_INLINE B arr_join_inline(B w, B x, bool consume, bool* reusedW) {
u64 wsz = mm_sizeUsable(v(w));
u8 wt = TY(w);
u8 we = TI(w, elType);
// TODO f64∾i32, i32∾i8, c32∾c8 etc
void* rp = tyany_ptr(w);
switch (wt) {
case t_bitarr: if (BITARR_SZ( ria)<wsz && TI(x,elType)==el_bit) goto yes; break;
@ -173,7 +176,15 @@ FORCE_INLINE B arr_join_inline(B w, B x, bool consume, bool* reusedW) {
case t_c8arr: if (TYARR_SZ(C8, ria)<wsz && TI(x,elType)==el_c8 ) goto yes; break;
case t_c16arr: if (TYARR_SZ(C16,ria)<wsz && TI(x,elType)<=el_c16 && TI(x,elType)>=el_c8) goto yes; break;
case t_c32arr: if (TYARR_SZ(C32,ria)<wsz && TI(x,elType)<=el_c32 && TI(x,elType)>=el_c8) goto yes; break;
case t_harr: if (fsizeof(HArr,a,B,ria)<wsz) { rp = harr_ptr(w); goto yes; } break;
case t_fillslice: {
JoinFillslice t = fillslice_getJoin(w, x, ria);
if (t.rp==NULL) goto no;
w = t.w2;
rp = t.rp;
goto yes;
}
case t_fillarr: if (fsizeof(FillArr,a,B,ria)<wsz) { rp = fillarr_ptr(a(w)); if (fillEqualsGetFill(c(FillArr,x)->fill, x)) goto yes; } break;
case t_harr: if (fsizeof(HArr, a,B,ria)<wsz) { rp = harr_ptr( w ); goto yes; } break;
}
no:; // failed to reuse
MAKE_MUT_INIT(r, ria, el_or(TI(w,elType), TI(x,elType)));