Author: Alex Gaynor <alex.gay...@gmail.com> Branch: Changeset: r46882:92e36ab4eb5e Date: 2011-08-29 05:05 -0400 http://bitbucket.org/pypy/pypy/changeset/92e36ab4eb5e/
Log: move sum() to app-level, it's significantly faster with the JIT, particularly in cases such as a generator expressions, where the storage can be unboxed. diff --git a/pypy/module/__builtin__/__init__.py b/pypy/module/__builtin__/__init__.py --- a/pypy/module/__builtin__/__init__.py +++ b/pypy/module/__builtin__/__init__.py @@ -19,6 +19,7 @@ 'sorted' : 'app_functional.sorted', 'any' : 'app_functional.any', 'all' : 'app_functional.all', + 'sum' : 'app_functional.sum', 'vars' : 'app_inspect.vars', 'dir' : 'app_inspect.dir', @@ -85,7 +86,6 @@ 'enumerate' : 'functional.W_Enumerate', 'min' : 'functional.min', 'max' : 'functional.max', - 'sum' : 'functional.sum', 'map' : 'functional.map', 'zip' : 'functional.zip', 'reduce' : 'functional.reduce', @@ -118,7 +118,7 @@ return module.Module(space, None, w_builtin) builtin = space.interpclass_w(w_builtin) if isinstance(builtin, module.Module): - return builtin + return builtin # no builtin! make a default one. Given them None, at least. builtin = module.Module(space, None) space.setitem(builtin.w_dict, space.wrap('None'), space.w_None) diff --git a/pypy/module/__builtin__/app_functional.py b/pypy/module/__builtin__/app_functional.py --- a/pypy/module/__builtin__/app_functional.py +++ b/pypy/module/__builtin__/app_functional.py @@ -34,3 +34,18 @@ if not x: return False return True + +def sum(sequence, start=0): + """sum(sequence[, start]) -> value + +Returns the sum of a sequence of numbers (NOT strings) plus the value +of parameter 'start' (which defaults to 0). When the sequence is +empty, returns start.""" + if isinstance(start, basestring): + raise TypeError("sum() can't sum strings") + last = start + for x in sequence: + # Very intentionally *not* +=, that would have different semantics if + # start was a mutable type, such as a list + last = last + x + return last \ No newline at end of file diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py --- a/pypy/module/__builtin__/functional.py +++ b/pypy/module/__builtin__/functional.py @@ -325,27 +325,6 @@ result_w.append(w_res) return result_w -def sum(space, w_sequence, w_start=0): - """sum(sequence[, start]) -> value - -Returns the sum of a sequence of numbers (NOT strings) plus the value -of parameter 'start' (which defaults to 0). When the sequence is -empty, returns start.""" - if space.is_true(space.isinstance(w_start, space.w_basestring)): - msg = "sum() can't sum strings" - raise OperationError(space.w_TypeError, space.wrap(msg)) - w_iter = space.iter(w_sequence) - w_last = w_start - while True: - try: - w_next = space.next(w_iter) - except OperationError, e: - if not e.match(space, space.w_StopIteration): - raise - break - w_last = space.add(w_last, w_next) - return w_last - @unwrap_spec(sequences_w="args_w") def zip(space, sequences_w): """Return a list of tuples, where the nth tuple contains every nth item of _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit