Repository navigation
Commit 1a36014
authored
* perf: skip _recursive_add wrapper for size-1 merge groups
merge_instances_list always built a 1-element wrapper list and called
_recursive_add even when group_indices held a single index — the
common case for queries with no parent duplication. _recursive_add
short-circuits at len(model_group) <= 1 anyway, so the wrapper list,
the call, and the [0] index were pure overhead.
Hoist the early return one frame up: if the group has exactly one row,
read it directly from result_rows.
Benchmarks vs the post-#1657 optimization tip:
- init[1000]: 6571 us -> 6491 us (-1.2%)
- get_all[1000]: 9139 us -> 9069 us (-0.8%)
- get_all[500]: 4951 us -> 4799 us (-3.1%)
- iterate[1000]: 16373 us -> 16068 us (-1.9%)
* perf: cheaper Model.__hash__ and __same__
__hash__ used hash(str(pk) + cls.__name__) on every cache miss —
two string allocations per call. Replace with hash((pk, type(self)))
which uses CPython's identity hash for type objects and skips the
string concat entirely.
__same__ used hash(self) == other.__hash__() to compare two saved
models, which fills both sides' hash caches just to test equality.
Direct (pk, type) compare on the saved-pk path skips both hash
allocations; fall through to hash equality only when both sides are
unsaved.
Saved-pk path is the hot one (every relation-cache lookup goes
through __hash__). Unsaved-pk path keeps the str(vals) shape because
__dict__ can hold list/dict values (json fields, reverse-relation
slots) that aren't hashable directly.
Benchmarks (workloads that exercise relation hashing):
- init_with_related[40]: 828 us -> 781 us (-5.7%)
* perf: specialize _process_kwargs per field-set
The per-key dispatch loop ran four set membership checks on every kwarg
even though typical models have empty json_fields and bytes_fields and
only a couple of relation fields. Restructure so the empty-set checks
are hoisted out of the loop:
- Pre-compute relation_field_names on the class (cached, like
_pydantic_field_names) so expand_relationship is only called for
fields that actually need it.
- Validate unknown kwargs once up front rather than per-iteration.
- Fast path for plain models (no json/bytes/relations) reduces to
``return dict(kwargs), through_tmp_dict`` — a single C-level dict copy.
- Slow path skips per-iteration empty-set checks via has_json /
has_bytes guards.
Side effect: BaseField.expand_relationship is no longer called (only
relation fields go through expand_relationship now). Marked as
``# pragma: no cover`` rather than removed since it remains a public
API contract on the field base class.
Benchmarks vs the previous commit:
- get_all[1000]: 9670 us -> 9024 us (-6.7%)
- get_all[500]: 5008 us -> 4769 us (-4.8%)
- iterate[1000]: 16642 us -> 15723 us (-5.5%)
- get_all_with_related[40]: 4408 us -> 4205 us (-4.6%)
1 parent 34d4b04 commit 1a36014
3 files changed
Lines changed: 85 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
387 | | - | |
| 387 | + | |
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| |||
418 | 419 | | |
419 | 420 | | |
420 | 421 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
440 | 429 | | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
441 | 449 | | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
442 | 468 | | |
443 | 469 | | |
444 | 470 | | |
| |||
471 | 497 | | |
472 | 498 | | |
473 | 499 | | |
474 | | - | |
475 | | - | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
476 | 503 | | |
477 | | - | |
478 | | - | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
479 | 513 | | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
486 | 520 | | |
487 | 521 | | |
488 | 522 | | |
489 | 523 | | |
490 | 524 | | |
491 | 525 | | |
492 | 526 | | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
497 | 532 | | |
498 | 533 | | |
499 | 534 | | |
500 | 535 | | |
501 | 536 | | |
502 | | - | |
503 | | - | |
504 | | - | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
505 | 544 | | |
506 | 545 | | |
507 | 546 | | |
| |||
0 commit comments