I'm experimenting with an idea for optimizing the performance of a series of nested class method calls. Basically I have a couple classes equivalent to the following:

/*an evaluation "functor" interface*/

interface Evaluation {
        public Evaluation evaluate(EvaluationContext ctx);
}

/*a context for evaluating Evaluation functors*/
class EvaluationContext {

        public Evaluation evaluate(double d){
                return new DefaultValue(d);
        }

        public double doubleValue(Evaluation eval){
                return (
                        (DefaultValue)eval.evaluate(this)
                ).doubleValue();
        }

        class DefaultValue {
                
                double d;

                public DefaultValue(double d){
                        this.double = double;
                }

                public double doubleValue(){
                        return d;
                }
        }
}

/*Some basic implmentations of the Evaluation functor*/
class Add implements Evaluation{

private Evaluation left;

private Evaluation right;

        public void setLeftOperand(Evaluation left) {
                this.left = left;
        }

        public void setRightOperand(Evaluation right) {
                this.right = right;
        }

    public Evaluation evaluate(EvaluationContext context){
        return context.evaluate(
            context.doubleValue(left) + context.doubleValue(right)
        );
    }
}


class Subtract implements Evaluation{


private Evaluation left;

private Evaluation right;

        public void setLeftOperand(Evaluation left) {
                this.left = left;
        }

        public void setRightOperand(Evaluation right) {
                this.right = right;
        }

    public Evaluation evaluate(EvaluationContext context){
        return context.evaluate(
            context.doubleValue(left) - context.doubleValue(right)
        );
    }

}


I can use these Classes to do some "symbolic calculation" like the following


EvaluationContext ctx = new EvaluationContext();

        Add add = new Add();
        add.setLeftOperand(ctx.evaluate(1.0));

add.setRightOperand(ctx.evaluate(2.0));

Subtract sub = new Subtract();

        sub.setLeftOperand(ctx.evaluate(3.0));
        sub.setRightOperand(add);

double result = ctx.doubleValue(sub);

which is roughly equvalent to

double result = (3.0 - (1.0 + 2.0));

but with a bunch of extra method calls inbetween. Conceptually, I can sort of "unravel" the "evaluation" of the "sub" hierarchy into the following code fragment

ctx.doubleValue(
        Subtract.evaluate(
            ctx.doubleValue(
                ctx.evaluate(
                        /* 3.0 */
                        DefaultValue.doubleValue();
                )
            )
            -
            ctx.doubleValue(
                Add.evaluate(
                   ctx.doubleValue(
                        /*1.0*/
                        DefaultValue.doubleValue();
                    )
                    +
                    ctx.doubleValue(
                        /*2.0*/
                        DefaultValue.doubleValue();
                    )
                )
            )
        )
)

What I'd like to be able to do is, remove any repeative methods calls across the classes that result in moving from a double to an evaluation back to a double, such as the following example;

        ctx.doubleValue(
                ctx.evaluate(
                        /* 3.0 */
                        DefaultValue.doubleValue();
                )
        )

This means basically that the property of "DefaultValue" containing 3.0 gets referenced directly in Subtract.evaluate, rather than going to the process of being "wrapped" in another DefaultValue.

(I have used BCEL in the past (primarily BCELifier) to generate code that I could use in extending existing classes, but this seems more like a search and copy proceedure that actually takes two classes and attempts to combine them, at the same time removing method calls.

Or,

This seems like a process of "exposing" previously private or protected properties and circumventing the method calls to reference those properties directly.

Either way, I want to find a way to be able to build these structures and then sill be able to optimize their evaluation.

How might you best recommend I approach this using BCEL?

-Mark
--
Mark Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to