Repository navigation
Commit 627d48b
committed
Use O(n) ghc-bignum Addr primitives for big Integer and Natural
Previously, putBigInteger/getBigInteger and putBigNatural/getBigNatural
used O(n^2) byte-by-byte shiftR/shiftL via unroll/roll and an
intermediate [Word8] list, taking 95.9 ms and 4.0 GB of allocation to
decode a single 64 KB Integer.
On GHC >= 9.0 (base >= 4.15, ghc-bignum), use integerSizeInBase#/
integerToAddr/integerFromAddr and naturalSizeInBase#/naturalToAddr/
naturalFromAddr with little-endian flag 0# (matching the exact
little-endian wire format of unroll/roll), keeping unroll/roll as the
portable fallback for older GHC / MicroHs.
Safety of unsafe operations (documented inline in Class.hs):
- BI.unsafeCreate: an = abs n (or n for Natural) is strictly positive
on the big-int path, and len = integerSizeInBase# 256## an (or
naturalSizeInBase# 256## n) is the exact byte count written by
integerToAddr / naturalToAddr into the freshly allocated len-byte
buffer.
- unsafeDupablePerformIO + BU.unsafeUseAsCString: bs is an immutable
ByteString of exact length n > 0 (guaranteed by getByteString n),
kept alive across the call by unsafeUseAsCString's withForeignPtr;
integerFromAddr / naturalFromAddr only performs a pure, idempotent
read of those n bytes (does not mutate or require a NUL terminator).
Also document the unsafeChr safety proof inline above getChr.
Object size (GHC 9.10.3 -O2):
- Get Main.o: 589,536 B -> 589,472 B ( -64 B, -0.0%)
- Put Main.o: 303,736 B -> 304,200 B ( +464 B, +0.2%)
- Data/Binary/Class.o: 758,680 B -> 755,856 B (-2,824 B, -0.4%)
- Wasm.o / Pcap.o / Bencode.o: unchanged (0.0%)
Benchmarks (GHC 9.10.3, +RTS -T):
- Get Integer/decode (64 KB): 95.9 ms (4.0 GB alloc, 2.2 GB peak) ->
4.36 us (65 KB alloc, 20 MB peak) (~22,000x faster, ~61,500x less alloc)
- Put [big Integer] (10k): 1.30 ms (12 MB) -> 1.23 ms (6.1 MB) (-49% alloc)
- Get [small Integer] (10k): 122 us (1.3 MB) -> 113 us (1.3 MB)
- Get [small Natural] (10k): 130 us (1.4 MB) -> 131 us (1.4 MB)
Add QuickCheck properties prop_big_integer_wire_format and
prop_big_natural_wire_format verifying bit-for-bit wire-format
equivalence with unroll and roundtrip decoding (240/240 QC properties
passing).
Assisted-By: Antigravity (Gemini 4)1 parent f6f1e0c commit 627d48b
2 files changed
Lines changed: 123 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| |||
100 | 105 | | |
101 | 106 | | |
102 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
103 | 116 | | |
| 117 | + | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
| |||
426 | 440 | | |
427 | 441 | | |
428 | 442 | | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
429 | 458 | | |
430 | 459 | | |
431 | 460 | | |
432 | 461 | | |
433 | 462 | | |
434 | 463 | | |
| 464 | + | |
435 | 465 | | |
436 | 466 | | |
437 | 467 | | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
438 | 494 | | |
439 | 495 | | |
440 | 496 | | |
| |||
443 | 499 | | |
444 | 500 | | |
445 | 501 | | |
| 502 | + | |
446 | 503 | | |
447 | 504 | | |
448 | 505 | | |
| |||
455 | 512 | | |
456 | 513 | | |
457 | 514 | | |
| 515 | + | |
458 | 516 | | |
459 | 517 | | |
460 | 518 | | |
| |||
468 | 526 | | |
469 | 527 | | |
470 | 528 | | |
| 529 | + | |
471 | 530 | | |
472 | 531 | | |
473 | 532 | | |
| |||
497 | 556 | | |
498 | 557 | | |
499 | 558 | | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
500 | 571 | | |
501 | 572 | | |
502 | 573 | | |
| 574 | + | |
503 | 575 | | |
504 | 576 | | |
505 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
506 | 596 | | |
507 | 597 | | |
508 | 598 | | |
| 599 | + | |
509 | 600 | | |
510 | 601 | | |
511 | 602 | | |
| |||
634 | 725 | | |
635 | 726 | | |
636 | 727 | | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
637 | 735 | | |
638 | 736 | | |
639 | 737 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1006 | 1006 | | |
1007 | 1007 | | |
1008 | 1008 | | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
1009 | 1030 | | |
1010 | 1031 | | |
1011 | 1032 | | |
| |||
1346 | 1367 | | |
1347 | 1368 | | |
1348 | 1369 | | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
1349 | 1374 | | |
1350 | 1375 | | |
1351 | 1376 | | |
| |||
0 commit comments