Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 127

Show
Ignore:
Timestamp:
08/09/07 22:51:33
Author:
fumanchu
Message:

Moved from pop/append to slicing in astwalk for speed.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/ast/geniusql/astwalk.py

    r126 r127  
    192192        terms = self.targets.get(ip) 
    193193        if terms: 
    194             clause = self.stack.pop() 
     194            clause = self.stack[-1] 
    195195            while terms: 
    196196                term, oper = terms.pop() 
     
    201201                else: 
    202202                    clause = oper((term, clause)) 
    203             self.stack.append(clause) 
     203            self.stack[-1] = clause 
    204204            if self.verbose: 
    205205                self.debug("clause:", clause, "\n") 
     
    217217     
    218218    def visit_BUILD_LIST(self, lo, hi): 
    219         terms = [self.stack.pop() for i in range(lo + (hi << 8))] 
    220         terms.reverse() 
    221         self.stack.append(ast.List(terms)) 
     219        numterms = lo + (hi << 8) 
     220        if numterms: 
     221            self.stack[-numterms:] = [ast.List(self.stack[-numterms:])] 
    222222     
    223223    def visit_BUILD_MAP(self, lo, hi): 
     
    226226     
    227227    def visit_BUILD_TUPLE(self, lo, hi): 
    228         terms = [self.stack.pop() for i in range(lo + (hi << 8))] 
    229         terms.reverse() 
    230         self.stack.append(ast.Tuple(terms)) 
     228        numterms = lo + (hi << 8) 
     229        if numterms: 
     230            self.stack[-numterms:] = [ast.Tuple(self.stack[-numterms:])] 
    231231     
    232232    def visit_CALL_FUNCTION(self, lo, hi): 
     
    237237            kwargs.append(ast.Keyword(key, val)) 
    238238         
    239         args = [self.stack.pop() for i in xrange(lo)] 
    240         args.reverse() 
    241          
    242         func = self.stack.pop() 
     239        if lo: 
     240            args = self.stack[-lo:] 
     241            self.stack[-lo:] = [] 
     242        else: 
     243            args = [] 
     244         
     245        func = self.stack[-1] 
    243246         
    244247        if self.reduce and isinstance(func, ast.Const): 
    245248            if func.value is getattr and not isinstance(args[0], ast.Const): 
    246                 self.stack.append(ast.Getattr(args[0], args[1].value)
     249                self.stack[-1] = ast.Getattr(args[0], args[1].value
    247250                return 
    248251            else: 
     
    266269                    if kwargvals is not None: 
    267270                        retval = func.value(*tuple(argvals), **kwargvals) 
    268                         self.stack.append(ast.Const(retval)
     271                        self.stack[-1] = ast.Const(retval
    269272                        return 
    270273         
    271274        if kwargs: 
    272275            args += kwargs 
    273         self.stack.append(ast.CallFunc(func, args)
     276        self.stack[-1] = ast.CallFunc(func, args
    274277     
    275278    def visit_COMPARE_OP(self, lo, hi): 
    276         term2, term1 = self.stack.pop(), self.stack.pop() 
     279        term1, term2 = self.stack[-2:] 
    277280        op = cmp_op[lo + (hi << 8)] 
    278281        if self.reduce and (isinstance(term1, ast.Const) and 
    279282                            isinstance(term2, ast.Const)): 
    280283            oper = codewalk.comparisons[op] 
    281             self.stack.append(ast.Const(oper(term1.value, term2.value))) 
    282         else: 
    283             self.stack.append(ast.Compare(term1, [(op, term2)])) 
     284            self.stack[-2:] = [ast.Const(oper(term1.value, term2.value))] 
     285        else: 
     286            self.stack[-2:] = [ast.Compare(term1, [(op, term2)])] 
    284287        if self.verbose: 
    285288            self.debug(op) 
     
    310313    def visit_LOAD_ATTR(self, lo, hi): 
    311314        term = self.co_names[lo + (hi << 8)] 
    312         obj = self.stack.pop() 
     315        obj = self.stack[-1] 
    313316        if self.reduce and isinstance(obj, ast.Const): 
    314             self.stack.append(ast.Const(getattr(obj.value, term))) 
    315         else: 
    316             self.stack.append(ast.Getattr(obj, term)
     317            self.stack[-1] = ast.Const(getattr(obj.value, term)) 
     318        else: 
     319            self.stack[-1] = ast.Getattr(obj, term
    317320        if self.verbose: 
    318321            self.debug(term) 
     
    351354     
    352355    def visit_ROT_TWO(self): 
    353         v = self.stack.pop() 
    354         k = self.stack.pop() 
    355         self.stack.extend([v, k]) 
     356        k, v = self.stack[-2:] 
     357        self.stack[-2:] = [v, k] 
    356358     
    357359    def visit_ROT_THREE(self): 
    358         v = self.stack.pop() 
    359         k = self.stack.pop() 
    360         x = self.stack.pop() 
    361         self.stack.extend([v, x, k]) 
     360        x, k, v = self.stack[-3:] 
     361        self.stack[-3:] = [v, x, k] 
    362362     
    363363    def visit_SLICE_PLUS_0(self): 
    364         obj = self.stack.pop() 
     364        obj = self.stack[-1] 
    365365        if self.reduce and isinstance(obj, ast.Const): 
    366             self.stack.append(ast.Const(obj.value[:])
    367         else: 
    368             self.stack.append(ast.Slice(obj, 'OP_APPLY', None, None)
     366            self.stack[-1] = ast.Const(obj.value[:]
     367        else: 
     368            self.stack[-1] = ast.Slice(obj, 'OP_APPLY', None, None
    369369     
    370370    def visit_SLICE_PLUS_1(self): 
    371         arg = self.stack.pop() 
    372         obj = self.stack.pop() 
     371        obj, arg = self.stack[-2:] 
    373372        if self.reduce and (isinstance(obj, ast.Const) and 
    374373                            isinstance(arg, ast.Const)): 
    375             self.stack.append(ast.Const(obj.value[arg.value:])) 
    376         else: 
    377             self.stack.append(ast.Slice(obj, 'OP_APPLY', arg, None)) 
     374            self.stack[-2:] = [ast.Const(obj.value[arg.value:])] 
     375        else: 
     376            self.stack[-2:] = [ast.Slice(obj, 'OP_APPLY', arg, None)] 
    378377     
    379378    def visit_SLICE_PLUS_2(self): 
    380         arg = self.stack.pop() 
    381         obj = self.stack.pop() 
     379        obj, arg = self.stack[-2:] 
    382380        if self.reduce and (isinstance(obj, ast.Const) and 
    383381                            isinstance(arg, ast.Const)): 
    384             self.stack.append(ast.Const(obj.value[:arg.value])
    385         else: 
    386             self.stack.append(ast.Slice(obj, 'OP_APPLY', None, arg)
     382            self.stack[-2:] = ast.Const(obj.value[:arg.value]
     383        else: 
     384            self.stack[-2:] = ast.Slice(obj, 'OP_APPLY', None, arg
    387385     
    388386    def visit_SLICE_PLUS_3(self): 
    389         arg2 = self.stack.pop() 
    390         arg1 = self.stack.pop() 
    391         obj = self.stack.pop() 
     387        obj, arg1, arg2 = self.stack[-3:] 
    392388        if self.reduce and (isinstance(obj, ast.Const) and 
    393389                            isinstance(arg1, ast.Const) and 
    394390                            isinstance(arg2, ast.Const)): 
    395             self.stack.append(ast.Const(obj.value[arg1.value:arg2.value])
    396         else: 
    397             self.stack.append(ast.Slice(obj, 'OP_APPLY', arg1, arg2)
     391            self.stack[-3:] = ast.Const(obj.value[arg1.value:arg2.value]
     392        else: 
     393            self.stack[-3:] = ast.Slice(obj, 'OP_APPLY', arg1, arg2
    398394     
    399395    def visit_STORE_SUBSCR(self): 
    400         k = self.stack.pop() 
    401396        # 'x' should be an ast.Dict 
    402         x = self.stack.pop() 
    403         v = self.stack.pop() 
     397        v, x, k = self.stack[-3:] 
     398        self.stack[-3:] = [] 
    404399        x.items.append((k, v)) 
    405400##     
     
    409404     
    410405    def visit_UNARY_INVERT(self): 
    411         term = self.stack.pop() 
     406        term = self.stack[-1] 
    412407        if self.reduce and isinstance(term, ast.Const): 
    413             self.stack.append(ast.Const(~term.value)
    414         else: 
    415             self.stack.append(ast.Invert(term)
     408            self.stack[-1] = ast.Const(~term.value
     409        else: 
     410            self.stack[-1] = ast.Invert(term
    416411     
    417412    def visit_UNARY_NEGATIVE(self): 
    418         term = self.stack.pop() 
     413        term = self.stack[-1] 
    419414        if self.reduce and isinstance(term, ast.Const): 
    420             self.stack.append(ast.Const(-term.value)
    421         else: 
    422             self.stack.append(ast.UnarySub(term)
     415            self.stack[-1] = ast.Const(-term.value
     416        else: 
     417            self.stack[-1] = ast.UnarySub(term
    423418     
    424419    def visit_UNARY_NOT(self): 
    425         term = self.stack.pop() 
     420        term = self.stack[-1] 
    426421        if self.reduce and isinstance(term, ast.Const): 
    427             self.stack.append(ast.Const(not term.value)
    428         else: 
    429             self.stack.append(ast.Not(term)
     422            self.stack[-1] = ast.Const(not term.value
     423        else: 
     424            self.stack[-1] = ast.Not(term
    430425     
    431426    def visit_UNARY_POSITIVE(self): 
    432         term = self.stack.pop() 
     427        term = self.stack[-1] 
    433428        if self.reduce and isinstance(term, ast.Const): 
    434             self.stack.append(ast.Const(+term.value)
    435         else: 
    436             self.stack.append(ast.UnaryAdd(term)
     429            self.stack[-1] = ast.Const(+term.value
     430        else: 
     431            self.stack[-1] = ast.UnaryAdd(term
    437432     
    438433    def visit_BINARY_SUBSCR(self): 
    439         op2, op1 = self.stack.pop(), self.stack.pop() 
     434        op1, op2 = self.stack[-2:] 
    440435        if self.reduce and (isinstance(op1, ast.Const) and 
    441436                            isinstance(op2, ast.Const)): 
    442             self.stack.append(ast.Const(op1.value[op2.value])) 
    443         else: 
    444             self.stack.append(ast.Subscript(op1, 'OP_APPLY', op2)) 
     437            self.stack[-2:] = [ast.Const(op1.value[op2.value])] 
     438        else: 
     439            self.stack[-2:] = [ast.Subscript(op1, 'OP_APPLY', op2)] 
    445440     
    446441    def binary_op(self, op): 
    447         op2, op1 = self.stack.pop(), self.stack.pop() 
     442        op1, op2 = self.stack[-2:] 
    448443        if self.reduce and (isinstance(op1, ast.Const) and 
    449444                            isinstance(op2, ast.Const)): 
    450             self.stack.append(ast.Const(ast_to_op[op](op1.value, op2.value))) 
     445            self.stack[-2:] = [ast.Const(ast_to_op[op](op1.value, op2.value))] 
    451446        else: 
    452447            # Binary ops like ast.Add take a single tuple as a first arg 
    453             self.stack.append(op((op1, op2))) 
     448            self.stack[-2:] = [op((op1, op2))] 
    454449     
    455450    def bit_op(self, op): 
    456         op2, op1 = self.stack.pop(), self.stack.pop() 
     451        op1, op2 = self.stack[-2:] 
    457452        if self.reduce and (isinstance(op1, ast.Const) and 
    458453                            isinstance(op2, ast.Const)): 
    459             self.stack.append(ast.Const(ast_to_op[op](op1.value, op2.value))) 
    460         else: 
    461             self.stack.append(op(op1, op2)) 
     454            self.stack[-2:] = [ast.Const(ast_to_op[op](op1.value, op2.value))] 
     455        else: 
     456            self.stack[-2:] = [op(op1, op2)] 
    462457 
    463458