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
6c21bf3b
Commit
6c21bf3b
authored
Jun 30, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored tuple unpacking code once again, removed loop for trivial list/tuple unpacking case
parent
17bdd442
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
66 deletions
+52
-66
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+52
-66
No files found.
Cython/Compiler/ExprNodes.py
View file @
6c21bf3b
...
...
@@ -4928,7 +4928,21 @@ class SequenceNode(ExprNode):
or
rhs
.
type
in
(
tuple_type
,
list_type
)
or
not
rhs
.
type
.
is_builtin_type
)
long_enough_for_a_loop
=
len
(
self
.
unpacked_items
)
>
3
if
special_unpack
:
self
.
generate_special_parallel_unpacking_code
(
code
,
rhs
)
code
.
putln
(
"{"
)
self
.
generate_generic_parallel_unpacking_code
(
code
,
rhs
,
self
.
unpacked_items
,
use_loop
=
long_enough_for_a_loop
)
code
.
putln
(
"}"
)
for
value_node
in
self
.
coerced_unpacked_items
:
value_node
.
generate_evaluation_code
(
code
)
for
i
in
range
(
len
(
self
.
args
)):
self
.
args
[
i
].
generate_assignment_code
(
self
.
coerced_unpacked_items
[
i
],
code
)
def
generate_special_parallel_unpacking_code
(
self
,
code
,
rhs
):
tuple_check
=
'likely(PyTuple_CheckExact(%s))'
%
rhs
.
py_result
()
list_check
=
'PyList_CheckExact(%s)'
%
rhs
.
py_result
()
sequence_type_test
=
'1'
...
...
@@ -4943,75 +4957,47 @@ class SequenceNode(ExprNode):
else
:
sequence_types
=
[
'Tuple'
,
'List'
]
sequence_type_test
=
"(%s) || (%s)"
%
(
tuple_check
,
list_check
)
code
.
putln
(
"#if CYTHON_COMPILING_IN_CPYTHON"
)
code
.
putln
(
"if (%s) {"
%
sequence_type_test
)
code
.
putln
(
"PyObject* sequence = %s;"
%
rhs
.
py_result
())
# CPython list/tuple => check size
code
.
putln
(
"Py_ssize_t size = Py_SIZE(sequence);"
)
code
.
putln
(
"if (unlikely(size != %d)) {"
%
len
(
self
.
args
))
code
.
globalstate
.
use_utility_code
(
raise_too_many_values_to_unpack
)
code
.
putln
(
"if (size > %d) __Pyx_RaiseTooManyValuesError(%d);"
%
(
len
(
self
.
args
),
len
(
self
.
args
)))
code
.
globalstate
.
use_utility_code
(
raise_need_more_values_to_unpack
)
code
.
putln
(
"else __Pyx_RaiseNeedMoreValuesError(size);"
)
code
.
putln
(
code
.
error_goto
(
self
.
pos
))
code
.
putln
(
"}"
)
# unpack items from list/tuple in unrolled loop (can't fail)
if
len
(
sequence_types
)
==
2
:
code
.
putln
(
"if (likely(Py%s_CheckExact(sequence))) {"
%
sequence_types
[
0
])
self
.
generate_special_parallel_unpacking_code
(
code
,
sequence_types
[
0
],
use_loop
=
long_enough_for_a_loop
and
sequence_types
[
0
]
!=
'Tuple'
)
for
i
,
item
in
enumerate
(
self
.
unpacked_items
):
code
.
putln
(
"%s = Py%s_GET_ITEM(sequence, %d); "
%
(
item
.
result
(),
sequence_types
[
0
],
i
)
)
if
len
(
sequence_types
)
==
2
:
code
.
putln
(
"} else {"
)
self
.
generate_special_parallel_unpacking_code
(
code
,
sequence_types
[
1
],
use_loop
=
long_enough_for_a_loop
)
for
i
,
item
in
enumerate
(
self
.
unpacked_items
):
code
.
putln
(
"%s = Py%s_GET_ITEM(sequence, %d); "
%
(
item
.
result
(),
sequence_types
[
1
],
i
))
code
.
putln
(
"}"
)
for
item
in
self
.
unpacked_items
:
code
.
put_incref
(
item
.
result
(),
item
.
ctype
())
rhs
.
generate_disposal_code
(
code
)
code
.
putln
(
"} else"
)
if
rhs
.
type
is
tuple_type
:
code
.
putln
(
"if (1) {"
)
code
.
globalstate
.
use_utility_code
(
tuple_unpacking_error_code
)
code
.
putln
(
"__Pyx_UnpackTupleError(%s, %s); %s"
%
(
rhs
.
py_result
(),
len
(
self
.
args
),
code
.
error_goto
(
self
.
pos
)))
# if not a tuple: None => save some code by generating the error directly
code
.
putln
(
"} else if (1) {"
)
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"RaiseNoneIterError"
,
"ObjectHandling.c"
))
code
.
putln
(
"__Pyx_RaiseNoneNotIterableError(); %s"
%
code
.
error_goto
(
self
.
pos
))
code
.
putln
(
"} else"
)
code
.
putln
(
"#endif"
)
code
.
putln
(
"{"
)
self
.
generate_generic_parallel_unpacking_code
(
code
,
rhs
,
self
.
unpacked_items
,
use_loop
=
long_enough_for_a_loop
)
code
.
putln
(
"}"
)
for
value_node
in
self
.
coerced_unpacked_items
:
value_node
.
generate_evaluation_code
(
code
)
for
i
in
range
(
len
(
self
.
args
)):
self
.
args
[
i
].
generate_assignment_code
(
self
.
coerced_unpacked_items
[
i
],
code
)
def
generate_special_parallel_unpacking_code
(
self
,
code
,
sequence_type
,
use_loop
):
code
.
globalstate
.
use_utility_code
(
raise_need_more_values_to_unpack
)
code
.
globalstate
.
use_utility_code
(
raise_too_many_values_to_unpack
)
if
use_loop
:
# must be at the start of a C block!
code
.
putln
(
"PyObject** temps[%s] = {%s};"
%
(
len
(
self
.
unpacked_items
),
','
.
join
([
'&%s'
%
item
.
result
()
for
item
in
self
.
unpacked_items
])))
code
.
putln
(
"if (unlikely(Py%s_GET_SIZE(sequence) != %d)) {"
%
(
sequence_type
,
len
(
self
.
args
)))
code
.
putln
(
"if (Py%s_GET_SIZE(sequence) > %d) __Pyx_RaiseTooManyValuesError(%d);"
%
(
sequence_type
,
len
(
self
.
args
),
len
(
self
.
args
)))
code
.
putln
(
"else __Pyx_RaiseNeedMoreValuesError(Py%s_GET_SIZE(sequence));"
%
sequence_type
)
code
.
putln
(
code
.
error_goto
(
self
.
pos
))
code
.
putln
(
"}"
)
if
use_loop
:
# shorter code in a loop works better for lists in CPython
counter
=
code
.
funcstate
.
allocate_temp
(
PyrexTypes
.
c_py_ssize_t_type
,
manage_ref
=
False
)
code
.
putln
(
"for (%s=0; %s < %s; %s++) {"
%
(
counter
,
counter
,
len
(
self
.
unpacked_items
),
counter
))
code
.
putln
(
"PyObject* item = Py%s_GET_ITEM(sequence, %s);"
%
(
sequence_type
,
counter
))
code
.
putln
(
"*(temps[%s]) = item;"
%
counter
)
code
.
put_incref
(
"item"
,
PyrexTypes
.
py_object_type
)
code
.
putln
(
"}"
)
code
.
funcstate
.
release_temp
(
counter
)
else
:
# unrolling the loop is very fast for tuples in CPython
for
i
,
item
in
enumerate
(
self
.
unpacked_items
):
code
.
putln
(
"%s = Py%s_GET_ITEM(sequence, %d); "
%
(
item
.
result
(),
sequence_type
,
i
))
code
.
put_incref
(
item
.
result
(),
item
.
ctype
())
def
generate_generic_parallel_unpacking_code
(
self
,
code
,
rhs
,
unpacked_items
,
use_loop
,
terminate
=
True
):
code
.
globalstate
.
use_utility_code
(
raise_need_more_values_to_unpack
)
code
.
globalstate
.
use_utility_code
(
UtilityCode
.
load_cached
(
"IterFinish"
,
"ObjectHandling.c"
))
...
...
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