[Ffmpeg-devel] [PATCH] optimize ff_eval

Oded Shimon ods15
Thu Oct 26 20:45:02 CEST 2006


I took the liberty of optimizing ff_eval for multiple runs by seperating 
the parsing/compiling and runtime steps.

before:
97730 dezicycles in ff_eval, 1023 runs, 1 skips
after:
13643 dezicycles in ff_parse, 1023 runs, 1 skips


What should I do with this? Deperecate or keep old ff_eval? update current 
calls for ff_eval to this new thing?

Patch is very obviously not cleaned up (there isn't even a free function 
yet), just shows what I did...

- ods15
-------------- next part --------------
Index: libavcodec/eval.c
===================================================================
--- libavcodec/eval.c	(revision 6797)
+++ libavcodec/eval.c	(working copy)
@@ -57,8 +57,6 @@
     char **error;
 } Parser;
 
-static double evalExpression(Parser *p);
-
 static int8_t si_prefixes['z' - 'E' + 1]={
     ['y'-'E']= -24,
     ['z'-'E']= -21,
@@ -126,23 +124,64 @@
     return 1;
 }
 
-static double evalPrimary(Parser *p){
-    double d, d2=NAN;
+typedef struct expr_s expr_t;
+struct expr_s {
+    int type;
+    double value; // is sign in other types
+    int const_index;
+    double (*func0)(double);
+    double (*func1)(void *, double);
+    double (*func2)(void *, double, double);
+    expr_t * param[2];
+};
+
+static double evalsquish(          double d           ) { return 1/(1+exp(4*d)); }
+static double evalgauss (          double d           ) { return exp(-d*d/2)/sqrt(2*M_PI); }
+static double evalmod   (void * a, double d, double d2) { return d - floor(d/d2)*d2; }
+static double evalmax   (void * a, double d, double d2) { return d >  d2 ?   d : d2; }
+static double evalmin   (void * a, double d, double d2) { return d <  d2 ?   d : d2; }
+static double evalgt    (void * a, double d, double d2) { return d >  d2 ? 1.0 : 0.0; }
+static double evalgte   (void * a, double d, double d2) { return d >= d2 ? 1.0 : 0.0; }
+static double evaleq    (void * a, double d, double d2) { return d == d2 ? 1.0 : 0.0; }
+static double evalpow   (void * a, double d, double d2) { return pow(d, d2); }
+static double evalmul   (void * a, double d, double d2) { return d * d2; }
+static double evaldiv   (void * a, double d, double d2) { return d / d2; }
+static double evaladd   (void * a, double d, double d2) { return d + d2; }
+
+static double eval_expr(Parser * p, expr_t * e) {
+    if (e->type == 0) return e->value;
+    else switch(e->type) {
+        case 1: return e->value * p->const_value[e->const_index];
+        case 2: return e->value * e->func0(eval_expr(p, e->param[0]));
+        case 3: return e->value * e->func1(p->opaque, eval_expr(p, e->param[0]));
+        case 4: return e->value * e->func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
+        default: assert(0); return 0;
+    }
+}
+
+static expr_t * parse_expr(Parser *p);
+
+static expr_t * parse_primary(Parser *p) {
+    expr_t * d = calloc(1, sizeof(expr_t));
     char *next= p->s;
     int i;
 
     /* number */
-    d= av_strtod(p->s, &next);
+    d->value = av_strtod(p->s, &next);
     if(next != p->s){
+        d->type = 0;
         p->s= next;
         return d;
     }
+    d->value = 1;
 
     /* named constants */
     for(i=0; p->const_name && p->const_name[i]; i++){
         if(strmatch(p->s, p->const_name[i])){
             p->s+= strlen(p->const_name[i]);
-            return p->const_value[i];
+            d->type = 1;
+            d->const_index = i;
+            return d;
         }
     }
 
@@ -150,141 +189,169 @@
     if(p->s==NULL){
         *p->error = "missing (";
         p->s= next;
-        return NAN;
+        return NULL;
     }
     p->s++; // "("
-    d= evalExpression(p);
+    if (*next == '(') { // special case do-nothing
+#undef free
+        free(d);
+        d = parse_expr(p);
+        if(p->s[0] != ')'){
+            *p->error = "missing )";
+            return NULL;
+        }
+        p->s++; // ")"
+        return d;
+    }
+    d->param[0] = parse_expr(p);
     if(p->s[0]== ','){
         p->s++; // ","
-        d2= evalExpression(p);
+        d->param[1] = parse_expr(p);
     }
     if(p->s[0] != ')'){
         *p->error = "missing )";
-        return NAN;
+        return NULL;
     }
     p->s++; // ")"
 
-         if( strmatch(next, "sinh"  ) ) d= sinh(d);
-    else if( strmatch(next, "cosh"  ) ) d= cosh(d);
-    else if( strmatch(next, "tanh"  ) ) d= tanh(d);
-    else if( strmatch(next, "sin"   ) ) d= sin(d);
-    else if( strmatch(next, "cos"   ) ) d= cos(d);
-    else if( strmatch(next, "tan"   ) ) d= tan(d);
-    else if( strmatch(next, "atan"  ) ) d= atan(d);
-    else if( strmatch(next, "asin"  ) ) d= asin(d);
-    else if( strmatch(next, "acos"  ) ) d= acos(d);
-    else if( strmatch(next, "exp"   ) ) d= exp(d);
-    else if( strmatch(next, "log"   ) ) d= log(d);
-    else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
-    else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
-    else if( strmatch(next, "abs"   ) ) d= fabs(d);
-    else if( strmatch(next, "mod"   ) ) d-= floor(d/d2)*d2;
-    else if( strmatch(next, "max"   ) ) d= d >  d2 ?   d : d2;
-    else if( strmatch(next, "min"   ) ) d= d <  d2 ?   d : d2;
-    else if( strmatch(next, "gt"    ) ) d= d >  d2 ? 1.0 : 0.0;
-    else if( strmatch(next, "gte"   ) ) d= d >= d2 ? 1.0 : 0.0;
-    else if( strmatch(next, "lt"    ) ) d= d >  d2 ? 0.0 : 1.0;
-    else if( strmatch(next, "lte"   ) ) d= d >= d2 ? 0.0 : 1.0;
-    else if( strmatch(next, "eq"    ) ) d= d == d2 ? 1.0 : 0.0;
-    else if( strmatch(next, "("     ) ) d= d;
-//    else if( strmatch(next, "l1"    ) ) d= 1 + d2*(d - 1);
-//    else if( strmatch(next, "sq01"  ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
+         if( strmatch(next, "sinh"  ) ) d->func0 = sinh;
+    else if( strmatch(next, "cosh"  ) ) d->func0 = cosh;
+    else if( strmatch(next, "tanh"  ) ) d->func0 = tanh;
+    else if( strmatch(next, "sin"   ) ) d->func0 = sin;
+    else if( strmatch(next, "cos"   ) ) d->func0 = cos;
+    else if( strmatch(next, "tan"   ) ) d->func0 = tan;
+    else if( strmatch(next, "atan"  ) ) d->func0 = atan;
+    else if( strmatch(next, "asin"  ) ) d->func0 = asin;
+    else if( strmatch(next, "acos"  ) ) d->func0 = acos;
+    else if( strmatch(next, "exp"   ) ) d->func0 = exp;
+    else if( strmatch(next, "log"   ) ) d->func0 = log;
+    else if( strmatch(next, "squish") ) d->func0 = evalsquish;
+    else if( strmatch(next, "gauss" ) ) d->func0 = evalgauss;
+    else if( strmatch(next, "abs"   ) ) d->func0 = fabs;
+    else if( strmatch(next, "mod"   ) ) d->func2 = evalmod;
+    else if( strmatch(next, "max"   ) ) d->func2 = evalmax;
+    else if( strmatch(next, "min"   ) ) d->func2 = evalmin;
+    else if( strmatch(next, "gt"    ) ) d->func2 = evalgt;
+    else if( strmatch(next, "gte"   ) ) d->func2 = evalgte;
+    else if( strmatch(next, "lt"    ) ) { expr_t * tmp = d->param[1]; d->func2 = evalgte; d->param[1] = d->param[0]; d->param[0] = tmp; }
+    else if( strmatch(next, "lte"   ) ) { expr_t * tmp = d->param[1]; d->func2 = evalgt;  d->param[1] = d->param[0]; d->param[0] = tmp; }
+    else if( strmatch(next, "eq"    ) ) d->func2 = evaleq;
     else{
         for(i=0; p->func1_name && p->func1_name[i]; i++){
             if(strmatch(next, p->func1_name[i])){
-                return p->func1[i](p->opaque, d);
+                d->func1 = p->func1[i];
+                d->type = 3;
+                return d;
             }
         }
 
         for(i=0; p->func2_name && p->func2_name[i]; i++){
             if(strmatch(next, p->func2_name[i])){
-                return p->func2[i](p->opaque, d, d2);
+                d->func2 = p->func2[i];
+                d->type = 4;
+                return d;
             }
         }
 
         *p->error = "unknown function";
-        return NAN;
+        return NULL;
     }
+    if (d->func0) d->type = 2;
+    if (d->func1) d->type = 3;
+    if (d->func2) d->type = 4;
 
     return d;
 }
 
-static double evalPow(Parser *p, int *sign){
+static expr_t * parse_pow(Parser *p, int *sign){
     *sign= (*p->s == '+') - (*p->s == '-');
     p->s += *sign&1;
-    return evalPrimary(p);
+    return parse_primary(p);
 }
 
-static double evalFactor(Parser *p){
+static expr_t * parse_factor(Parser *p){
     int sign, sign2;
-    double ret, e;
-    ret= evalPow(p, &sign);
+    expr_t * e = parse_pow(p, &sign);
     while(p->s[0]=='^'){
+        expr_t * tmp = calloc(1, sizeof(expr_t));
+
         p->s++;
-        e= evalPow(p, &sign2);
-        ret= pow(ret, (sign2|1) * e);
+
+        tmp->type = 4;
+        tmp->func2 = &evalpow;
+        tmp->value = 1.;
+        tmp->param[0] = e;
+        tmp->param[1] = parse_pow(p, &sign2);
+        tmp->param[1]->value *= (sign2|1);
+        e = tmp;
     }
-    return (sign|1) * ret;
+    e->value *= (sign|1);
+    return e;
 }
 
-static double evalTerm(Parser *p){
-    double ret= evalFactor(p);
+static expr_t * parse_term(Parser *p){
+    expr_t * e = parse_factor(p);
     while(p->s[0]=='*' || p->s[0]=='/'){
-        if(*p->s++ == '*') ret*= evalFactor(p);
-        else               ret/= evalFactor(p);
+        expr_t * tmp = calloc(1, sizeof(expr_t));
+        tmp->type = 4;
+        if(*p->s++ == '*') tmp->func2 = &evalmul;
+        else               tmp->func2 = &evaldiv;
+        tmp->value = 1.;
+        tmp->param[0] = e;
+        tmp->param[1] = parse_factor(p);
+        e = tmp;
     }
-    return ret;
+    return e;
 }
 
-static double evalExpression(Parser *p){
-    double ret= 0;
+static expr_t * parse_expr(Parser *p) {
+    expr_t * e;
 
     if(p->stack_index <= 0) //protect against stack overflows
-        return NAN;
+        return NULL;
     p->stack_index--;
 
-    do{
-        ret += evalTerm(p);
-    }while(*p->s == '+' || *p->s == '-');
+    e = parse_term(p);
 
+    while(*p->s == '+' || *p->s == '-') {
+        expr_t * tmp = calloc(1, sizeof(expr_t));
+        tmp->type = 4;
+        tmp->func2 = &evaladd;
+        tmp->value = 1.;
+        tmp->param[0] = e;
+        tmp->param[1] = parse_term(p);
+        e = tmp;
+    };
+
     p->stack_index++;
 
-    return ret;
+    return e;
 }
 
-double ff_eval2(char *s, double *const_value, const char **const_name,
+expr_t * ff_parse(char *s, const char **const_name,
                double (**func1)(void *, double), const char **func1_name,
                double (**func2)(void *, double, double), char **func2_name,
-               void *opaque, char **error){
+               char **error){
     Parser p;
 
     p.stack_index=100;
     p.s= s;
-    p.const_value= const_value;
     p.const_name = const_name;
     p.func1      = func1;
     p.func1_name = func1_name;
     p.func2      = func2;
     p.func2_name = func2_name;
-    p.opaque     = opaque;
     p.error= error;
 
-    return evalExpression(&p);
+    return parse_expr(&p);
 }
+double ff_parse_eval(expr_t * e, double *const_value, void *opaque) {
+    Parser p;
 
-#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
-attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
-               double (**func1)(void *, double), const char **func1_name,
-               double (**func2)(void *, double, double), char **func2_name,
-               void *opaque){
-    char *error=NULL;
-    double ret;
-    ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
-    if (error)
-        av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
-    return ret;
+    p.const_value= const_value;
+    p.opaque     = opaque;
+    return eval_expr(&p, e);
 }
-#endif
 
 #ifdef TEST
 #undef printf



More information about the ffmpeg-devel mailing list