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
a53a17ea
Commit
a53a17ea
authored
Dec 31, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise iteration over dict(something).items()
parent
58897392
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
4 deletions
+27
-4
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+15
-4
tests/run/iterdict.pyx
tests/run/iterdict.pyx
+12
-0
No files found.
Cython/Compiler/Optimize.py
View file @
a53a17ea
...
...
@@ -159,14 +159,25 @@ class IterationTransform(Visitor.VisitorTransform):
if
function
.
is_attribute
and
not
reversed
and
not
arg_count
:
base_obj
=
iterator
.
self
or
function
.
obj
method
=
function
.
attribute
# in Py3, items() is equivalent to Py2's iteritems()
is_safe_iter
=
self
.
module_scope
.
context
.
language_level
>=
3
if
not
is_safe_iter
and
method
in
(
'keys'
,
'values'
,
'items'
):
# try to reduce this to the corresponding .iter*() methods
if
isinstance
(
base_obj
,
ExprNodes
.
SimpleCallNode
):
inner_function
=
base_obj
.
function
if
(
inner_function
.
is_name
and
inner_function
.
name
==
'dict'
and
inner_function
.
entry
and
inner_function
.
entry
.
is_builtin
):
# e.g. dict(something).items() => safe to use .iter*()
is_safe_iter
=
True
is_py3
=
self
.
module_scope
.
context
.
language_level
>=
3
keys
=
values
=
False
if
method
==
'iterkeys'
or
(
is_
py3
and
method
==
'keys'
):
if
method
==
'iterkeys'
or
(
is_
safe_iter
and
method
==
'keys'
):
keys
=
True
elif
method
==
'itervalues'
or
(
is_
py3
and
method
==
'values'
):
elif
method
==
'itervalues'
or
(
is_
safe_iter
and
method
==
'values'
):
values
=
True
elif
method
==
'iteritems'
or
(
is_
py3
and
method
==
'items'
):
elif
method
==
'iteritems'
or
(
is_
safe_iter
and
method
==
'items'
):
keys
=
values
=
True
if
keys
or
values
:
...
...
tests/run/iterdict.pyx
View file @
a53a17ea
...
...
@@ -470,3 +470,15 @@ def optimistic_iterdict_change_size(d):
if
count
>
5
:
break
# safety
return
"DONE"
@
cython
.
test_assert_path_exists
(
"//WhileStatNode"
,
"//WhileStatNode//DictIterationNextNode"
)
def
values_of_expression
(
**
kwargs
):
"""
>>> sorted(values_of_expression(a=3, b=4))
[3, 4]
"""
# this can be optimised even in Py2
return
[
arg
for
arg
in
dict
(
kwargs
.
items
()).
values
()
]
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