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
Gwenaël Samain
cython
Commits
6c9398f8
Commit
6c9398f8
authored
Jul 30, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise set([x,y,z]) into a set literal
--HG-- extra : rebase_source : 55b0537a6e3c5a410956fb55e2dafc2bf8b30439
parent
7932ecf3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
0 deletions
+45
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+21
-0
tests/run/set.pyx
tests/run/set.pyx
+24
-0
No files found.
Cython/Compiler/Optimize.py
View file @
6c9398f8
...
...
@@ -1957,6 +1957,27 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
exception_value
=
"((double)-1)"
,
exception_check
=
True
)
def
_handle_simple_function_set
(
self
,
node
,
pos_args
):
if
len
(
pos_args
)
==
1
and
isinstance
(
pos_args
[
0
],
(
ExprNodes
.
ListNode
,
ExprNodes
.
TupleNode
)):
# We can optimise set([x,y,z]) safely into a set literal,
# but only if we create all items before adding them -
# adding an item may raise an exception if it is not
# hashable, but creating the later items may have
# side-effects.
args
=
[]
temps
=
[]
for
arg
in
pos_args
[
0
].
args
:
if
not
arg
.
is_simple
():
arg
=
UtilNodes
.
LetRefNode
(
arg
)
temps
.
append
(
arg
)
args
.
append
(
arg
)
result
=
ExprNodes
.
SetNode
(
node
.
pos
,
is_temp
=
1
,
args
=
args
)
for
temp
in
temps
[::
-
1
]:
result
=
UtilNodes
.
EvalWithTempExprNode
(
temp
,
result
)
return
result
return
node
def
_handle_simple_function_float
(
self
,
node
,
pos_args
):
"""Transform float() into either a C type cast or a faster C
function call.
...
...
tests/run/set.pyx
View file @
6c9398f8
...
...
@@ -123,6 +123,30 @@ def test_set_sideeffect_unhashable_failure():
else
:
assert
False
,
"expected exception not raised"
return
L
@
cython
.
test_assert_path_exists
(
"//SetNode"
)
@
cython
.
test_fail_if_path_exists
(
"//SimpleCallNode"
)
def
test_set_of_list
():
"""
>>> s = test_set_of_list()
>>> isinstance(s, _set)
True
>>> sorted(s)
[1, 2, 3]
"""
return
set
([
1
,
2
,
3
])
@
cython
.
test_assert_path_exists
(
"//SetNode"
)
@
cython
.
test_fail_if_path_exists
(
"//SimpleCallNode"
)
def
test_set_of_tuple
():
"""
>>> s = test_set_of_tuple()
>>> isinstance(s, _set)
True
>>> sorted(s)
[1, 2, 3]
"""
return
set
([
1
,
2
,
3
])
def
sorted
(
it
):
# Py3 can't compare strings to ints
chars
=
[]
...
...
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