Optimize Expr::From(double) by statically allocating common constants.

pull/4/head
EvilSpirit 2015-10-14 22:26:19 +06:00 committed by whitequark
parent 0933bbf687
commit ef3ee55d42
2 changed files with 32 additions and 0 deletions

View File

@ -219,6 +219,35 @@ Expr *Expr::From(hParam p) {
} }
Expr *Expr::From(double v) { Expr *Expr::From(double v) {
// Statically allocate common constants.
// Note: this is only valid because AllocExpr() uses AllocTemporary(),
// and Expr* is never explicitly freed.
if(v == 0.0) {
static Expr zero(0.0);
return &zero;
}
if(v == 1.0) {
static Expr one(1.0);
return &one;
}
if(v == -1.0) {
static Expr mone(-1.0);
return &mone;
}
if(v == 0.5) {
static Expr half(0.5);
return ½
}
if(v == -0.5) {
static Expr mhalf(-0.5);
return &mhalf;
}
Expr *r = AllocExpr(); Expr *r = AllocExpr();
r->op = CONSTANT; r->op = CONSTANT;
r->x.v = v; r->x.v = v;

View File

@ -63,6 +63,9 @@ public:
char c; char c;
} x; } x;
Expr() { }
Expr(double v) : op(CONSTANT) { x.v = v; }
static inline Expr *AllocExpr(void) static inline Expr *AllocExpr(void)
{ return (Expr *)AllocTemporary(sizeof(Expr)); } { return (Expr *)AllocTemporary(sizeof(Expr)); }