Skip to content

Commit 24d6736

Browse files
author
fredrik.lundh
committed
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
where appropriate git-svn-id: http://svn.python.org/projects/python/trunk@46468 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c0e3dff commit 24d6736

6 files changed

Lines changed: 55 additions & 41 deletions

File tree

Objects/stringlib/count.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#error must include "stringlib/fastsearch.h" before including this module
88
#endif
99

10-
Py_LOCAL(Py_ssize_t)
10+
Py_LOCAL_INLINE(Py_ssize_t)
1111
stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
1212
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
1313
{

Objects/stringlib/fastsearch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define FAST_COUNT 0
1818
#define FAST_SEARCH 1
1919

20-
Py_LOCAL(Py_ssize_t)
20+
Py_LOCAL_INLINE(Py_ssize_t)
2121
fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
2222
const STRINGLIB_CHAR* p, Py_ssize_t m,
2323
int mode)

Objects/stringlib/find.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#error must include "stringlib/fastsearch.h" before including this module
88
#endif
99

10-
Py_LOCAL(Py_ssize_t)
10+
Py_LOCAL_INLINE(Py_ssize_t)
1111
stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
1212
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
1313
Py_ssize_t offset)
@@ -25,7 +25,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
2525
return pos;
2626
}
2727

28-
Py_LOCAL(Py_ssize_t)
28+
Py_LOCAL_INLINE(Py_ssize_t)
2929
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
3030
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
3131
Py_ssize_t offset)
@@ -50,7 +50,7 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
5050

5151
#ifdef STRINGLIB_STR
5252

53-
Py_LOCAL(Py_ssize_t)
53+
Py_LOCAL_INLINE(Py_ssize_t)
5454
stringlib_find_obj(PyObject* str, PyObject* sub,
5555
Py_ssize_t start, Py_ssize_t end)
5656
{
@@ -60,7 +60,7 @@ stringlib_find_obj(PyObject* str, PyObject* sub,
6060
);
6161
}
6262

63-
Py_LOCAL(int)
63+
Py_LOCAL_INLINE(int)
6464
stringlib_contains_obj(PyObject* str, PyObject* sub)
6565
{
6666
return stringlib_find(
@@ -69,7 +69,7 @@ stringlib_contains_obj(PyObject* str, PyObject* sub)
6969
) != -1;
7070
}
7171

