Changeset 12065


Ignore:
Timestamp:
07/26/09 22:37:27 (14 years ago)
Author:
ehuelsmann
Message:

Implement a stack frame pool to save execution time
on stack management.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/LispThread.java

    r12064 r12065  
    450450    private static class StackFrame
    451451    {
    452         public final LispObject operator;
    453         private final LispObject first;
    454         private final LispObject second;
    455         private final LispObject third;
    456         private final LispObject[] args;
     452        public LispObject operator;
     453        private LispObject first;
     454        private LispObject second;
     455        private LispObject third;
     456        private LispObject[] args;
    457457        final StackFrame next;
    458458
    459         public StackFrame(LispObject operator, StackFrame next)
     459        public StackFrame(StackFrame next) {
     460            this.next = next;
     461        }
     462
     463        public final void set(LispObject operator)
    460464        {
    461465            this.operator = operator;
     
    464468            third = null;
    465469            args = null;
    466             this.next = next;
    467         }
    468 
    469         public StackFrame(LispObject operator, LispObject arg, StackFrame next)
     470        }
     471
     472        public final void set(LispObject operator, LispObject arg)
    470473        {
    471474            this.operator = operator;
     
    474477            third = null;
    475478            args = null;
    476             this.next = next;
    477         }
    478 
    479         public StackFrame(LispObject operator, LispObject first,
    480                           LispObject second, StackFrame next)
     479        }
     480
     481        public final void set(LispObject operator, LispObject first,
     482                              LispObject second)
    481483        {
    482484            this.operator = operator;
     
    485487            third = null;
    486488            args = null;
    487             this.next = next;
    488         }
    489 
    490         public StackFrame(LispObject operator, LispObject first,
    491                           LispObject second, LispObject third, StackFrame next)
     489        }
     490
     491        public final void set(LispObject operator, LispObject first,
     492                              LispObject second, LispObject third)
    492493        {
    493494            this.operator = operator;
     
    496497            this.third = third;
    497498            args = null;
    498             this.next = next;
    499         }
    500 
    501         public StackFrame(LispObject operator, LispObject[] args, StackFrame next)
     499        }
     500
     501        public final void set(LispObject operator, LispObject[] args)
    502502        {
    503503            this.operator = operator;
     
    506506            third = null;
    507507            this.args = args;
    508             this.next = next;
    509508        }
    510509
     
    542541
    543542    private StackFrame stack = null;
     543    private final int framePoolSize = 256;
     544    private final StackFrame[] framePool = new StackFrame[256];
     545    private int framePointer = -1;
    544546
    545547    @Deprecated
     
    563565    }
    564566
     567    private final StackFrame newStackFrame() {
     568        if (++framePointer < framePoolSize) {
     569            if (framePool[framePointer] == null)
     570                framePool[framePointer] = new StackFrame(stack);
     571            return (stack = framePool[framePointer]);
     572        } else
     573            return (stack = new StackFrame(stack));
     574    }
     575
    565576    public final void pushStackFrame(LispObject operator)
    566577        throws ConditionThrowable
    567578    {
    568         stack = new StackFrame(operator, stack);
     579        newStackFrame().set(operator);
    569580        doProfiling();
    570581    }
     
    573584        throws ConditionThrowable
    574585    {
    575         stack = new StackFrame(operator, arg, stack);
     586        newStackFrame().set(operator, arg);
    576587        doProfiling();
    577588    }
     
    581592        throws ConditionThrowable
    582593    {
    583         stack = new StackFrame(operator, first, second, stack);
     594        newStackFrame().set(operator, first, second);
    584595        doProfiling();
    585596    }
     
    589600        throws ConditionThrowable
    590601    {
    591         stack = new StackFrame(operator, first, second, third, stack);
     602        newStackFrame().set(operator, first, second, third);
    592603        doProfiling();
    593604    }
     
    596607        throws ConditionThrowable
    597608    {
    598         stack = new StackFrame(operator, args, stack);
     609        newStackFrame().set(operator, args);
    599610        doProfiling();
    600611    }
     
    602613    public final void popStackFrame()
    603614    {
    604         if (stack != null)
     615        if (stack != null) {
     616            if (framePointer < framePoolSize)
     617                stack.set(null);
    605618            stack = stack.next;
     619            framePointer--;
     620        }
    606621    }
    607622
     
    609624    {
    610625        stack = null;
     626        // Clear out old frames, in order to prevent leaking references
     627        // to old objects
     628        for (StackFrame frame : framePool)
     629            if (frame != null)
     630                frame.set(null);
     631        framePointer = -1;
    611632    }
    612633
Note: See TracChangeset for help on using the changeset viewer.