Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bb8a05

Browse files
Several optimizations and speed improvements.
cstubs: Use Matrix type instead of float[4][4].
1 parent a75d306 commit 3bb8a05

10 files changed

Lines changed: 216 additions & 63 deletions

File tree

Include/stringobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ functions should be applied to nil objects.
5151

5252
typedef struct {
5353
OB_VARHEAD
54+
#ifdef CACHE_HASH
55+
long ob_shash;
56+
#endif
5457
char ob_sval[1];
5558
} stringobject;
5659

Modules/cstubs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,21 +529,21 @@ gl_getmatrix(self, args)
529529
object *self;
530530
object *args;
531531
{
532-
float arg1 [ 16 ] ;
532+
Matrix arg1;
533533
object *v, *w;
534-
int i;
534+
int i, j;
535535
getmatrix( arg1 );
536536
v = newlistobject(16);
537537
if (v == NULL) {
538538
return err_nomem();
539539
}
540-
for (i = 0; i < 16; i++) {
541-
w = mknewfloatobject(arg1[i]);
540+
for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) {
541+
w = mknewfloatobject(arg1[i][j]);
542542
if (w == NULL) {
543543
DECREF(v);
544544
return NULL;
545545
}
546-
setlistitem(v, i, w);
546+
setlistitem(v, i*4+j, w);
547547
}
548548
return v;
549549
}
@@ -559,7 +559,7 @@ gl_altgetmatrix(self, args)
559559
object *self;
560560
object *args;
561561
{
562-
float arg1 [ 4 ] [ 4 ] ;
562+
Matrix arg1;
563563
object *v, *w;
564564
int i, j;
565565
getmatrix( arg1 );
@@ -742,7 +742,7 @@ gl_packrect(self, args)
742742
if (packed == NULL)
743743
return NULL;
744744
parray = (unsigned long *) getstringvalue(unpacked);
745-
p = getstringvalue(packed);
745+
p = (unsigned char *) getstringvalue(packed);
746746
for (y = 0; y < height; y += packfactor, parray += packfactor*width) {
747747
for (x = 0; x < width; x += packfactor) {
748748
pixel = parray[x];

Modules/stropmodule.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,26 @@ strop_lower(self, args)
254254
object *self; /* Not used */
255255
object *args;
256256
{
257-
char *s;
257+
char *s, *s_new;
258258
int i, n;
259259
object *new;
260260
int changed;
261261

262262
if (!getargs(args, "s#", &s, &n))
263263
return NULL;
264-
new = newsizedstringobject(s, n);
264+
new = newsizedstringobject(NULL, n);
265265
if (new == NULL)
266266
return NULL;
267-
s = getstringvalue(new);
267+
s_new = getstringvalue(new);
268268
changed = 0;
269269
for (i = 0; i < n; i++) {
270-
char c = s[i];
270+
char c = *s++;
271271
if (isupper(c)) {
272272
changed = 1;
273-
s[i] = tolower(c);
274-
}
273+
*s_new = tolower(c);
274+
} else
275+
*s_new = c;
276+
s_new++;
275277
}
276278
if (!changed) {
277279
DECREF(new);
@@ -287,24 +289,26 @@ strop_upper(self, args)
287289
object *self; /* Not used */
288290
object *args;
289291
{
290-
char *s;
292+
char *s, *s_new;
291293
int i, n;
292294
object *new;
293295
int changed;
294296

295297
if (!getargs(args, "s#", &s, &n))
296298
return NULL;
297-
new = newsizedstringobject(s, n);
299+
new = newsizedstringobject(NULL, n);
298300
if (new == NULL)
299301
return NULL;
300-
s = getstringvalue(new);
302+
s_new = getstringvalue(new);
301303
changed = 0;
302304
for (i = 0; i < n; i++) {
303-
char c = s[i];
305+
char c = *s++;
304306
if (islower(c)) {
305307
changed = 1;
306-
s[i] = toupper(c);
307-
}
308+
*s_new = toupper(c);
309+
} else
310+
*s_new = c;
311+
s_new++;
308312
}
309313
if (!changed) {
310314
DECREF(new);
@@ -320,28 +324,31 @@ strop_swapcase(self, args)
320324
object *self; /* Not used */
321325
object *args;
322326
{
323-
char *s;
327+
char *s, *s_new;
324328
int i, n;
325329
object *new;
326330
int changed;
327331

328332
if (!getargs(args, "s#", &s, &n))
329333
return NULL;
330-
new = newsizedstringobject(s, n);
334+
new = newsizedstringobject(NULL, n);
331335
if (new == NULL)
332336
return NULL;
333-
s = getstringvalue(new);
337+
s_new = getstringvalue(new);
334338
changed = 0;
335339
for (i = 0; i < n; i++) {
336-
char c = s[i];
340+
char c = *s++;
337341
if (islower(c)) {
338342
changed = 1;
339-
s[i] = toupper(c);
343+
*s_new = toupper(c);
340344
}
341345
else if (isupper(c)) {
342346
changed = 1;
343-
s[i] = tolower(c);
347+
*s_new = tolower(c);
344348
}
349+
else
350+
*s_new = c;
351+
s_new++;
345352
}
346353
if (!changed) {
347354
DECREF(new);

Objects/accessobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,11 @@ isprivileged(caller)
232232
object *caller;
233233
{
234234
object *g;
235-
if (caller != NULL && hasattr(caller, "__privileged__"))
235+
static char privileged[] = "__privileged__";
236+
if (caller != NULL && hasattr(caller, privileged))
236237
return 1;
237238
g = getglobals();
238-
if (g != NULL && dictlookup(g, "__privileged__"))
239+
if (g != NULL && dictlookup(g, privileged))
239240
return 1;
240241
return 0;
241242
}

Objects/classobject.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,15 @@ instance_getattr(inst, name)
355355
{
356356
register object *v;
357357
classobject *class;
358-
if (strcmp(name, "__dict__") == 0) {
359-
INCREF(inst->in_dict);
360-
return inst->in_dict;
361-
}
362-
if (strcmp(name, "__class__") == 0) {
363-
INCREF(inst->in_class);
364-
return (object *)inst->in_class;
358+
if (name[0] == '_' && name[1] == '_') {
359+
if (strcmp(name, "__dict__") == 0) {
360+
INCREF(inst->in_dict);
361+
return inst->in_dict;
362+
}
363+
if (strcmp(name, "__class__") == 0) {
364+
INCREF(inst->in_class);
365+
return (object *)inst->in_class;
366+
}
365367
}
366368
class = NULL;
367369
v = dictlookup(inst->in_dict, name);

Objects/dictobject.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ mappinglookup(op, key)
242242
err_badcall();
243243
return NULL;
244244
}
245+
#ifdef CACHE_HASH
246+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
247+
#endif
245248
hash = hashobject(key);
246249
if (hash == -1)
247250
return NULL;
@@ -260,6 +263,9 @@ mappinginsert(op, key, value)
260263
err_badcall();
261264
return -1;
262265
}
266+
#ifdef CACHE_HASH
267+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
268+
#endif
263269
hash = hashobject(key);
264270
if (hash == -1)
265271
return -1;
@@ -289,6 +295,9 @@ mappingremove(op, key)
289295
err_badcall();
290296
return -1;
291297
}
298+
#ifdef CACHE_HASH
299+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
300+
#endif
292301
hash = hashobject(key);
293302
if (hash == -1)
294303
return -1;
@@ -447,7 +456,11 @@ mapping_subscript(mp, key)
447456
register object *key;
448457
{
449458
object *v;
450-
long hash = hashobject(key);
459+
long hash;
460+
#ifdef CACHE_HASH
461+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
462+
#endif
463+
hash = hashobject(key);
451464
if (hash == -1)
452465
return NULL;
453466
v = lookmapping(mp, key, hash) -> me_value;
@@ -628,9 +641,15 @@ mapping_compare(a, b)
628641
res = cmpobject(akey, bkey);
629642
if (res != 0)
630643
break;
644+
#ifdef CACHE_HASH
645+
if (!is_stringobject(akey) || (ahash = ((stringobject *) akey)->ob_shash) == -1)
646+
#endif
631647
ahash = hashobject(akey);
632648
if (ahash == -1)
633649
err_clear(); /* Don't want errors here */
650+
#ifdef CACHE_HASH
651+
if (!is_stringobject(bkey) || (bhash = ((stringobject *) bkey)->ob_shash) == -1)
652+
#endif
634653
bhash = hashobject(bkey);
635654
if (bhash == -1)
636655
err_clear(); /* Don't want errors here */
@@ -661,6 +680,9 @@ mapping_has_key(mp, args)
661680
register long ok;
662681
if (!getargs(args, "O", &key))
663682
return NULL;
683+
#ifdef CACHE_HASH
684+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
685+
#endif
664686
hash = hashobject(key);
665687
if (hash == -1)
666688
return NULL;

Objects/mappingobject.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ mappinglookup(op, key)
242242
err_badcall();
243243
return NULL;
244244
}
245+
#ifdef CACHE_HASH
246+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
247+
#endif
245248
hash = hashobject(key);
246249
if (hash == -1)
247250
return NULL;
@@ -260,6 +263,9 @@ mappinginsert(op, key, value)
260263
err_badcall();
261264
return -1;
262265
}
266+
#ifdef CACHE_HASH
267+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
268+
#endif
263269
hash = hashobject(key);
264270
if (hash == -1)
265271
return -1;
@@ -289,6 +295,9 @@ mappingremove(op, key)
289295
err_badcall();
290296
return -1;
291297
}
298+
#ifdef CACHE_HASH
299+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
300+
#endif
292301
hash = hashobject(key);
293302
if (hash == -1)
294303
return -1;
@@ -447,7 +456,11 @@ mapping_subscript(mp, key)
447456
register object *key;
448457
{
449458
object *v;
450-
long hash = hashobject(key);
459+
long hash;
460+
#ifdef CACHE_HASH
461+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
462+
#endif
463+
hash = hashobject(key);
451464
if (hash == -1)
452465
return NULL;
453466
v = lookmapping(mp, key, hash) -> me_value;
@@ -628,9 +641,15 @@ mapping_compare(a, b)
628641
res = cmpobject(akey, bkey);
629642
if (res != 0)
630643
break;
644+
#ifdef CACHE_HASH
645+
if (!is_stringobject(akey) || (ahash = ((stringobject *) akey)->ob_shash) == -1)
646+
#endif
631647
ahash = hashobject(akey);
632648
if (ahash == -1)
633649
err_clear(); /* Don't want errors here */
650+
#ifdef CACHE_HASH
651+
if (!is_stringobject(bkey) || (bhash = ((stringobject *) bkey)->ob_shash) == -1)
652+
#endif
634653
bhash = hashobject(bkey);
635654
if (bhash == -1)
636655
err_clear(); /* Don't want errors here */
@@ -661,6 +680,9 @@ mapping_has_key(mp, args)
661680
register long ok;
662681
if (!getargs(args, "O", &key))
663682
return NULL;
683+
#ifdef CACHE_HASH
684+
if (!is_stringobject(key) || (hash = ((stringobject *) key)->ob_shash) == -1)
685+
#endif
664686
hash = hashobject(key);
665687
if (hash == -1)
666688
return NULL;

Objects/object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ long ref_total;
3838
static typeobject *type_list;
3939
extern int tuple_zero_allocs, fast_tuple_allocs;
4040
extern int quick_int_allocs, quick_neg_int_allocs;
41+
extern int null_strings, one_strings;
4142
void
4243
dump_counts()
4344
{
@@ -51,6 +52,7 @@ dump_counts()
5152
tuple_zero_allocs);
5253
printf("fast int allocs: pos: %d, neg: %d\n", quick_int_allocs,
5354
quick_neg_int_allocs);
55+
printf("null strings: %d, 1-strings: %d\n", null_strings, one_strings);
5456
}
5557

5658
void

0 commit comments

Comments
 (0)