Commit 8ed52bf
committed
⚡️ Don't memoize normalized SequenceSet#string
Not duplicating the data in `@tuples` and `@string` saves memory. For
large sequence sets, this memory savings can be substantial.
But this is a tradeoff: it saves time when the string is not used, but
uses more time when the string is used more than once. Working with set
operations can create many ephemeral sets, so avoiding unintentional
string generation can save a lot of time.
Also, by quickly scanning the entries after a string is parsed, we can
bypass the merge algorithm for normalized strings. But this does cause
a small penalty for non-normalized strings.
**Please note:** It _is still possible_ to create a memoized string on
a normalized SequenceSet with `#append`. For example: create a
monotonically sorted SequenceSet with non-normal final entry, then
call `#append` with an adjacently following entry. `#append` coalesces
the final entry and converts it into normal form, but doesn't check
whether the _preceding entries_ of the SequenceSet are normalized.
--------------------------------------------------------------------
Results from benchmarks/sequence_set-normalize.yml
There is still room for improvement here, because #normalize generates
the normalized string for comparison rather than just reparse the
string.
```
normal
local: 19938.9 i/s
v0.5.12: 2988.7 i/s - 6.67x slower
frozen and normal
local: 17011413.5 i/s
v0.5.12: 3574.4 i/s - 4759.30x slower
unsorted
local: 19434.9 i/s
v0.5.12: 2957.5 i/s - 6.57x slower
abnormal
local: 19835.9 i/s
v0.5.12: 3037.1 i/s - 6.53x slower
```
--------------------------------------------------------------------
Results from benchmarks/sequence_set-new.yml
Note that this benchmark doesn't use `SequenceSet::new`; it uses
`SequenceSet::[]`, which freezes the result. In this case, the benchmark
result differences are mostly driven by improved performance of
`#freeze`.
```
n= 10 ints (sorted)
local: 118753.9 i/s
v0.5.12: 85411.4 i/s - 1.39x slower
n= 10 string (sorted)
v0.5.12: 123087.2 i/s
local: 122746.3 i/s - 1.00x slower
n= 10 ints (shuffled)
local: 105919.2 i/s
v0.5.12: 79294.5 i/s - 1.34x slower
n= 10 string (shuffled)
v0.5.12: 114826.6 i/s
local: 108086.2 i/s - 1.06x slower
n= 100 ints (sorted)
local: 16418.4 i/s
v0.5.12: 11864.2 i/s - 1.38x slower
n= 100 string (sorted)
local: 18161.7 i/s
v0.5.12: 15219.3 i/s - 1.19x slower
n= 100 ints (shuffled)
local: 16640.1 i/s
v0.5.12: 11815.8 i/s - 1.41x slower
n= 100 string (shuffled)
v0.5.12: 14755.8 i/s
local: 14512.8 i/s - 1.02x slower
n= 1,000 ints (sorted)
local: 1722.2 i/s
v0.5.12: 1229.0 i/s - 1.40x slower
n= 1,000 string (sorted)
local: 1862.1 i/s
v0.5.12: 1543.2 i/s - 1.21x slower
n= 1,000 ints (shuffled)
local: 1684.9 i/s
v0.5.12: 1252.3 i/s - 1.35x slower
n= 1,000 string (shuffled)
v0.5.12: 1467.3 i/s
local: 1424.6 i/s - 1.03x slower
n= 10,000 ints (sorted)
local: 158.1 i/s
v0.5.12: 127.9 i/s - 1.24x slower
n= 10,000 string (sorted)
local: 187.7 i/s
v0.5.12: 143.4 i/s - 1.31x slower
n= 10,000 ints (shuffled)
local: 145.8 i/s
v0.5.12: 114.5 i/s - 1.27x slower
n= 10,000 string (shuffled)
v0.5.12: 138.4 i/s
local: 136.9 i/s - 1.01x slower
n=100,000 ints (sorted)
local: 14.9 i/s
v0.5.12: 10.6 i/s - 1.40x slower
n=100,000 string (sorted)
local: 19.2 i/s
v0.5.12: 14.0 i/s - 1.37x slower
```
The new code is ~1-6% slower for shuffled strings, but ~30-40% faster
for sorted sets (note that unsorted non-string inputs create a sorted
set).
📚 Update SequenceSet#normalize rdoc1 parent 6de22fd commit 8ed52bf
File tree
3 files changed
+107
-29
lines changed- benchmarks
- lib/net/imap
3 files changed
+107
-29
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
14 | 23 | | |
15 | 24 | | |
16 | 25 | | |
| |||
35 | 44 | | |
36 | 45 | | |
37 | 46 | | |
38 | | - | |
| 47 | + | |
39 | 48 | | |
40 | 49 | | |
41 | 50 | | |
42 | | - | |
| 51 | + | |
43 | 52 | | |
44 | 53 | | |
45 | 54 | | |
46 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
47 | 64 | | |
48 | 65 | | |
49 | 66 | | |
50 | | - | |
| 67 | + | |
51 | 68 | | |
52 | 69 | | |
53 | 70 | | |
54 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
55 | 80 | | |
56 | 81 | | |
57 | 82 | | |
58 | | - | |
| 83 | + | |
59 | 84 | | |
60 | 85 | | |
61 | 86 | | |
62 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
63 | 96 | | |
64 | 97 | | |
65 | 98 | | |
66 | | - | |
| 99 | + | |
67 | 100 | | |
68 | 101 | | |
69 | 102 | | |
70 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
71 | 112 | | |
72 | 113 | | |
73 | 114 | | |
74 | | - | |
| 115 | + | |
75 | 116 | | |
76 | 117 | | |
77 | 118 | | |
78 | | - | |
| 119 | + | |
79 | 120 | | |
80 | 121 | | |
81 | 122 | | |
82 | | - | |
| 123 | + | |
83 | 124 | | |
84 | 125 | | |
85 | 126 | | |
| |||
89 | 130 | | |
90 | 131 | | |
91 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
92 | 137 | | |
93 | 138 | | |
94 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
102 | 106 | | |
103 | 107 | | |
104 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
148 | 152 | | |
149 | 153 | | |
150 | 154 | | |
| |||
586 | 590 | | |
587 | 591 | | |
588 | 592 | | |
589 | | - | |
| 593 | + | |
590 | 594 | | |
591 | 595 | | |
592 | 596 | | |
| |||
605 | 609 | | |
606 | 610 | | |
607 | 611 | | |
608 | | - | |
609 | | - | |
610 | | - | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
611 | 620 | | |
612 | 621 | | |
613 | 622 | | |
614 | | - | |
| 623 | + | |
615 | 624 | | |
616 | 625 | | |
617 | 626 | | |
| |||
624 | 633 | | |
625 | 634 | | |
626 | 635 | | |
627 | | - | |
628 | 636 | | |
629 | 637 | | |
630 | 638 | | |
| |||
971 | 979 | | |
972 | 980 | | |
973 | 981 | | |
974 | | - | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
975 | 986 | | |
976 | 987 | | |
977 | 988 | | |
| |||
1685 | 1696 | | |
1686 | 1697 | | |
1687 | 1698 | | |
1688 | | - | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
1689 | 1703 | | |
1690 | | - | |
1691 | | - | |
1692 | 1704 | | |
1693 | 1705 | | |
1694 | 1706 | | |
1695 | 1707 | | |
1696 | 1708 | | |
1697 | 1709 | | |
1698 | 1710 | | |
1699 | | - | |
1700 | | - | |
1701 | | - | |
| 1711 | + | |
| 1712 | + | |
1702 | 1713 | | |
1703 | 1714 | | |
1704 | 1715 | | |
| |||
1884 | 1895 | | |
1885 | 1896 | | |
1886 | 1897 | | |
1887 | | - | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
1888 | 1901 | | |
1889 | | - | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
1890 | 1919 | | |
1891 | 1920 | | |
1892 | 1921 | | |
| |||
0 commit comments