Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions graalpython/com.oracle.graal.python.cext/src/capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ CAPI_BUILTINS
}

Py_LOCAL_SYMBOL uint32_t Py_Truffle_Options;
Py_LOCAL_SYMBOL void graalpy_init_complex_alloc(void); // complexobject.c
Py_LOCAL_SYMBOL void graalpy_init_list_slots(void); // listobject.c
Py_LOCAL_SYMBOL void graalpy_init_long_alloc(void); // longobject.c
Py_LOCAL_SYMBOL void graalpy_init_str(void); // unicodeobject.c
Py_LOCAL_SYMBOL void graalpy_init_small_alloc(void); // obmalloc.c
Py_LOCAL_SYMBOL void graalpy_init_dict_mirror(void); // dictobject.c

#undef bool
Expand Down Expand Up @@ -602,6 +606,14 @@ PyAPI_FUNC(PyThreadState **) initialize_graal_capi(void **builtin_closures, GCSt
Py_Truffle_Options = GraalPyPrivate_Native_Options();

initialize_builtin_types_and_structs();
// GraalPy change: shared native allocator for exact complex
graalpy_init_complex_alloc();
// GraalPy change: shared native allocator for exact int
graalpy_init_long_alloc();
// GraalPy change: native str values
graalpy_init_str();
// GraalPy change: small-block pool for PyObject_Malloc shared with Java (floats)
graalpy_init_small_alloc();
// GraalPy change: PyDict_Next entry mirror ("Java ran" epoch)
graalpy_init_dict_mirror();
// GraalPy change: PyList_Type's sq_length/mp_length read the list stub's mirrored ob_size
Expand Down
180 changes: 174 additions & 6 deletions graalpython/com.oracle.graal.python.cext/src/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,174 @@ class complex "PyComplexObject *" "&PyComplex_Type"
#include "clinic/complexobject.c.h"
#endif // GraalPy change

/* GraalPy change: Exact complex objects are native structs allocated from a size-class
* free list shared with Java (com.oracle.graal.python.runtime.nativeaccess.NativeComplex); the layouts below
* must match the Java offsets. Single-threaded (GIL). */
typedef struct {
PyComplexObject obj; /* ob_refcnt @0, ob_type @8, cval @16 */
int64_t serial; /* @32, 0 = free */
int64_t in_fifo; /* @40 */
} GraalPyComplexBlock;

#define GRAALPY_COMPLEX_FIFO_SIZE 64
#define GRAALPY_COMPLEX_ARENA_SIZE (64 * 1024)
#define GRAALPY_COMPLEX_ARENA_HEADER 16
enum { GC_ALLOC = 0, GC_FREE = 1, GC_INCREF = 2, GC_DECREF = 3, GC_STALE = 4, GC_EVICT_FREED = 5, GC_EVICT_KEPT = 6 };

typedef struct {
GraalPyComplexBlock *free_list; /* @0 */
char *bump; /* @8 */
char *end; /* @16 */
int64_t serial; /* @24 */
PyTypeObject *type; /* @32 */
int64_t fifo_pos; /* @40 */
int64_t flags; /* @48: bit 0 = counters on */
char *arenas; /* @56 */
int64_t counters[16]; /* @64 */
struct {
GraalPyComplexBlock *p;
int64_t serial;
} fifo[GRAALPY_COMPLEX_FIFO_SIZE]; /* @192 */
} GraalPyComplexAllocState;

_Static_assert(sizeof(GraalPyComplexBlock) == 48, "complex block layout");
_Static_assert(offsetof(GraalPyComplexAllocState, fifo) == 192, "complex state layout");

static GraalPyComplexAllocState *graalpy_complex_state;
static destructor graalpy_complex_orig_dealloc;

#define GRAALPY_COMPLEX_COUNT(IDX) do { if (graalpy_complex_state->flags & 1) graalpy_complex_state->counters[IDX]++; } while (0)
/* The free list link is stored in the bytes of a free block's first value field. memcpy keeps
this within the aliasing rules; compilers turn it into a plain load or store. */
static inline GraalPyComplexBlock *
graalpy_complex_get_link(GraalPyComplexBlock *b)
{
GraalPyComplexBlock *next;
memcpy(&next, &b->obj.cval.real, sizeof(next));
return next;
}

static inline void
graalpy_complex_set_link(GraalPyComplexBlock *b, GraalPyComplexBlock *next)
{
memcpy(&b->obj.cval.real, &next, sizeof(next));
}

static GraalPyComplexBlock *
graalpy_complex_new_arena(GraalPyComplexAllocState *st)
{
char *arena = (char *) malloc(GRAALPY_COMPLEX_ARENA_SIZE);
if (arena == NULL) {
return NULL;
}
*(char **) arena = st->arenas;
st->arenas = arena;
char *p = arena + GRAALPY_COMPLEX_ARENA_HEADER;
st->bump = p + sizeof(GraalPyComplexBlock);
st->end = arena + GRAALPY_COMPLEX_ARENA_SIZE;
return (GraalPyComplexBlock *) p;
}

/* New exact complex with refcount 1 (owned by the caller). */
static PyObject *
graalpy_complex_alloc(double real, double imag)
{
GraalPyComplexAllocState *st = graalpy_complex_state;
GraalPyComplexBlock *b = st->free_list;
if (b != NULL) {
st->free_list = graalpy_complex_get_link(b);
} else if (st->bump + sizeof(GraalPyComplexBlock) <= st->end) {
b = (GraalPyComplexBlock *) st->bump;
st->bump += sizeof(GraalPyComplexBlock);
} else {
b = graalpy_complex_new_arena(st);
if (b == NULL) {
return PyErr_NoMemory();
}
}
b->serial = ++st->serial;
b->in_fifo = 0;
Py_SET_REFCNT((PyObject *) b, 1);
Py_SET_TYPE((PyObject *) b, st->type);
b->obj.cval.real = real;
b->obj.cval.imag = imag;
GRAALPY_COMPLEX_COUNT(GC_ALLOC);
return (PyObject *) b;
}

static void
graalpy_complex_free_block(GraalPyComplexAllocState *st, GraalPyComplexBlock *b)
{
b->serial = 0;
graalpy_complex_set_link(b, st->free_list);
st->free_list = b;
GRAALPY_COMPLEX_COUNT(GC_FREE);
}

/* Same algorithm as NativeComplex.enqueue: a zero-count struct waits in the FIFO; the evicted entry is freed
* if it is the same incarnation (serial) and its count is still zero. */
static void
graalpy_complex_enqueue(GraalPyComplexBlock *b)
{
GraalPyComplexAllocState *st = graalpy_complex_state;
if (b->in_fifo) {
return;
}
b->in_fifo = 1;
int64_t pos = st->fifo_pos;
int idx = (int) (pos & (GRAALPY_COMPLEX_FIFO_SIZE - 1));
GraalPyComplexBlock *old = st->fifo[idx].p;
if (old != NULL) {
if (old->serial == st->fifo[idx].serial) {
old->in_fifo = 0;
if (Py_REFCNT((PyObject *) old) == 0) {
graalpy_complex_free_block(st, old);
GRAALPY_COMPLEX_COUNT(GC_EVICT_FREED);
} else {
GRAALPY_COMPLEX_COUNT(GC_EVICT_KEPT);
}
} else {
GRAALPY_COMPLEX_COUNT(GC_EVICT_KEPT);
}
}
st->fifo[idx].p = b;
st->fifo[idx].serial = b->serial;
st->fifo_pos = pos + 1;
}

static void
graalpy_complex_dealloc(PyObject *op)
{
if (Py_TYPE(op) != &PyComplex_Type) {
/* native subtype that inherited this slot */
if (graalpy_complex_orig_dealloc != NULL) {
graalpy_complex_orig_dealloc(op);
}
return;
}
graalpy_complex_enqueue((GraalPyComplexBlock *) op);
}

static PyObject *
graalpy_complex_tp_alloc(PyTypeObject *type, Py_ssize_t nitems)
{
if (type == &PyComplex_Type && nitems == 0) {
return graalpy_complex_alloc(0.0, 0.0);
}
return PyType_GenericAlloc(type, nitems);
}

Py_LOCAL_SYMBOL void
graalpy_init_complex_alloc(void)
{
graalpy_complex_state = (GraalPyComplexAllocState *) GraalPyPrivate_Complex_AllocState();
graalpy_complex_state->type = &PyComplex_Type;
graalpy_complex_orig_dealloc = PyComplex_Type.tp_dealloc;
PyComplex_Type.tp_dealloc = graalpy_complex_dealloc;
PyComplex_Type.tp_alloc = graalpy_complex_tp_alloc;
}
/* end GraalPy change */

/* elementary operations on complex numbers */

static Py_complex c_1 = {1., 0.};
Expand Down Expand Up @@ -226,6 +394,10 @@ static PyObject *
complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
{
PyObject *op;
// GraalPy change: exact complex from the shared allocator
if (type == &PyComplex_Type) {
return graalpy_complex_alloc(cval.real, cval.imag);
}

op = type->tp_alloc(type, 0);
if (op != NULL)
Expand All @@ -249,16 +421,12 @@ GraalPyPrivate_Complex_SubtypeFromDoubles(PyTypeObject *type, double real, doubl
return complex_subtype_from_c_complex(type, c);
}

#if 0 // GraalPy change
PyObject *
PyComplex_FromDoubles(double real, double imag)
{
Py_complex c;
c.real = real;
c.imag = imag;
return PyComplex_FromCComplex(c);
// GraalPy change: no upcall: shared native allocator
return graalpy_complex_alloc(real, imag);
}
#endif // GraalPy change

static PyObject * try_complex_special_method(PyObject *);

Expand Down
11 changes: 9 additions & 2 deletions graalpython/com.oracle.graal.python.cext/src/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,16 @@ PyFloat_FromDouble(double fval)
// GraalPy: different implementation
if (fval == (float)fval) {
return float_to_pointer(fval);
} else {
return GraalPyPrivate_Float_FromDouble(fval);
}
// A native PyFloatObject struct (PyObject_Malloc: small-block pool shared with Java) instead of
// an upcall that creates a Java PFloat and a handle-space stub
op = (PyFloatObject *) PyObject_Malloc(sizeof(PyFloatObject));
if (op == NULL) {
return PyErr_NoMemory();
}
(void) PyObject_Init((PyObject *) op, &PyFloat_Type);
op->ob_fval = fval;
return (PyObject *) op;
}
#if 0 // GraalPy change
#ifdef WITH_FREELISTS
Expand Down
Loading
Loading