Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
245434dc
Commit
245434dc
authored
May 27, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
drop sum(genexpr) into plain C code when the result is C typed
parent
514f2542
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
3 deletions
+27
-3
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+11
-1
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+2
-2
tests/run/inlined_generator_expressions.pyx
tests/run/inlined_generator_expressions.pyx
+14
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
245434dc
...
...
@@ -4037,10 +4037,13 @@ class GeneratorExpressionNode(ScopedExprNode):
class
InlinedGeneratorExpressionNode
(
GeneratorExpressionNode
):
# An inlined generator expression for which the result is
# calculated inside of the loop.
# calculated inside of the loop. This will only be created by
# transforms when replacing builtin calls on generator
# expressions.
#
# loop ForStatNode the for-loop, not containing any YieldExprNodes
# result_node ResultRefNode the reference to the result value temp
# orig_func String the name of the builtin function this node replaces
child_attrs
=
[
"loop"
]
...
...
@@ -4048,6 +4051,13 @@ class InlinedGeneratorExpressionNode(GeneratorExpressionNode):
self
.
type
=
self
.
result_node
.
type
self
.
is_temp
=
True
def
coerce_to
(
self
,
dst_type
,
env
):
if
self
.
orig_func
==
'sum'
and
dst_type
.
is_numeric
:
# we can optimise by dropping the aggregation variable into C
self
.
result_node
.
type
=
self
.
type
=
dst_type
return
self
return
GeneratorExpressionNode
.
coerce_to
(
self
,
dst_type
,
env
)
def
generate_result_code
(
self
,
code
):
self
.
result_node
.
result_code
=
self
.
result
()
self
.
loop
.
generate_execution_code
(
code
)
...
...
Cython/Compiler/Optimize.py
View file @
245434dc
...
...
@@ -1186,7 +1186,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return
ExprNodes
.
InlinedGeneratorExpressionNode
(
gen_expr_node
.
pos
,
loop
=
loop_node
,
result_node
=
result_ref
,
expr_scope
=
gen_expr_node
.
expr_scope
)
expr_scope
=
gen_expr_node
.
expr_scope
,
orig_func
=
is_any
and
'any'
or
'all'
)
def
_handle_simple_function_sum
(
self
,
node
,
pos_args
):
"""Transform sum(genexpr) into an equivalent inlined aggregation loop.
...
...
@@ -1230,7 +1230,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return
ExprNodes
.
InlinedGeneratorExpressionNode
(
gen_expr_node
.
pos
,
loop
=
exec_code
,
result_node
=
result_ref
,
expr_scope
=
gen_expr_node
.
expr_scope
)
expr_scope
=
gen_expr_node
.
expr_scope
,
orig_func
=
'sum'
)
# specific handlers for general call nodes
...
...
tests/run/inlined_generator_expressions.pyx
View file @
245434dc
...
...
@@ -15,6 +15,20 @@ def range_sum(int N):
result
=
sum
(
i
for
i
in
range
(
N
))
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
,
"//InlinedGeneratorExpressionNode"
)
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode'
,
'//ForInStatNode'
)
def
range_sum_typed
(
int
N
):
"""
>>> sum(range(10))
45
>>> range_sum_typed(10)
45
"""
cdef
int
result
=
sum
(
i
for
i
in
range
(
N
))
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
,
"//InlinedGeneratorExpressionNode"
)
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment