Skip to content

Commit fcbfd6f

Browse files
jbrockmendelproost
authored andcommitted
release gil more (pandas-dev#29322)
1 parent c12051d commit fcbfd6f

File tree

3 files changed

+252
-241
lines changed

3 files changed

+252
-241
lines changed

pandas/_libs/algos_common_helper.pxi.in

+32-30
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def diff_2d(ndarray[diff_t, ndim=2] arr,
2424
Py_ssize_t periods, int axis):
2525
cdef:
2626
Py_ssize_t i, j, sx, sy, start, stop
27+
bint f_contig = arr.flags.f_contiguous
2728

2829
# Disable for unsupported dtype combinations,
2930
# see https://github.com/cython/cython/issues/2646
@@ -37,40 +38,41 @@ def diff_2d(ndarray[diff_t, ndim=2] arr,
3738
# We put this inside an indented else block to avoid cython build
3839
# warnings about unreachable code
3940
sx, sy = (<object>arr).shape
40-
if arr.flags.f_contiguous:
41-
if axis == 0:
42-
if periods >= 0:
43-
start, stop = periods, sx
41+
with nogil:
42+
if f_contig:
43+
if axis == 0:
44+
if periods >= 0:
45+
start, stop = periods, sx
46+
else:
47+
start, stop = 0, sx + periods
48+
for j in range(sy):
49+
for i in range(start, stop):
50+
out[i, j] = arr[i, j] - arr[i - periods, j]
4451
else:
45-
start, stop = 0, sx + periods
46-
for j in range(sy):
47-
for i in range(start, stop):
48-
out[i, j] = arr[i, j] - arr[i - periods, j]
52+
if periods >= 0:
53+
start, stop = periods, sy
54+
else:
55+
start, stop = 0, sy + periods
56+
for j in range(start, stop):
57+
for i in range(sx):
58+
out[i, j] = arr[i, j] - arr[i, j - periods]
4959
else:
50-
if periods >= 0:
51-
start, stop = periods, sy
60+
if axis == 0:
61+
if periods >= 0:
62+
start, stop = periods, sx
63+
else:
64+
start, stop = 0, sx + periods
65+
for i in range(start, stop):
66+
for j in range(sy):
67+
out[i, j] = arr[i, j] - arr[i - periods, j]
5268
else:
53-
start, stop = 0, sy + periods
54-
for j in range(start, stop):
69+
if periods >= 0:
70+
start, stop = periods, sy
71+
else:
72+
start, stop = 0, sy + periods
5573
for i in range(sx):
56-
out[i, j] = arr[i, j] - arr[i, j - periods]
57-
else:
58-
if axis == 0:
59-
if periods >= 0:
60-
start, stop = periods, sx
61-
else:
62-
start, stop = 0, sx + periods
63-
for i in range(start, stop):
64-
for j in range(sy):
65-
out[i, j] = arr[i, j] - arr[i - periods, j]
66-
else:
67-
if periods >= 0:
68-
start, stop = periods, sy
69-
else:
70-
start, stop = 0, sy + periods
71-
for i in range(sx):
72-
for j in range(start, stop):
73-
out[i, j] = arr[i, j] - arr[i, j - periods]
74+
for j in range(start, stop):
75+
out[i, j] = arr[i, j] - arr[i, j - periods]
7476

7577

7678
# ----------------------------------------------------------------------

pandas/_libs/join.pyx

+79-73
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ def inner_join(const int64_t[:] left, const int64_t[:] right,
2929
left_sorter, left_count = groupsort_indexer(left, max_groups)
3030
right_sorter, right_count = groupsort_indexer(right, max_groups)
3131

32-
# First pass, determine size of result set, do not use the NA group
33-
for i in range(1, max_groups + 1):
34-
lc = left_count[i]
35-
rc = right_count[i]
32+
with nogil:
33+
# First pass, determine size of result set, do not use the NA group
34+
for i in range(1, max_groups + 1):
35+
lc = left_count[i]
36+
rc = right_count[i]
3637

37-
if rc > 0 and lc > 0:
38-
count += lc * rc
38+
if rc > 0 and lc > 0:
39+
count += lc * rc
3940

4041
# exclude the NA group
4142
left_pos = left_count[0]
@@ -44,19 +45,20 @@ def inner_join(const int64_t[:] left, const int64_t[:] right,
4445
left_indexer = np.empty(count, dtype=np.int64)
4546
right_indexer = np.empty(count, dtype=np.int64)
4647

47-
for i in range(1, max_groups + 1):
48-
lc = left_count[i]
49-
rc = right_count[i]
50-
51-
if rc > 0 and lc > 0:
52-
for j in range(lc):
53-
offset = position + j * rc
54-
for k in range(rc):
55-
left_indexer[offset + k] = left_pos + j
56-
right_indexer[offset + k] = right_pos + k
57-
position += lc * rc
58-
left_pos += lc
59-
right_pos += rc
48+
with nogil:
49+
for i in range(1, max_groups + 1):
50+
lc = left_count[i]
51+
rc = right_count[i]
52+
53+
if rc > 0 and lc > 0:
54+
for j in range(lc):
55+
offset = position + j * rc
56+
for k in range(rc):
57+
left_indexer[offset + k] = left_pos + j
58+
right_indexer[offset + k] = right_pos + k
59+
position += lc * rc
60+
left_pos += lc
61+
right_pos += rc
6062

6163
return (_get_result_indexer(left_sorter, left_indexer),
6264
_get_result_indexer(right_sorter, right_indexer))
@@ -79,12 +81,13 @@ def left_outer_join(const int64_t[:] left, const int64_t[:] right,
7981
left_sorter, left_count = groupsort_indexer(left, max_groups)
8082
right_sorter, right_count = groupsort_indexer(right, max_groups)
8183

82-
# First pass, determine size of result set, do not use the NA group
83-
for i in range(1, max_groups + 1):
84-
if right_count[i] > 0:
85-
count += left_count[i] * right_count[i]
86-
else:
87-
count += left_count[i]
84+
with nogil:
85+
# First pass, determine size of result set, do not use the NA group
86+
for i in range(1, max_groups + 1):
87+
if right_count[i] > 0:
88+
count += left_count[i] * right_count[i]
89+
else:
90+
count += left_count[i]
8891

8992
# exclude the NA group
9093
left_pos = left_count[0]
@@ -93,24 +96,25 @@ def left_outer_join(const int64_t[:] left, const int64_t[:] right,
9396
left_indexer = np.empty(count, dtype=np.int64)
9497
right_indexer = np.empty(count, dtype=np.int64)
9598

96-
for i in range(1, max_groups + 1):
97-
lc = left_count[i]
98-
rc = right_count[i]
99+
with nogil:
100+
for i in range(1, max_groups + 1):
101+
lc = left_count[i]
102+
rc = right_count[i]
99103

100-
if rc == 0:
101-
for j in range(lc):
102-
left_indexer[position + j] = left_pos + j
103-
right_indexer[position + j] = -1
104-
position += lc
105-
else:
106-
for j in range(lc):
107-
offset = position + j * rc
108-
for k in range(rc):
109-
left_indexer[offset + k] = left_pos + j
110-
right_indexer[offset + k] = right_pos + k
111-
position += lc * rc
112-
left_pos += lc
113-
right_pos += rc
104+
if rc == 0:
105+
for j in range(lc):
106+
left_indexer[position + j] = left_pos + j
107+
right_indexer[position + j] = -1
108+
position += lc
109+
else:
110+
for j in range(lc):
111+
offset = position + j * rc
112+
for k in range(rc):
113+
left_indexer[offset + k] = left_pos + j
114+
right_indexer[offset + k] = right_pos + k
115+
position += lc * rc
116+
left_pos += lc
117+
right_pos += rc
114118

115119
left_indexer = _get_result_indexer(left_sorter, left_indexer)
116120
right_indexer = _get_result_indexer(right_sorter, right_indexer)
@@ -149,15 +153,16 @@ def full_outer_join(const int64_t[:] left, const int64_t[:] right,
149153
left_sorter, left_count = groupsort_indexer(left, max_groups)
150154
right_sorter, right_count = groupsort_indexer(right, max_groups)
151155

152-
# First pass, determine size of result set, do not use the NA group
153-
for i in range(1, max_groups + 1):
154-
lc = left_count[i]
155-
rc = right_count[i]
156+
with nogil:
157+
# First pass, determine size of result set, do not use the NA group
158+
for i in range(1, max_groups + 1):
159+
lc = left_count[i]
160+
rc = right_count[i]
156161

157-
if rc > 0 and lc > 0:
158-
count += lc * rc
159-
else:
160-
count += lc + rc
162+
if rc > 0 and lc > 0:
163+
count += lc * rc
164+
else:
165+
count += lc + rc
161166

162167
# exclude the NA group
163168
left_pos = left_count[0]
@@ -166,29 +171,30 @@ def full_outer_join(const int64_t[:] left, const int64_t[:] right,
166171
left_indexer = np.empty(count, dtype=np.int64)
167172
right_indexer = np.empty(count, dtype=np.int64)
168173

169-
for i in range(1, max_groups + 1):
170-
lc = left_count[i]
171-
rc = right_count[i]
172-
173-
if rc == 0:
174-
for j in range(lc):
175-
left_indexer[position + j] = left_pos + j
176-
right_indexer[position + j] = -1
177-
position += lc
178-
elif lc == 0:
179-
for j in range(rc):
180-
left_indexer[position + j] = -1
181-
right_indexer[position + j] = right_pos + j
182-
position += rc
183-
else:
184-
for j in range(lc):
185-
offset = position + j * rc
186-
for k in range(rc):
187-
left_indexer[offset + k] = left_pos + j
188-
right_indexer[offset + k] = right_pos + k
189-
position += lc * rc
190-
left_pos += lc
191-
right_pos += rc
174+
with nogil:
175+
for i in range(1, max_groups + 1):
176+
lc = left_count[i]
177+
rc = right_count[i]
178+
179+
if rc == 0:
180+
for j in range(lc):
181+
left_indexer[position + j] = left_pos + j
182+
right_indexer[position + j] = -1
183+
position += lc
184+
elif lc == 0:
185+
for j in range(rc):
186+
left_indexer[position + j] = -1
187+
right_indexer[position + j] = right_pos + j
188+
position += rc
189+
else:
190+
for j in range(lc):
191+
offset = position + j * rc
192+
for k in range(rc):
193+
left_indexer[offset + k] = left_pos + j
194+
right_indexer[offset + k] = right_pos + k
195+
position += lc * rc
196+
left_pos += lc
197+
right_pos += rc
192198

193199
return (_get_result_indexer(left_sorter, left_indexer),
194200
_get_result_indexer(right_sorter, right_indexer))

0 commit comments

Comments
 (0)