72-
Py_LOCAL(Py_ssize_t)
72+
Py_LOCAL_INLINE(Py_ssize_t)
7373
stringlib_rfind_obj(PyObject* str, PyObject* sub,
7474
Py_ssize_t start, Py_ssize_t end)
7575
{

Objects/stringlib/partition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#error must include "stringlib/fastsearch.h" before including this module
88
#endif
99

10-
Py_LOCAL(PyObject*)
10+
Py_LOCAL_INLINE(PyObject*)
1111
stringlib_partition(
1212
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
1313
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
@@ -51,7 +51,7 @@ stringlib_partition(
5151
return out;
5252
}
5353

54-
Py_LOCAL(PyObject*)
54+
Py_LOCAL_INLINE(PyObject*)
5555
stringlib_rpartition(
5656
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
5757
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len

Objects/stringobject.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
13711371
#define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; }
13721372
#define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; }
13731373

1374-
Py_LOCAL(PyObject *)
1374+
Py_LOCAL_INLINE(PyObject *)
13751375
split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
13761376
{
13771377
Py_ssize_t i, j, count=0;
@@ -1405,7 +1405,7 @@ split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
14051405
return NULL;
14061406
}
14071407

1408-
Py_LOCAL(PyObject *)
1408+
Py_LOCAL_INLINE(PyObject *)
14091409
split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
14101410
{
14111411
register Py_ssize_t i, j, count=0;
@@ -1578,7 +1578,7 @@ string_rpartition(PyStringObject *self, PyObject *sep_obj)
15781578
);
15791579
}
15801580

1581-
Py_LOCAL(PyObject *)
1581+
Py_LOCAL_INLINE(PyObject *)
15821582
rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
15831583
{
15841584
Py_ssize_t i, j, count=0;
@@ -1614,7 +1614,7 @@ rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
16141614
return NULL;
16151615
}
16161616

1617-
Py_LOCAL(PyObject *)
1617+
Py_LOCAL_INLINE(PyObject *)
16181618
rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
16191619
{
16201620
register Py_ssize_t i, j, count=0;
@@ -1828,7 +1828,7 @@ _PyString_Join(PyObject *sep, PyObject *x)
18281828
return string_join((PyStringObject *)sep, x);
18291829
}
18301830

1831-
Py_LOCAL(void)
1831+
Py_LOCAL_INLINE(void)
18321832
string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
18331833
{
18341834
if (*end > len)
@@ -1843,7 +1843,7 @@ string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
18431843
*start = 0;
18441844
}
18451845

1846-
Py_LOCAL(Py_ssize_t)
1846+
Py_LOCAL_INLINE(Py_ssize_t)
18471847
string_find_internal(PyStringObject *self, PyObject *args, int dir)
18481848
{
18491849
const char *s = PyString_AS_STRING(self), *sub;
@@ -1953,7 +1953,7 @@ string_rindex(PyStringObject *self, PyObject *args)
19531953
}
19541954

19551955

1956-
Py_LOCAL(PyObject *)
1956+
Py_LOCAL_INLINE(PyObject *)
19571957
do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
19581958
{
19591959
char *s = PyString_AS_STRING(self);
@@ -1986,7 +1986,7 @@ do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
19861986
}
19871987

19881988

1989-
Py_LOCAL(PyObject *)
1989+
Py_LOCAL_INLINE(PyObject *)
19901990
do_strip(PyStringObject *self, int striptype)
19911991
{
19921992
char *s = PyString_AS_STRING(self);
@@ -2016,7 +2016,7 @@ do_strip(PyStringObject *self, int striptype)
20162016
}
20172017

20182018

2019-
Py_LOCAL(PyObject *)
2019+
Py_LOCAL_INLINE(PyObject *)
20202020
do_argstrip(PyStringObject *self, int striptype, PyObject *args)
20212021
{
20222022
PyObject *sep = NULL;
@@ -2460,7 +2460,7 @@ return_self(PyStringObject *self)
24602460
PyString_GET_SIZE(self));
24612461
}
24622462

2463-
Py_LOCAL(Py_ssize_t)
2463+
Py_LOCAL_INLINE(Py_ssize_t)
24642464
countchar(char *target, int target_len, char c, Py_ssize_t maxcount)
24652465
{
24662466
Py_ssize_t count=0;
@@ -2514,7 +2514,7 @@ findstring(char *target, Py_ssize_t target_len,
25142514
return -1;
25152515
}
25162516

2517-
Py_LOCAL(Py_ssize_t)
2517+
Py_LOCAL_INLINE(Py_ssize_t)
25182518
countstring(char *target, Py_ssize_t target_len,
25192519
char *pattern, Py_ssize_t pattern_len,
25202520
Py_ssize_t start,
@@ -3335,7 +3335,7 @@ string_expandtabs(PyStringObject *self, PyObject *args)
33353335
return u;
33363336
}
33373337

3338-
Py_LOCAL(PyObject *)
3338+
Py_LOCAL_INLINE(PyObject *)
33393339
pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
33403340
{
33413341
PyObject *u;
@@ -4096,7 +4096,7 @@ _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
40964096

40974097
/* Helpers for formatstring */
40984098

4099-
Py_LOCAL(PyObject *)
4099+
Py_LOCAL_INLINE(PyObject *)
41004100
getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
41014101
{
41024102
Py_ssize_t argidx = *p_argidx;
@@ -4125,7 +4125,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
41254125
#define F_ALT (1<<3)
41264126
#define F_ZERO (1<<4)
41274127

4128-
Py_LOCAL(int)
4128+
Py_LOCAL_INLINE(int)
41294129
formatfloat(char *buf, size_t buflen, int flags,
41304130
int prec, int type, PyObject *v)
41314131
{
@@ -4312,7 +4312,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
43124312
return result;
43134313
}
43144314

4315-
Py_LOCAL(int)
4315+
Py_LOCAL_INLINE(int)
43164316
formatint(char *buf, size_t buflen, int flags,
43174317
int prec, int type, PyObject *v)
43184318
{
@@ -4384,7 +4384,7 @@ formatint(char *buf, size_t buflen, int flags,
43844384
return (int)strlen(buf);
43854385
}
43864386

4387-
Py_LOCAL(int)
4387+
Py_LOCAL_INLINE(int)
43884388
formatchar(char *buf, size_t buflen, PyObject *v)
43894389
{
43904390
/* presume that the buffer is at least 2 characters long */

Objects/unicodeobject.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static BLOOM_MASK bloom_linebreak;
141141
#define BLOOM_LINEBREAK(ch)\
142142
(BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch)))
143143

144-
Py_LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
144+
Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
145145
{
146146
/* calculate simple bloom-style bitmask for a given unicode string */
147147

@@ -155,7 +155,7 @@ Py_LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
155155
return mask;
156156
}
157157

158-
Py_LOCAL(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
158+
Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
159159
{
160160
Py_ssize_t i;
161161

@@ -2015,7 +2015,7 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s,
20152015
20162016
*/
20172017

2018-
Py_LOCAL(const Py_UNICODE *) findchar(const Py_UNICODE *s,
2018+
Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,
20192019
Py_ssize_t size,
20202020
Py_UNICODE ch)
20212021
{
@@ -3860,7 +3860,7 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s,
38603860
#define STRINGLIB_NEW PyUnicode_FromUnicode
38613861
#define STRINGLIB_STR PyUnicode_AS_UNICODE
38623862

3863-
Py_LOCAL(int)
3863+
Py_LOCAL_INLINE(int)
38643864
STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len)
38653865
{
38663866
if (str[0] != other[0])
@@ -4710,7 +4710,7 @@ PyObject *replace(PyUnicodeObject *self,
47104710
}
47114711
} else {
47124712

4713-
Py_ssize_t n, i;
4713+
Py_ssize_t n, i, j, e;
47144714
Py_ssize_t product, new_size, delta;
47154715
Py_UNICODE *p;
47164716

@@ -4743,21 +4743,35 @@ PyObject *replace(PyUnicodeObject *self,
47434743
return NULL;
47444744
i = 0;
47454745
p = u->str;
4746+
e = self->length - str1->length;
47464747
if (str1->length > 0) {
4747-
while (i <= self->length - str1->length)
4748-
if (Py_UNICODE_MATCH(self, i, str1)) {
4749-
/* replace string segment */
4748+
while (n-- > 0) {
4749+
/* look for next match */
4750+
j = i;
4751+
while (j <= e) {
4752+
if (Py_UNICODE_MATCH(self, j, str1))
4753+
break;
4754+
j++;
4755+
}
4756+
if (j > i) {
4757+
if (j > e)
4758+
break;
4759+
/* copy unchanged part [i:j] */
4760+
Py_UNICODE_COPY(p, self->str+i, j-i);
4761+
p += j - i;
4762+
}
4763+
/* copy substitution string */
4764+
if (str2->length > 0) {
47504765
Py_UNICODE_COPY(p, str2->str, str2->length);
47514766
p += str2->length;
4752-
i += str1->length;
4753-
if (--n <= 0) {
4754-
/* copy remaining part */
4755-
Py_UNICODE_COPY(p, self->str+i, self->length-i);
4756-
break;
4757-
}
4758-
} else
4759-
*p++ = self->str[i++];
4767+
}
4768+
i = j + str1->length;
4769+
}
4770+
if (i < self->length)
4771+
/* copy tail [i:] */
4772+
Py_UNICODE_COPY(p, self->str+i, self->length-i);
47604773
} else {
4774+
/* interleave */
47614775
while (n > 0) {
47624776
Py_UNICODE_COPY(p, str2->str, str2->length);
47634777
p += str2->length;

0 commit comments

Comments
 (0)