[Python-checkins] CVS: python/dist/src/Tools/compiler/compiler pyassem.py,1.21,1.22

Jeremy Hylton jhylton@users.sourceforge.net
Wed, 29 Aug 2001 11:09:52 -0700


Update of /cvsroot/python/python/dist/src/Tools/compiler/compiler
In directory usw-pr-cvs1:/tmp/cvs-serv14439

Modified Files:
	pyassem.py 
Log Message:
Modify name conversion to be (hopefully) a bit more efficient.

Use a dictionary instead of a list to map objects to their offsets in
a const/name tuple of a code object.

XXX The conversion is perhaps incomplete, in that we shouldn't have to
do the list2dict to start.


Index: pyassem.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/compiler/compiler/pyassem.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** pyassem.py	2001/08/28 16:36:12	1.21
--- pyassem.py	2001/08/29 18:09:50	1.22
***************
*** 16,19 ****
--- 16,30 ----
      return l
  
+ def list2dict(l):
+     d = {}
+     for i in range(len(l)):
+         d[l[i]] = i
+     return d
+ 
+ def dict2list(d):
+     l = [(v, k) for k, v in d.items()]
+     l.sort()
+     return [k for v, k in l]
+ 
  class FlowGraph:
      def __init__(self):
***************
*** 327,331 ****
      super_init = FlowGraph.__init__
  
!     def __init__(self, name, filename, args=(), optimized=0):
          self.super_init()
          self.name = name
--- 338,342 ----
      super_init = FlowGraph.__init__
  
!     def __init__(self, name, filename, args=(), optimized=0, klass=None):
          self.super_init()
          self.name = name
***************
*** 334,337 ****
--- 345,349 ----
          self.args = args # XXX
          self.argcount = getArgCount(args)
+         self.klass = klass
          if optimized:
              self.flags = CO_OPTIMIZED | CO_NEWLOCALS 
***************
*** 446,457 ****
          self.consts.insert(0, self.docstring)
          self.sort_cellvars()
          for i in range(len(self.insts)):
              t = self.insts[i]
              if len(t) == 2:
!                 opname = t[0]
!                 oparg = t[1]
                  conv = self._converters.get(opname, None)
                  if conv:
                      self.insts[i] = opname, conv(self, oparg)
          self.stage = CONV
  
--- 458,480 ----
          self.consts.insert(0, self.docstring)
          self.sort_cellvars()
+ 
+         self.c_varnames = list2dict(self.varnames)
+         self.c_names = list2dict(self.names)
+         self.c_consts = list2dict(self.consts)
+         self.c_closure = list2dict(self.closure)
+ 
          for i in range(len(self.insts)):
              t = self.insts[i]
              if len(t) == 2:
!                 opname, oparg = t
                  conv = self._converters.get(opname, None)
                  if conv:
                      self.insts[i] = opname, conv(self, oparg)
+ 
+         self.varnames = dict2list(self.c_varnames)
+         self.names = dict2list(self.c_names)
+         self.consts = dict2list(self.c_consts)
+         self.closure = dict2list(self.c_closure)
+ 
          self.stage = CONV
  
***************
*** 469,478 ****
          self.closure = self.cellvars + self.freevars
  
!     def _lookupName(self, name, list):
          """Return index of name in list, appending if necessary"""
          t = type(name)
          for i in range(len(list)):
!             # must do a comparison on type first to prevent UnicodeErrors 
!             if t == type(list[i]) and list[i] == name:
                  return i
          end = len(list)
--- 492,512 ----
          self.closure = self.cellvars + self.freevars
  
!     def _lookupName(self, name, dict):
!         i = dict.get(name, None)
!         if i is None:
!             i = dict[name] = len(dict)
!         return i
! 
!     def XXX_lookupName(self, name, list):
          """Return index of name in list, appending if necessary"""
+         # XXX It should be possible to replace this with some
+         # dictionary operations, but not sure how
          t = type(name)
          for i in range(len(list)):
!             # must do a comparison on type first to prevent UnicodeErrors
!             # not clear that a dictionary would work, because we could
!             # get UnicodeErrors on lookups 
!             elt = list[i]
!             if isinstance(elt, t) and elt == name:
                  return i
          end = len(list)
***************
*** 484,501 ****
          if hasattr(arg, 'getCode'):
              arg = arg.getCode()
!         return self._lookupName(arg, self.consts)
  
      def _convert_LOAD_FAST(self, arg):
!         self._lookupName(arg, self.names)
!         return self._lookupName(arg, self.varnames)
      _convert_STORE_FAST = _convert_LOAD_FAST
      _convert_DELETE_FAST = _convert_LOAD_FAST
  
      def _convert_LOAD_NAME(self, arg):
!         return self._lookupName(arg, self.names)
  
      def _convert_NAME(self, arg):
!         self._lookupName(arg, self.varnames)
!         return self._lookupName(arg, self.names)
      _convert_STORE_NAME = _convert_NAME
      _convert_DELETE_NAME = _convert_NAME
--- 518,536 ----
          if hasattr(arg, 'getCode'):
              arg = arg.getCode()
!         return self._lookupName(arg, self.c_consts)
  
      def _convert_LOAD_FAST(self, arg):
!         self._lookupName(arg, self.c_names)
!         return self._lookupName(arg, self.c_varnames)
      _convert_STORE_FAST = _convert_LOAD_FAST
      _convert_DELETE_FAST = _convert_LOAD_FAST
  
      def _convert_LOAD_NAME(self, arg):
!         return self._lookupName(arg, self.c_names)
  
      def _convert_NAME(self, arg):
!         if self.klass is None:
!             self._lookupName(arg, self.c_varnames)
!         return self._lookupName(arg, self.c_names)
      _convert_STORE_NAME = _convert_NAME
      _convert_DELETE_NAME = _convert_NAME
***************
*** 510,522 ****
  
      def _convert_DEREF(self, arg):
!         self._lookupName(arg, self.names)
!         self._lookupName(arg, self.varnames)
!         return self._lookupName(arg, self.closure)
      _convert_LOAD_DEREF = _convert_DEREF
      _convert_STORE_DEREF = _convert_DEREF
  
      def _convert_LOAD_CLOSURE(self, arg):
!         self._lookupName(arg, self.varnames)
!         return self._lookupName(arg, self.closure)
  
      _cmp = list(dis.cmp_op)
--- 545,557 ----
  
      def _convert_DEREF(self, arg):
!         self._lookupName(arg, self.c_names)
!         self._lookupName(arg, self.c_varnames)
!         return self._lookupName(arg, self.c_closure)
      _convert_LOAD_DEREF = _convert_DEREF
      _convert_STORE_DEREF = _convert_DEREF
  
      def _convert_LOAD_CLOSURE(self, arg):
!         self._lookupName(arg, self.c_varnames)
!         return self._lookupName(arg, self.c_closure)
  
      _cmp = list(dis.cmp_op)