Commits

Victor Stinner  committed 9157483

NOTES; TODO

  • Participants
  • Parent commits e96e0fd

Comments (0)

Files changed (3)

-Cannot be optimized
-===================
+Maybe optimized later
+=====================
+
+Need type inference.
 
  - x + 0.
    Example: 'abc' + 0 raises a TypeError.
    Example: 5*0=0, 'abc'*0=''.
  - x and True, x and False, x or True, x or False.
    Example: '' and True='', 'abc' and True=True.
+
+Cannot be optimized
+===================
+
  - "def f(): if 0: yield", without the yield, the function is no more
    a generator.
- - x in {1, 2} => x in frozenset({1, 2}), cannot be done in AST.
-   AST cannot easily create constant frozenset. "frozenset" may be
-   shadowed at runtime.
- - x in [a, b, c] => x in {a, b, c}.
+ - x in {1, 2} => x in frozenset({1, 2}).
+   AST cannot create literal frozenset. The "frozenset" builtin may be shadowed
+   at runtime.
+ - x in (a, b, c) => x in {a, b, c}.
    A list can contain non-hashable objects, a set cannot.
    x can be non-hashable, whereas x in set() would fail.
  - [x*2 for x in "abc"] => (x*2 for x in "abc").
    The generator expression doesn't set the local variable x, and generators
    are slower than list comprehensions.
- - range(n) => xrange(n): xrange() raises an OverflowError if n is too big
+ - range(n) => xrange(n).
+   xrange() raises an OverflowError if n is too big, or a TypeError if n is
+   not an integer.
 
    - ``sys.maxint`` => ``2147483647``
    - ``os.linesep`` => ``"\\n"``
 
+ * ``"struct"``: struct module, calcsize(), pack() and unpack() functions.
+
  * ``"cpython_tests"``: disable some optimizations to workaround issues with
    the CPython test suite. Only use it for tests.
 
  - "i=0; while i < 10: print(i); i = i + 1": don't replace print(i) with print('0')
  - "for x in (): try: pass finally: continue" must raise a SyntaxError
  - "type(iter([]))"
- - "list([1, 2])" and "dict({...})" should create a copy?
 
 major optimizations:
 
 
 other:
 
+ - enable pythonbin by default?
+ - disable string and math by default?
+ - "list([1, 2])" and "dict({...})" should create a copy?
  - operator module:
 
+   * need to add an import, need to ensure that operator name is not used
    * lambda x: x[1] => operator.itemgetter(1)
    * lambda x: x.a => operator.attrgetter('a')
    * lambda x: x.f('a', b=1) => operator.methodcaller('f', 'a', b=1)
 
  - Python 3.2+: x in (1, 2, 3) => x in {1, 2, 3}, set converted to frozenset
    by the peepholer
- - {1,2}|{3} => {1, 2, 3}
  - Python 2: from __future__ import divison
  - [x*2 for x in range(3)]
  - map, itertools.map, filter:
    * (2 * x for x in a) => map((2).__mul__, a)
    * (x for x in a if x in 'abc') => filter('abc'.__contains__, a)
 
- - enable pythonbin by default?
  - not not not x
  - 1 < 2 < 3
  - str.format() (str%args already implemented), don't optimize {!n} (locale dependant)
 
  - infer type:
 
+   * "x=int(text)" : x is an int
    * int: "x=1; for ... in ...: x = x + 1; return x + 0"
      =>  "x=1; for ... in ...: x += 1; return x"
    * str: "x='abc'; for ... in ...: x = 'def'; return x == 'abc' or x == 'def'"
  - re module, warning at the locale dependent patterns
  - base64, binascii
  - pow() and a**b for complex numbers
- - optimize mutable types? "x=[1, 2, 3]; print(len(x))"
+ - optimize mutable types? "x=[1, 2, 3]; print(len(x))",
+   {1,2}|{3} => {1, 2, 3}.
  - Python3: print(sep="-"); print(end="")
- - x in {1, 2, 3} => x in frozenset((1, 2, 3)). Bytecode compiler doesn't
-   accept literal frozenset using ast.Call().