Optimize Expr::From(double) by statically allocating common constants.
parent
0933bbf687
commit
ef3ee55d42
29
src/expr.cpp
29
src/expr.cpp
|
@ -219,6 +219,35 @@ Expr *Expr::From(hParam p) {
|
|||
}
|
||||
|
||||
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();
|
||||
r->op = CONSTANT;
|
||||
r->x.v = v;
|
||||
|
|
|
@ -63,6 +63,9 @@ public:
|
|||
char c;
|
||||
} x;
|
||||
|
||||
Expr() { }
|
||||
Expr(double v) : op(CONSTANT) { x.v = v; }
|
||||
|
||||
static inline Expr *AllocExpr(void)
|
||||
{ return (Expr *)AllocTemporary(sizeof(Expr)); }
|
||||
|
||||
|
|
Loading…
Reference in New Issue