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
5bcbcf84
Commit
5bcbcf84
authored
Jul 10, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use inlined generator expressions for set(genexpr), list(genexpr), dict(genexpr)
parent
995597cc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
48 deletions
+27
-48
CHANGES.rst
CHANGES.rst
+4
-6
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+17
-28
tests/run/dictcomp.pyx
tests/run/dictcomp.pyx
+3
-7
tests/run/setcomp.pyx
tests/run/setcomp.pyx
+3
-7
No files found.
CHANGES.rst
View file @
5bcbcf84
...
@@ -41,12 +41,10 @@ Features added
...
@@ -41,12 +41,10 @@ Features added
* Binary and/or/xor/rshift operations with small constant Python integers
* Binary and/or/xor/rshift operations with small constant Python integers
are faster.
are faster.
* The builtin functions ``any()`` and ``all()`` are inlined into the
* When called on generator expressions, the builtin functions ``all()``,
for-loops when called on generator expressions to avoid the generator
``any()``, ``dict()``, ``list()``, ``set()`` and ``sorted()`` are
iteration overhead.
(partially) inlined into the for-loops to avoid the generator iteration
overhead.
* The builtin function ``sorted()`` is optimised when called on a
generator expression.
* Keyword argument dicts are no longer copied on function entry when they
* Keyword argument dicts are no longer copied on function entry when they
are not being used or only passed through to other function calls (e.g.
are not being used or only passed through to other function calls (e.g.
...
...
Cython/Compiler/Optimize.py
View file @
5bcbcf84
...
@@ -1770,7 +1770,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
...
@@ -1770,7 +1770,7 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return
self
.
_transform_list_set_genexpr
(
node
,
pos_args
,
Builtin
.
set_type
)
return
self
.
_transform_list_set_genexpr
(
node
,
pos_args
,
Builtin
.
set_type
)
def
_transform_list_set_genexpr
(
self
,
node
,
pos_args
,
target_type
):
def
_transform_list_set_genexpr
(
self
,
node
,
pos_args
,
target_type
):
"""Replace set(genexpr) and list(genexpr) by a
literal
comprehension.
"""Replace set(genexpr) and list(genexpr) by a
n inlined
comprehension.
"""
"""
if
len
(
pos_args
)
>
1
:
if
len
(
pos_args
)
>
1
:
return
node
return
node
...
@@ -1780,29 +1780,24 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
...
@@ -1780,29 +1780,24 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
loop_node
=
gen_expr_node
.
loop
loop_node
=
gen_expr_node
.
loop
yield_expression
,
yield_stat_node
=
self
.
_find_single_yield_expression
(
loop_node
)
yield_expression
,
yield_stat_node
=
self
.
_find_single_yield_expression
(
loop_node
)
# FIXME: currently nonfunctional
yield_expression
=
None
if
yield_expression
is
None
:
if
yield_expression
is
None
:
return
node
return
node
result_node
=
ExprNodes
.
InlinedGeneratorExpressionNode
(
node
.
pos
,
gen_expr_node
,
orig_func
=
'set'
if
target_type
is
Builtin
.
set_type
else
'list'
,
comprehension_type
=
target_type
)
append_node
=
ExprNodes
.
ComprehensionAppendNode
(
append_node
=
ExprNodes
.
ComprehensionAppendNode
(
yield_expression
.
pos
,
yield_expression
.
pos
,
expr
=
yield_expression
)
expr
=
yield_expression
,
target
=
result_node
.
target
)
Visitor
.
recursively_replace_node
(
loop_node
,
yield_stat_node
,
append_node
)
Visitor
.
recursively_replace_node
(
loop_node
,
yield_stat_node
,
append_node
)
return
result_node
comp
=
ExprNodes
.
ComprehensionNode
(
node
.
pos
,
has_local_scope
=
True
,
expr_scope
=
gen_expr_node
.
expr_scope
,
loop
=
loop_node
,
append
=
append_node
,
type
=
target_type
)
append_node
.
target
=
comp
return
comp
def
_handle_simple_function_dict
(
self
,
node
,
pos_args
):
def
_handle_simple_function_dict
(
self
,
node
,
pos_args
):
"""Replace dict( (a,b) for ... ) by a
literal { a:b for ... }.
"""Replace dict( (a,b) for ... ) by a
n inlined { a:b for ... }
"""
"""
if
len
(
pos_args
)
==
0
:
if
len
(
pos_args
)
==
0
:
return
ExprNodes
.
DictNode
(
node
.
pos
,
key_value_pairs
=
[],
constant_result
=
{})
return
ExprNodes
.
DictNode
(
node
.
pos
,
key_value_pairs
=
[],
constant_result
=
{})
...
@@ -1814,8 +1809,6 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
...
@@ -1814,8 +1809,6 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
loop_node
=
gen_expr_node
.
loop
loop_node
=
gen_expr_node
.
loop
yield_expression
,
yield_stat_node
=
self
.
_find_single_yield_expression
(
loop_node
)
yield_expression
,
yield_stat_node
=
self
.
_find_single_yield_expression
(
loop_node
)
# FIXME: currently nonfunctional
yield_expression
=
None
if
yield_expression
is
None
:
if
yield_expression
is
None
:
return
node
return
node
...
@@ -1824,22 +1817,18 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
...
@@ -1824,22 +1817,18 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
if
len
(
yield_expression
.
args
)
!=
2
:
if
len
(
yield_expression
.
args
)
!=
2
:
return
node
return
node
result_node
=
ExprNodes
.
InlinedGeneratorExpressionNode
(
node
.
pos
,
gen_expr_node
,
orig_func
=
'dict'
,
comprehension_type
=
Builtin
.
dict_type
)
append_node
=
ExprNodes
.
DictComprehensionAppendNode
(
append_node
=
ExprNodes
.
DictComprehensionAppendNode
(
yield_expression
.
pos
,
yield_expression
.
pos
,
key_expr
=
yield_expression
.
args
[
0
],
key_expr
=
yield_expression
.
args
[
0
],
value_expr
=
yield_expression
.
args
[
1
])
value_expr
=
yield_expression
.
args
[
1
],
target
=
result_node
.
target
)
Visitor
.
recursively_replace_node
(
loop_node
,
yield_stat_node
,
append_node
)
Visitor
.
recursively_replace_node
(
loop_node
,
yield_stat_node
,
append_node
)
return
result_node
dictcomp
=
ExprNodes
.
ComprehensionNode
(
node
.
pos
,
has_local_scope
=
True
,
expr_scope
=
gen_expr_node
.
expr_scope
,
loop
=
loop_node
,
append
=
append_node
,
type
=
Builtin
.
dict_type
)
append_node
.
target
=
dictcomp
return
dictcomp
# specific handlers for general call nodes
# specific handlers for general call nodes
...
...
tests/run/dictcomp.pyx
View file @
5bcbcf84
...
@@ -15,13 +15,9 @@ def dictcomp():
...
@@ -15,13 +15,9 @@ def dictcomp():
assert
x
==
'abc'
# do not leak!
assert
x
==
'abc'
# do not leak!
return
result
return
result
# enable when inlined:
@
cython
.
test_assert_path_exists
(
#@cython.test_fail_if_path_exists(
"//InlinedGeneratorExpressionNode"
,
# "//GeneratorExpressionNode",
"//DictComprehensionAppendNode"
)
# "//SimpleCallNode")
#@cython.test_assert_path_exists(
# "//ComprehensionNode",
# "//ComprehensionNode//DictComprehensionAppendNode")
def
genexpr
():
def
genexpr
():
"""
"""
>>> type(genexpr()) is dict
>>> type(genexpr()) is dict
...
...
tests/run/setcomp.pyx
View file @
5bcbcf84
...
@@ -20,13 +20,9 @@ def setcomp():
...
@@ -20,13 +20,9 @@ def setcomp():
assert
x
==
'abc'
# do not leak
assert
x
==
'abc'
# do not leak
return
result
return
result
# enable when inlined:
@
cython
.
test_assert_path_exists
(
#@cython.test_fail_if_path_exists(
"//InlinedGeneratorExpressionNode"
,
# "//GeneratorExpressionNode",
"//ComprehensionAppendNode"
)
# "//SimpleCallNode")
#@cython.test_assert_path_exists(
# "//ComprehensionNode",
# "//ComprehensionNode//ComprehensionAppendNode")
def
genexp_set
():
def
genexp_set
():
"""
"""
>>> type(genexp_set()) is _set
>>> type(genexp_set()) is _set
...
...
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