Commit 3099a9c1 authored by Marius Wachtler's avatar Marius Wachtler

intern ints from -5 to 256 like cpython does

this increases the compatibility if some one is using 'is' identity comparisons
instead of equality '==' comparisons.
parent 1102c455
...@@ -1359,9 +1359,9 @@ void setupInt() { ...@@ -1359,9 +1359,9 @@ void setupInt() {
static PyNumberMethods int_as_number; static PyNumberMethods int_as_number;
int_cls->tp_as_number = &int_as_number; int_cls->tp_as_number = &int_as_number;
for (int i = 0; i < NUM_INTERNED_INTS; i++) { for (int i = MIN_INTERNED_INT; i <= MAX_INTERNED_INT; i++) {
interned_ints[i] = new BoxedInt(i); interned_ints[-MIN_INTERNED_INT + i] = new BoxedInt(i);
gc::registerPermanentRoot(interned_ints[i]); gc::registerPermanentRoot(interned_ints[-MIN_INTERNED_INT + i]);
} }
int_cls->giveAttr("__getnewargs__", new BoxedFunction(FunctionMetadata::create((void*)int_getnewargs, UNKNOWN, 1, int_cls->giveAttr("__getnewargs__", new BoxedFunction(FunctionMetadata::create((void*)int_getnewargs, UNKNOWN, 1,
......
...@@ -1163,11 +1163,15 @@ inline BoxedString* boxString(llvm::StringRef s) { ...@@ -1163,11 +1163,15 @@ inline BoxedString* boxString(llvm::StringRef s) {
return new (s.size()) BoxedString(s); return new (s.size()) BoxedString(s);
} }
#define NUM_INTERNED_INTS 100 #define MIN_INTERNED_INT -5 // inclusive
#define MAX_INTERNED_INT 256 // inclusive
static_assert(MIN_INTERNED_INT < 0 && MAX_INTERNED_INT > 0, "");
#define NUM_INTERNED_INTS ((-MIN_INTERNED_INT) + MAX_INTERNED_INT + 1)
extern BoxedInt* interned_ints[NUM_INTERNED_INTS]; extern BoxedInt* interned_ints[NUM_INTERNED_INTS];
extern "C" inline Box* boxInt(int64_t n) { extern "C" inline Box* boxInt(int64_t n) {
if (0 <= n && n < NUM_INTERNED_INTS) { if (n >= MIN_INTERNED_INT && n <= MAX_INTERNED_INT) {
return interned_ints[n]; return interned_ints[(-MIN_INTERNED_INT) + n];
} }
return new BoxedInt(n); return new BoxedInt(n);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment