From 78764ec47ccd71318e8524b140a6fc502474a0b0 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 22 Apr 2023 12:36:32 +0300 Subject: [PATCH] move SLICE source refcount incrementing outside of loop --- src/builtins/cells.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/builtins/cells.c b/src/builtins/cells.c index 5cf93ee5..fbc15234 100644 --- a/src/builtins/cells.c +++ b/src/builtins/cells.c @@ -10,7 +10,10 @@ B transp_c2(B, B, B); B fold_rows(Md1D* d, B x); // from fold.c B takedrop_highrank(bool take, B w, B x); // from sfns.c -#define S_KSLICES(X, XSH, K) \ +// X - variable name; XSH - its shape; K - number of leading axes that get iterated over; SLN - number of slices that will be made; DX - additional refcount count to add to x +#define S_KSLICES(X, XSH, K, SLN, DX)\ + usz X##_sn = (SLN); \ + assert(X##_sn>0); \ usz X##_k = (K); \ usz X##_cr = RNK(X)-X##_k; \ usz X##_csz = 1; \ @@ -18,6 +21,7 @@ B takedrop_highrank(bool take, B w, B x); // from sfns.c if (LIKELY(X##_cr >= 1)) { \ if (RARE(X##_cr > 1)) { \ X##_csh = m_shArr(X##_cr); \ + ptr_incBy(X##_csh, X##_sn-1); \ PLAINLOOP for (usz i = 0; i < X##_cr; i++) { \ usz v = XSH[i+X##_k]; \ X##_csz*= v; \ @@ -25,17 +29,19 @@ B takedrop_highrank(bool take, B w, B x); // from sfns.c } \ } else X##_csz = XSH[X##_k]; \ } \ - BSS2A X##_slc = TI(X,slice); + BSS2A X##_slc = TI(X,slice); \ + incBy(X, (i64)X##_sn + ((i64)DX-1)); -#define S_SLICES(X) usz* X##_sh = SH(X); S_KSLICES(X, X##_sh, 1) -#define SLICE(X, S) taga(arr_shSetI(X##_slc(incG(X), S, X##_csz), X##_cr, X##_csh)) -#define E_SLICES(X) if (X##_cr>1) ptr_dec(X##_csh); decG(X); +#define S_SLICES(X, SLN) usz* X##_sh = SH(X); S_KSLICES(X, X##_sh, 1, SLN, 0) +#define SLICE(X, S) taga(arr_shSetU(X##_slc(X, S, X##_csz), X##_cr, X##_csh)) +#define E_SLICES(X) // Used by Insert in fold.c B insert_base(B f, B x, usz xia, bool has_w, B w) { - S_SLICES(x) + assert(isArr(x) && RNK(x)>0); + S_SLICES(x, *x_sh) usz p = xia; B r = w; if (!has_w) { @@ -71,7 +77,7 @@ NOINLINE B toCells(B x) { for (usz i=0,p=0; i 1); for (usz i=0,p=0; i1) shcpy(rsh, xsh, k); - E_SLICES(x); + decG(x); return bqn_merge(HARR_O(r).b); } @@ -437,7 +443,7 @@ B cell_c2(Md1D* d, B w, B x) { B f = d->f; if (rtid==n_drop && xr>1 && isF64(w)) return takedrop_highrank(0, m_hVec2(m_f64(0), w), x); if (rtid==n_transp && q_usz(w)) { usz a=o2sG(w); if (af; } else if (xr==0) { usz cam = SH(w)[0]; if (cam==0) return cell2_empty(f, w, x, wr, xr); - S_SLICES(w) + S_SLICES(w, cam) M_HARR(r, cam); for (usz i=0,p=0; if; if (!q_N(r)) { decG(w); decG(x); return r; } } } - S_SLICES(w) S_SLICES(x) + S_SLICES(w, cam) S_SLICES(x, cam) M_HARR(r, cam); for (usz i=0,wp=0,xp=0; i