optimize v_get, v_set, and a bit of fills

This commit is contained in:
dzaima 2021-05-27 19:00:08 +03:00
parent 752a68cb2d
commit d9110e8b5a
3 changed files with 41 additions and 30 deletions

View File

@ -13,26 +13,27 @@ void validateFill(B x);
bool fillEqual(B w, B x); bool fillEqual(B w, B x);
B withFill(B x, B fill); // consumes both B withFill(B x, B fill); // consumes both
static B qWithFill(B x, B fill) { // consumes both static B qWithFill(B x, B fill) { // consumes both
if (noFill(fill)) return x; assert(isArr(x));
if (noFill(fill) || TI(x).elType!=el_B) return x;
return withFill(x, fill); return withFill(x, fill);
} }
static B getFillR(B x) { // doesn't consume; can return bi_noFill static B getFillR(B x) { // doesn't consume; can return bi_noFill
if (isArr(x)) { if (isArr(x)) {
u8 xe = TI(x).elType;
if (xe<=el_f64) return m_f64(0);
if (xe==el_c32) return m_c32(' ');
u8 t = v(x)->type; u8 t = v(x)->type;
if (t==t_fillarr ) { B r = inc(c(FillArr,x )->fill); return r; } if (t==t_fillarr ) return inc(c(FillArr,x )->fill);
if (t==t_fillslice) { B r = inc(c(FillArr,c(Slice,x)->p)->fill); return r; } if (t==t_fillslice) return inc(c(FillArr,c(Slice,x)->p)->fill);
if (t==t_c32arr || t==t_c32slice) return m_c32(' ');
if (t==t_i32arr || t==t_i32slice) return m_f64(0 );
if (t==t_f64arr || t==t_f64slice) return m_f64(0 );
return bi_noFill; return bi_noFill;
} }
if (isF64(x)|isI32(x)) return m_i32(0); if (isF64(x)|isI32(x)) return m_i32(0);
if (isC32(x)) return m_c32(' '); if (isC32(x)) return m_c32(' ');
return bi_noFill; return bi_noFill;
} }
static B getFillQ(B x) { // doesn't consume; can return bi_noFill if CATCH_ERRORS static B getFillQ(B x) { // doesn't consume; returns 0 if !CATCH_ERRORS
B r = getFillR(x); B r = getFillR(x);
#ifdef CATCH_ERRORS #ifdef CATCH_ERRORS
return r; return r;

View File

@ -297,22 +297,26 @@ typedef struct FldAlias {
B obj; B obj;
i32 p; i32 p;
} FldAlias; } FldAlias;
void v_set(Scope* pscs[], B s, B x, bool upd) { // doesn't consume
if (isVar(s)) { static NOINLINE void v_setR(Scope* pscs[], B s, B x, bool upd);
static void v_set(Scope* pscs[], B s, B x, bool upd) { // doesn't consume
if (RARE(!isVar(s))) return v_setR(pscs, s, x, upd);;
Scope* sc = pscs[(u16)(s.u>>32)]; Scope* sc = pscs[(u16)(s.u>>32)];
B prev = sc->vars[(u32)s.u]; B prev = sc->vars[(u32)s.u];
if (upd) { if (upd) {
if (prev.u==bi_noVar.u) thrM("↩: Updating undefined variable"); if (prev.u==bi_noVar.u) thrM("↩: Updating undefined variable");
dec(prev); dec(prev);
} // else if (prev.u!=bi_noVar.u) thrM("←: Redefining variable"); }
sc->vars[(u32)s.u] = inc(x); sc->vars[(u32)s.u] = inc(x);
} else if (isExt(s)) { }
static NOINLINE void v_setR(Scope* pscs[], B s, B x, bool upd) {
if (isExt(s)) {
Scope* sc = pscs[(u16)(s.u>>32)]; Scope* sc = pscs[(u16)(s.u>>32)];
B prev = sc->ext->vars[(u32)s.u]; B prev = sc->ext->vars[(u32)s.u];
if (upd) { if (upd) {
if (prev.u==bi_noVar.u) thrM("↩: Updating undefined variable"); if (prev.u==bi_noVar.u) thrM("↩: Updating undefined variable");
dec(prev); dec(prev);
} // else if (prev.u!=bi_noVar.u) thrM("←: Redefining variable"); }
sc->ext->vars[(u32)s.u] = inc(x); sc->ext->vars[(u32)s.u] = inc(x);
} else { } else {
VTY(s, t_harr); VTY(s, t_harr);
@ -344,13 +348,19 @@ void v_set(Scope* pscs[], B s, B x, bool upd) { // doesn't consume
for (u64 i = 0; i < ia; i++) v_set(pscs, sp[i], xgetU(x,i), upd); for (u64 i = 0; i < ia; i++) v_set(pscs, sp[i], xgetU(x,i), upd);
} }
} }
B v_get(Scope* pscs[], B s) { // get value representing s, replacing with bi_optOut; doesn't consume
if (isVar(s)) {
static NOINLINE B v_getR(Scope* pscs[], B s);
static B v_get(Scope* pscs[], B s) { // get value representing s, replacing with bi_optOut; doesn't consume
if (RARE(!isVar(s))) return v_getR(pscs, s);
Scope* sc = pscs[(u16)(s.u>>32)]; Scope* sc = pscs[(u16)(s.u>>32)];
B r = sc->vars[(u32)s.u]; B r = sc->vars[(u32)s.u];
sc->vars[(u32)s.u] = bi_optOut; sc->vars[(u32)s.u] = bi_optOut;
return r; return r;
} else if (isExt(s)) { }
static NOINLINE B v_getR(Scope* pscs[], B s) {
if (isExt(s)) {
Scope* sc = pscs[(u16)(s.u>>32)]; Scope* sc = pscs[(u16)(s.u>>32)];
B r = sc->ext->vars[(u32)s.u]; B r = sc->ext->vars[(u32)s.u];
sc->ext->vars[(u32)s.u] = bi_optOut; sc->ext->vars[(u32)s.u] = bi_optOut;