Commit 1a3150b
committed
Replace internal maps with unsorted Vecs
HashMap and BTreeMap are overkill in this context. Unsorted vectors are
plenty fast enough and the necessary collection interfaces are
straightforward to implement. This change has two benefits.
First, it improves binary size. For the `print` example from signal-hook
in release mode, the .text section shrinks by about 18 KiB and overall
file size shrinks by about 30 KiB. That's roughly a 6% reduction in both
metrics.
Second, the simpler data structures make it more obvious that the signal
handler only does async-signal-safe operations. In particular, the
default HashMap has a `RandomState`, which can access TLS, do dlsym
lookups, open and read from files, etc. depending on the platform. I
don't think that's a problem for the hash table *lookup* done in the
signal handler since that shouldn't touch the `RandomState`, but it's a
bit subtle and the standard library doesn't make any guarantees about
this. Avoiding hash maps entirely removes the need to think about it.
Performance notes:
* (Un-)registering actions does an insert/remove by ActionId, which is
asymptotically slower with this PR. However, (un-)registering is a
slow operation and should be done rarely. Besides locking, it always
clones the entire `SignalData`, so it already takes O(n) time when
there's n actions registered across all symbols.
* The signal handler looks up the `Slot` by signal number, which is
asymptotically slower with this PR. However, there's only a very small
constant number of signals, so asymptotics don't matter.
* After looking up the right `Slot`, the signal handler only iterates
sequentially over the actions, it doesn't do any lookups by ActionId.
* For a simple microbenchmark that registers one action each for 20
signals and then raises one signal 100k times, this implementation
appears to be slightly *faster* regardless of which signal is raised.1 parent ee9a032 commit 1a3150b
2 files changed
+106
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
68 | 69 | | |
69 | | - | |
70 | | - | |
71 | 70 | | |
72 | 71 | | |
73 | 72 | | |
| |||
89 | 88 | | |
90 | 89 | | |
91 | 90 | | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
144 | | - | |
145 | | - | |
| 143 | + | |
| 144 | + | |
146 | 145 | | |
147 | 146 | | |
148 | 147 | | |
| |||
200 | 199 | | |
201 | 200 | | |
202 | 201 | | |
203 | | - | |
| 202 | + | |
204 | 203 | | |
205 | 204 | | |
206 | 205 | | |
207 | 206 | | |
208 | 207 | | |
209 | 208 | | |
210 | | - | |
| 209 | + | |
211 | 210 | | |
212 | 211 | | |
213 | 212 | | |
| |||
321 | 320 | | |
322 | 321 | | |
323 | 322 | | |
324 | | - | |
| 323 | + | |
325 | 324 | | |
326 | 325 | | |
327 | 326 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
0 commit comments