Repository navigation
Commit ee1f8ee
authored
Replace the hand-written `dialect_new` with an Argument Clinic + vectorcall. Move `_csv_Dialect___replace___impl()` from calling `dialect_new()` to `dialect_new_helper()` to avoid temporary tuple.
Error message for too many arguments is now `TypeError: Dialect() takes at most 9 arguments (N given)` instead of `TypeError: function takes at most 9 arguments (N given)`.
Benchmark:
```
+-------------------------------------------------+---------+-----------------------+
| Benchmark | main | patch |
+=================================================+=========+=======================+
| _csv.Dialect() | 240 ns | 198 ns: 1.22x faster |
+-------------------------------------------------+---------+-----------------------+
| _csv.Dialect().__replace__(**{'delimiter':','}) | 1.79 us | 1.55 us: 1.15x faster |
+-------------------------------------------------+---------+-----------------------+
| csv.reader(f) | 409 ns | 381 ns: 1.07x faster |
+-------------------------------------------------+---------+-----------------------+
| csv.reader(f, delimiter=';') | 803 ns | 557 ns: 1.44x faster |
+-------------------------------------------------+---------+-----------------------+
| csv.writer(g) | 399 ns | 371 ns: 1.08x faster |
+-------------------------------------------------+---------+-----------------------+
| parse_small() | 5.45 us | 5.07 us: 1.07x faster |
+-------------------------------------------------+---------+-----------------------+
| Geometric mean | (ref) | 1.14x faster |
+-------------------------------------------------+---------+-----------------------+
Benchmark hidden because not significant (1): parse_large()
```
```python
import pyperf
SETUP = """
import _csv, csv, io
f = io.StringIO()
g = io.StringIO()
small_data = "a,b,c\\n" + "1,2,3\\n" * 20 # ~140 bytes
large_data = "a,b,c\\n" + "1,2,3\\n" * 50000 # ~350 KB
def parse_small():
for row in csv.reader(io.StringIO(small_data), delimiter=','):
pass
def parse_large():
for row in csv.reader(io.StringIO(large_data), delimiter=','):
pass
"""
CASES = [
("_csv.Dialect()", "_csv.Dialect()"),
("_csv.Dialect().__replace__(**{'delimiter':','})", "_csv.Dialect().__replace__(**{'delimiter':','})"),
("csv.reader(f)", "csv.reader(f)"),
("csv.reader(f, delimiter=';')", "csv.reader(f, delimiter=';')"),
("csv.writer(g)", "csv.writer(g)"),
("parse_small()", "parse_small()"),
("parse_large()", "parse_large()"),
]
runner = pyperf.Runner()
for name, stmt in CASES:
runner.timeit(name, stmt=stmt, setup=SETUP)
```
1 parent a92a4d1 commit ee1f8ee
6 files changed
Lines changed: 273 additions & 62 deletions
File tree
- Include/internal
- Modules
- clinic
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
437 | 437 | | |
438 | 438 | | |
439 | 439 | | |
| 440 | + | |
440 | 441 | | |
441 | 442 | | |
442 | 443 | | |
443 | 444 | | |
444 | 445 | | |
445 | 446 | | |
| 447 | + | |
446 | 448 | | |
447 | 449 | | |
448 | 450 | | |
| |||
456 | 458 | | |
457 | 459 | | |
458 | 460 | | |
| 461 | + | |
459 | 462 | | |
460 | 463 | | |
461 | 464 | | |
| |||
475 | 478 | | |
476 | 479 | | |
477 | 480 | | |
| 481 | + | |
478 | 482 | | |
479 | 483 | | |
480 | 484 | | |
| |||
638 | 642 | | |
639 | 643 | | |
640 | 644 | | |
| 645 | + | |
641 | 646 | | |
642 | 647 | | |
643 | 648 | | |
| |||
771 | 776 | | |
772 | 777 | | |
773 | 778 | | |
| 779 | + | |
774 | 780 | | |
| 781 | + | |
775 | 782 | | |
776 | 783 | | |
777 | 784 | | |
| |||
837 | 844 | | |
838 | 845 | | |
839 | 846 | | |
| 847 | + | |
840 | 848 | | |
841 | 849 | | |
842 | 850 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | 460 | | |
474 | 461 | | |
475 | 462 | | |
| |||
486 | 473 | | |
487 | 474 | | |
488 | 475 | | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
489 | 491 | | |
490 | | - | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
491 | 498 | | |
492 | | - | |
| 499 | + | |
493 | 500 | | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
516 | 512 | | |
517 | 513 | | |
518 | 514 | | |
519 | | - | |
| 515 | + | |
520 | 516 | | |
521 | 517 | | |
522 | 518 | | |
523 | 519 | | |
524 | 520 | | |
525 | | - | |
526 | | - | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
527 | 524 | | |
528 | 525 | | |
529 | 526 | | |
| |||
536 | 533 | | |
537 | 534 | | |
538 | 535 | | |
539 | | - | |
540 | | - | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
541 | 540 | | |
542 | 541 | | |
543 | 542 | | |
544 | 543 | | |
545 | | - | |
546 | | - | |
| 544 | + | |
547 | 545 | | |
548 | 546 | | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | 547 | | |
559 | 548 | | |
560 | 549 | | |
| |||
676 | 665 | | |
677 | 666 | | |
678 | 667 | | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
686 | 672 | | |
687 | 673 | | |
688 | 674 | | |
| |||
719 | 705 | | |
720 | 706 | | |
721 | 707 | | |
| 708 | + | |
722 | 709 | | |
723 | 710 | | |
724 | 711 | | |
| |||
0 commit comments