Skip to content

Commit 38f7e7c

Browse files
committed
Store decimal constraints in a single details slot holding a 5-tuple
Frees up 4 bits in the types bitmask (main now uses bits 38-39 for frozendict, leaving too few free bits for per-constraint decimal bits). Also compare the multiple_of remainder against zero explicitly instead of relying on __bool__.
1 parent f59ed16 commit 38f7e7c

2 files changed

Lines changed: 110 additions & 126 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 93 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,11 +2830,11 @@ AssocList_Sort(AssocList* list) {
28302830
#define MS_TYPE_NAMEDTUPLE (1ull << 35)
28312831
#define MS_TYPE_BOOLLITERAL_TRUE (1ull << 36)
28322832
#define MS_TYPE_BOOLLITERAL_FALSE (1ull << 37)
2833-
/* Decimal constraints - stored as PyObject* (Decimal instances) */
2834-
#define MS_CONSTR_DECIMAL_GT (1ull << 38)
2835-
#define MS_CONSTR_DECIMAL_GE (1ull << 39)
2836-
#define MS_CONSTR_DECIMAL_LT (1ull << 40)
2837-
#define MS_CONSTR_DECIMAL_LE (1ull << 41)
2833+
/* Decimal constraints - a single bit, the details slot holds a 5-tuple
2834+
* (gt, ge, lt, le, multiple_of) of Decimal instances (None when unset).
2835+
* Grouped into one slot since there aren't enough free bits left for
2836+
* per-constraint bits. */
2837+
#define MS_CONSTR_DECIMAL (1ull << 38)
28382838
/* Constraints */
28392839
#define MS_CONSTR_INT_MIN (1ull << 42)
28402840
#define MS_CONSTR_INT_MAX (1ull << 43)
@@ -2855,7 +2855,6 @@ AssocList_Sort(AssocList* list) {
28552855
#define MS_CONSTR_MAP_MAX_LENGTH (1ull << 58)
28562856
#define MS_CONSTR_TZ_AWARE (1ull << 59)
28572857
#define MS_CONSTR_TZ_NAIVE (1ull << 60)
2858-
#define MS_CONSTR_DECIMAL_MULTIPLE_OF (1ull << 61)
28592858
/* Extra flag bit, used by TypedDict/dataclass implementations */
28602859
#define MS_EXTRA_FLAG (1ull << 63)
28612860

@@ -2897,9 +2896,7 @@ AssocList_Sort(AssocList* list) {
28972896
* S | ARRAY_MAX_LENGTH |
28982897
* S | MAP_MIN_LENGTH |
28992898
* S | MAP_MAX_LENGTH |
2900-
* O | DECIMAL_GT | DECIMAL_GE |
2901-
* O | DECIMAL_LT | DECIMAL_LE |
2902-
* O | DECIMAL_MULTIPLE_OF |
2899+
* O | DECIMAL [5-tuple (gt, ge, lt, le, multiple_of)] |
29032900
* T | FIXTUPLE [size, types ...] |
29042901
* */
29052902

@@ -2929,14 +2926,11 @@ AssocList_Sort(AssocList* list) {
29292926
#define SLOT_19 MS_CONSTR_ARRAY_MAX_LENGTH
29302927
#define SLOT_20 MS_CONSTR_MAP_MIN_LENGTH
29312928
#define SLOT_21 MS_CONSTR_MAP_MAX_LENGTH
2932-
#define SLOT_22 (MS_CONSTR_DECIMAL_GT | MS_CONSTR_DECIMAL_GE)
2933-
#define SLOT_23 (MS_CONSTR_DECIMAL_LT | MS_CONSTR_DECIMAL_LE)
2934-
#define SLOT_24 MS_CONSTR_DECIMAL_MULTIPLE_OF
2929+
#define SLOT_22 MS_CONSTR_DECIMAL
29352930

29362931
/* Common groups */
29372932
#define MS_INT_CONSTRS (SLOT_08 | SLOT_09 | SLOT_10)
29382933
#define MS_FLOAT_CONSTRS (SLOT_11 | SLOT_12 | SLOT_13)
2939-
#define MS_DECIMAL_CONSTRS (SLOT_22 | SLOT_23 | SLOT_24)
29402934
#define MS_STR_CONSTRS (SLOT_05 | SLOT_14 | SLOT_15)
29412935
#define MS_BYTES_CONSTRS (SLOT_16 | SLOT_17)
29422936
#define MS_ARRAY_CONSTRS (SLOT_18 | SLOT_19)
@@ -3372,7 +3366,7 @@ TypeNode_get_constr_map_max_length(TypeNode *type) {
33723366
}
33733367

33743368
static MS_INLINE PyObject *
3375-
TypeNode_get_constr_decimal_min(TypeNode *type) {
3369+
TypeNode_get_constr_decimal(TypeNode *type) {
33763370
Py_ssize_t i = ms_popcount(
33773371
type->types & (
33783372
SLOT_00 | SLOT_01 | SLOT_02 | SLOT_03 | SLOT_04 | SLOT_05 | SLOT_06 | SLOT_07 |
@@ -3383,37 +3377,13 @@ TypeNode_get_constr_decimal_min(TypeNode *type) {
33833377
return (PyObject *)(type->details[i].pointer);
33843378
}
33853379

3386-
static MS_INLINE PyObject *
3387-
TypeNode_get_constr_decimal_max(TypeNode *type) {
3388-
Py_ssize_t i = ms_popcount(
3389-
type->types & (
3390-
SLOT_00 | SLOT_01 | SLOT_02 | SLOT_03 | SLOT_04 | SLOT_05 | SLOT_06 | SLOT_07 |
3391-
SLOT_08 | SLOT_09 | SLOT_10 | SLOT_11 | SLOT_12 | SLOT_13 | SLOT_14 | SLOT_15 |
3392-
SLOT_16 | SLOT_17 | SLOT_18 | SLOT_19 | SLOT_20 | SLOT_21 | SLOT_22
3393-
)
3394-
);
3395-
return (PyObject *)(type->details[i].pointer);
3396-
}
3397-
3398-
static MS_INLINE PyObject *
3399-
TypeNode_get_constr_decimal_multiple_of(TypeNode *type) {
3400-
Py_ssize_t i = ms_popcount(
3401-
type->types & (
3402-
SLOT_00 | SLOT_01 | SLOT_02 | SLOT_03 | SLOT_04 | SLOT_05 | SLOT_06 | SLOT_07 |
3403-
SLOT_08 | SLOT_09 | SLOT_10 | SLOT_11 | SLOT_12 | SLOT_13 | SLOT_14 | SLOT_15 |
3404-
SLOT_16 | SLOT_17 | SLOT_18 | SLOT_19 | SLOT_20 | SLOT_21 | SLOT_22 | SLOT_23
3405-
)
3406-
);
3407-
return (PyObject *)(type->details[i].pointer);
3408-
}
3409-
34103380
static MS_INLINE void
34113381
TypeNode_get_fixtuple(TypeNode *type, Py_ssize_t *offset, Py_ssize_t *size) {
34123382
Py_ssize_t i = ms_popcount(
34133383
type->types & (
34143384
SLOT_00 | SLOT_01 | SLOT_02 | SLOT_03 | SLOT_04 | SLOT_05 | SLOT_06 | SLOT_07 |
34153385
SLOT_08 | SLOT_09 | SLOT_10 | SLOT_11 | SLOT_12 | SLOT_13 | SLOT_14 | SLOT_15 |
3416-
SLOT_16 | SLOT_17 | SLOT_18 | SLOT_19 | SLOT_20 | SLOT_21 | SLOT_22 | SLOT_23 | SLOT_24
3386+
SLOT_16 | SLOT_17 | SLOT_18 | SLOT_19 | SLOT_20 | SLOT_21 | SLOT_22
34173387
)
34183388
);
34193389
*size = type->details[i].py_ssize_t;
@@ -3477,17 +3447,9 @@ TypeNode_Free(TypeNode *self) {
34773447
TypeNode *node = (TypeNode *)(self->details[i + fixtuple_offset].pointer);
34783448
TypeNode_Free(node);
34793449
}
3480-
/* Free Decimal constraint PyObject* stored after scalar constraint slots */
3481-
if (self->types & (MS_CONSTR_DECIMAL_GT | MS_CONSTR_DECIMAL_GE)) {
3482-
PyObject *obj = TypeNode_get_constr_decimal_min(self);
3483-
Py_XDECREF(obj);
3484-
}
3485-
if (self->types & (MS_CONSTR_DECIMAL_LT | MS_CONSTR_DECIMAL_LE)) {
3486-
PyObject *obj = TypeNode_get_constr_decimal_max(self);
3487-
Py_XDECREF(obj);
3488-
}
3489-
if (self->types & MS_CONSTR_DECIMAL_MULTIPLE_OF) {
3490-
PyObject *obj = TypeNode_get_constr_decimal_multiple_of(self);
3450+
/* Free the Decimal constraints tuple stored after scalar constraint slots */
3451+
if (self->types & MS_CONSTR_DECIMAL) {
3452+
PyObject *obj = TypeNode_get_constr_decimal(self);
34913453
Py_XDECREF(obj);
34923454
}
34933455
PyMem_Free(self);
@@ -3503,16 +3465,10 @@ TypeNode_traverse(TypeNode *self, visitproc visit, void *arg) {
35033465
PyObject *obj = (PyObject *)(self->details[i].pointer);
35043466
Py_VISIT(obj);
35053467
}
3506-
/* Visit Decimal constraint PyObject* (direct references on this node) before
3507-
* recursing into children, which may return early. */
3508-
if (self->types & (MS_CONSTR_DECIMAL_GT | MS_CONSTR_DECIMAL_GE)) {
3509-
Py_VISIT(TypeNode_get_constr_decimal_min(self));
3510-
}
3511-
if (self->types & (MS_CONSTR_DECIMAL_LT | MS_CONSTR_DECIMAL_LE)) {
3512-
Py_VISIT(TypeNode_get_constr_decimal_max(self));
3513-
}
3514-
if (self->types & MS_CONSTR_DECIMAL_MULTIPLE_OF) {
3515-
Py_VISIT(TypeNode_get_constr_decimal_multiple_of(self));
3468+
/* Visit the Decimal constraints tuple (a direct reference on this node)
3469+
* before recursing into children, which may return early. */
3470+
if (self->types & MS_CONSTR_DECIMAL) {
3471+
Py_VISIT(TypeNode_get_constr_decimal(self));
35163472
}
35173473
for (i = n_obj; i < (n_obj + n_typenode); i++) {
35183474
int out;
@@ -3630,10 +3586,9 @@ typedef struct {
36303586
bool literal_none;
36313587
bool literal_bool_true;
36323588
bool literal_bool_false;
3633-
/* Decimal constraints (stored as PyObject* Decimal instances) */
3634-
PyObject *py_decimal_min;
3635-
PyObject *py_decimal_max;
3636-
PyObject *py_decimal_multiple_of;
3589+
/* Decimal constraints, a 5-tuple (gt, ge, lt, le, multiple_of) of
3590+
* Decimal instances (None when unset) */
3591+
PyObject *py_decimal_constrs;
36373592
/* Constraints */
36383593
int64_t c_int_min;
36393594
int64_t c_int_max;
@@ -3922,25 +3877,41 @@ typenode_collect_constraints(
39223877
}
39233878
}
39243879
else if (kind == CK_DECIMAL) {
3925-
if (constraints->gt != NULL) {
3926-
state->types |= MS_CONSTR_DECIMAL_GT;
3927-
if (!_constr_as_decimal(constraints->gt, &(state->py_decimal_min))) return -1;
3928-
}
3929-
else if (constraints->ge != NULL) {
3930-
state->types |= MS_CONSTR_DECIMAL_GE;
3931-
if (!_constr_as_decimal(constraints->ge, &(state->py_decimal_min))) return -1;
3932-
}
3933-
if (constraints->lt != NULL) {
3934-
state->types |= MS_CONSTR_DECIMAL_LT;
3935-
if (!_constr_as_decimal(constraints->lt, &(state->py_decimal_max))) return -1;
3936-
}
3937-
else if (constraints->le != NULL) {
3938-
state->types |= MS_CONSTR_DECIMAL_LE;
3939-
if (!_constr_as_decimal(constraints->le, &(state->py_decimal_max))) return -1;
3880+
PyObject *bounds[5] = {
3881+
constraints->gt, constraints->ge,
3882+
constraints->lt, constraints->le,
3883+
constraints->multiple_of,
3884+
};
3885+
PyObject *converted[5] = {NULL, NULL, NULL, NULL, NULL};
3886+
bool any = false;
3887+
for (size_t i = 0; i < 5; i++) {
3888+
if (bounds[i] == NULL) continue;
3889+
if (!_constr_as_decimal(bounds[i], &converted[i])) {
3890+
for (size_t j = 0; j < i; j++) {
3891+
Py_XDECREF(converted[j]);
3892+
}
3893+
return -1;
3894+
}
3895+
any = true;
39403896
}
3941-
if (constraints->multiple_of != NULL) {
3942-
state->types |= MS_CONSTR_DECIMAL_MULTIPLE_OF;
3943-
if (!_constr_as_decimal(constraints->multiple_of, &(state->py_decimal_multiple_of))) return -1;
3897+
if (any) {
3898+
PyObject *tuple = PyTuple_New(5);
3899+
if (tuple == NULL) {
3900+
for (size_t i = 0; i < 5; i++) {
3901+
Py_XDECREF(converted[i]);
3902+
}
3903+
return -1;
3904+
}
3905+
for (size_t i = 0; i < 5; i++) {
3906+
PyObject *val = converted[i];
3907+
if (val == NULL) {
3908+
val = Py_None;
3909+
Py_INCREF(val);
3910+
}
3911+
PyTuple_SET_ITEM(tuple, i, val);
3912+
}
3913+
state->types |= MS_CONSTR_DECIMAL;
3914+
state->py_decimal_constrs = tuple;
39443915
}
39453916
}
39463917
else if (kind == CK_STR) {
@@ -4034,9 +4005,7 @@ typenode_from_collect_state(TypeNodeCollectState *state) {
40344005
MS_CONSTR_ARRAY_MAX_LENGTH |
40354006
MS_CONSTR_MAP_MIN_LENGTH |
40364007
MS_CONSTR_MAP_MAX_LENGTH |
4037-
MS_CONSTR_DECIMAL_GT | MS_CONSTR_DECIMAL_GE |
4038-
MS_CONSTR_DECIMAL_LT | MS_CONSTR_DECIMAL_LE |
4039-
MS_CONSTR_DECIMAL_MULTIPLE_OF
4008+
MS_CONSTR_DECIMAL
40404009
)
40414010
);
40424011
if (state->types & MS_TYPE_FIXTUPLE) {
@@ -4232,17 +4201,9 @@ typenode_from_collect_state(TypeNodeCollectState *state) {
42324201
if (state->types & MS_CONSTR_MAP_MAX_LENGTH) {
42334202
out->details[e_ind++].py_ssize_t = state->c_map_max_length;
42344203
}
4235-
if (state->types & (MS_CONSTR_DECIMAL_GT | MS_CONSTR_DECIMAL_GE)) {
4236-
Py_INCREF(state->py_decimal_min);
4237-
out->details[e_ind++].pointer = state->py_decimal_min;
4238-
}
4239-
if (state->types & (MS_CONSTR_DECIMAL_LT | MS_CONSTR_DECIMAL_LE)) {
4240-
Py_INCREF(state->py_decimal_max);
4241-
out->details[e_ind++].pointer = state->py_decimal_max;
4242-
}
4243-
if (state->types & MS_CONSTR_DECIMAL_MULTIPLE_OF) {
4244-
Py_INCREF(state->py_decimal_multiple_of);
4245-
out->details[e_ind++].pointer = state->py_decimal_multiple_of;
4204+
if (state->types & MS_CONSTR_DECIMAL) {
4205+
Py_INCREF(state->py_decimal_constrs);
4206+
out->details[e_ind++].pointer = state->py_decimal_constrs;
42464207
}
42474208
return (TypeNode *)out;
42484209

@@ -4959,9 +4920,7 @@ typenode_collect_clear_state(TypeNodeCollectState *state) {
49594920
Py_CLEAR(state->literal_str_values);
49604921
Py_CLEAR(state->literal_str_lookup);
49614922
Py_CLEAR(state->c_str_regex);
4962-
Py_CLEAR(state->py_decimal_min);
4963-
Py_CLEAR(state->py_decimal_max);
4964-
Py_CLEAR(state->py_decimal_multiple_of);
4923+
Py_CLEAR(state->py_decimal_constrs);
49654924
}
49664925

49674926
/* This decomposes an input type `obj`, stripping out any "wrapper" types
@@ -10554,54 +10513,62 @@ ms_check_float_constraints(PyObject *obj, TypeNode *type, PathNode *path) {
1055410513

1055510514
static MS_NOINLINE bool
1055610515
ms_passes_decimal_constraints(PyObject *obj, TypeNode *type, PathNode *path) {
10557-
if (type->types & MS_CONSTR_DECIMAL_GT) {
10558-
PyObject *c = TypeNode_get_constr_decimal_min(type);
10559-
int ok = PyObject_RichCompareBool(obj, c, Py_GT);
10516+
PyObject *constrs = TypeNode_get_constr_decimal(type);
10517+
PyObject *gt = PyTuple_GET_ITEM(constrs, 0);
10518+
PyObject *ge = PyTuple_GET_ITEM(constrs, 1);
10519+
PyObject *lt = PyTuple_GET_ITEM(constrs, 2);
10520+
PyObject *le = PyTuple_GET_ITEM(constrs, 3);
10521+
PyObject *multiple_of = PyTuple_GET_ITEM(constrs, 4);
10522+
10523+
if (gt != Py_None) {
10524+
int ok = PyObject_RichCompareBool(obj, gt, Py_GT);
1056010525
if (MS_UNLIKELY(ok < 0)) return false;
1056110526
if (MS_UNLIKELY(!ok)) {
10562-
ms_raise_validation_error(path, "Expected `Decimal` > %R", c);
10527+
ms_raise_validation_error(path, "Expected `Decimal` > %R", gt);
1056310528
return false;
1056410529
}
1056510530
}
10566-
else if (type->types & MS_CONSTR_DECIMAL_GE) {
10567-
PyObject *c = TypeNode_get_constr_decimal_min(type);
10568-
int ok = PyObject_RichCompareBool(obj, c, Py_GE);
10531+
else if (ge != Py_None) {
10532+
int ok = PyObject_RichCompareBool(obj, ge, Py_GE);
1056910533
if (MS_UNLIKELY(ok < 0)) return false;
1057010534
if (MS_UNLIKELY(!ok)) {
10571-
ms_raise_validation_error(path, "Expected `Decimal` >= %R", c);
10535+
ms_raise_validation_error(path, "Expected `Decimal` >= %R", ge);
1057210536
return false;
1057310537
}
1057410538
}
10575-
if (type->types & MS_CONSTR_DECIMAL_LT) {
10576-
PyObject *c = TypeNode_get_constr_decimal_max(type);
10577-
int ok = PyObject_RichCompareBool(obj, c, Py_LT);
10539+
if (lt != Py_None) {
10540+
int ok = PyObject_RichCompareBool(obj, lt, Py_LT);
1057810541
if (MS_UNLIKELY(ok < 0)) return false;
1057910542
if (MS_UNLIKELY(!ok)) {
10580-
ms_raise_validation_error(path, "Expected `Decimal` < %R", c);
10543+
ms_raise_validation_error(path, "Expected `Decimal` < %R", lt);
1058110544
return false;
1058210545
}
1058310546
}
10584-
else if (type->types & MS_CONSTR_DECIMAL_LE) {
10585-
PyObject *c = TypeNode_get_constr_decimal_max(type);
10586-
int ok = PyObject_RichCompareBool(obj, c, Py_LE);
10547+
else if (le != Py_None) {
10548+
int ok = PyObject_RichCompareBool(obj, le, Py_LE);
1058710549
if (MS_UNLIKELY(ok < 0)) return false;
1058810550
if (MS_UNLIKELY(!ok)) {
10589-
ms_raise_validation_error(path, "Expected `Decimal` <= %R", c);
10551+
ms_raise_validation_error(path, "Expected `Decimal` <= %R", le);
1059010552
return false;
1059110553
}
1059210554
}
10593-
if (type->types & MS_CONSTR_DECIMAL_MULTIPLE_OF) {
10594-
PyObject *c = TypeNode_get_constr_decimal_multiple_of(type);
10595-
PyObject *modulo = PyNumber_Remainder(obj, c);
10555+
if (multiple_of != Py_None) {
10556+
PyObject *modulo = PyNumber_Remainder(obj, multiple_of);
1059610557
if (modulo == NULL) return false;
10597-
/* A Decimal is falsy iff it is zero. Using truthiness avoids
10598-
* allocating a zero to compare against, and (unlike coercing to a
10599-
* C integer) stays correct for fractional remainders. */
10600-
int nonzero = PyObject_IsTrue(modulo);
10558+
/* Compare the remainder against zero explicitly rather than relying
10559+
* on `__bool__`, which Decimal subclasses may override. `zero` is a
10560+
* cached small int, creating it here is effectively free. */
10561+
PyObject *zero = PyLong_FromLong(0);
10562+
if (MS_UNLIKELY(zero == NULL)) {
10563+
Py_DECREF(modulo);
10564+
return false;
10565+
}
10566+
int equal = PyObject_RichCompareBool(modulo, zero, Py_EQ);
10567+
Py_DECREF(zero);
1060110568
Py_DECREF(modulo);
10602-
if (MS_UNLIKELY(nonzero < 0)) return false;
10603-
if (MS_UNLIKELY(nonzero)) {
10604-
ms_raise_validation_error(path, "Expected `Decimal` that's a multiple of %R", c);
10569+
if (MS_UNLIKELY(equal < 0)) return false;
10570+
if (MS_UNLIKELY(!equal)) {
10571+
ms_raise_validation_error(path, "Expected `Decimal` that's a multiple of %R", multiple_of);
1060510572
return false;
1060610573
}
1060710574
}
@@ -10610,7 +10577,7 @@ ms_passes_decimal_constraints(PyObject *obj, TypeNode *type, PathNode *path) {
1061010577

1061110578
static MS_NOINLINE PyObject *
1061210579
ms_check_decimal_constraints(PyObject *obj, TypeNode *type, PathNode *path) {
10613-
if (MS_LIKELY(!(type->types & MS_DECIMAL_CONSTRS))) return obj;
10580+
if (MS_LIKELY(!(type->types & MS_CONSTR_DECIMAL))) return obj;
1061410581
if (ms_passes_decimal_constraints(obj, type, path)) return obj;
1061510582
Py_DECREF(obj);
1061610583
return NULL;

‎tests/unit/test_constraints.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,23 @@ def test_convert(self):
514514
with pytest.raises(msgspec.ValidationError):
515515
msgspec.convert(Decimal("0"), typ)
516516

517+
def test_multiple_of_decimal_subclass_bound_bool_override(self, proto):
518+
# A subclass bound with an overridden `__bool__` must not affect
519+
# validation: bounds are normalized to plain `Decimal` on collection,
520+
# and the remainder is compared against zero explicitly rather than
521+
# through `__bool__`.
522+
class WeirdDecimal(Decimal):
523+
def __bool__(self):
524+
return True # even for zero
525+
526+
class Ex(msgspec.Struct):
527+
x: Annotated[Decimal, Meta(multiple_of=WeirdDecimal("0.1"))]
528+
529+
dec = proto.Decoder(Ex)
530+
assert dec.decode(proto.encode(Ex(Decimal("0.3")))).x == Decimal("0.3")
531+
with pytest.raises(msgspec.ValidationError, match="multiple of"):
532+
dec.decode(proto.encode(Ex(Decimal("0.05"))))
533+
517534
def test_decimal_subclass_bound(self, proto):
518535
# A `Decimal` subclass is a valid bound value: unlike `int`/`float`
519536
# (where only exact instances are accepted), a subclass is genuinely a

0 commit comments

Comments
 (0)