Commit a94d736
authored
Optimize event loop for improved performance (#19)
* Optimize event loop for improved performance
- Fix O(2n) list reversal in nif_get_pending using enif_make_list_from_array
- Use bitwise AND instead of modulo for hash index calculation
- Add single-lookup callback dispatch with _callbacks_by_cid dict
- Implement coalesced wakeup flag to reduce condition variable signals
- Add timer heap cleanup iteration limit (max 10 per cycle)
- Lazy-load transport extra info (sockname/peername) on first access
- Remove per-event stats updates from py_event_worker
* Remove redundant wakeups and optimize transport buffer
- Remove py_nif:event_loop_wakeup calls from worker FD and timer handlers
(already handled by coalesced wakeup in event_loop_add_pending)
- Replace O(n) buffer deletion with O(1) offset tracking in transport
- Use memoryview for efficient buffer slicing during writes
* Increase hash table size and add backpressure
- Increase PENDING_HASH_SIZE from 128 to 256 for better load factor
- Add backpressure check before acquiring lock in event_loop_add_pending
- Drop events when queue exceeds MAX_PENDING_EVENTS (256)
* Add benchmark scripts for event loop performance testing
- run_benchmark.erl: Erlang module for callback and TCP benchmarks
- simple_bench.py: Simple callback dispatch benchmark
- io_bench.py: TCP echo I/O benchmark
* Optimize pool and router for event loop performance
- Pool: Create independent loops instead of duplicating same reference
- Pool: Use tuple for O(1) element access instead of lists:nth
- Pool: Each loop gets its own worker for true parallelism
- Router: Use combined handle_fd_event_and_reselect NIF call
- Router: Add message_queue_data off_heap for large queues
* Cache reactor callables for faster hot path
- Add global cache for erlang.reactor module and callbacks
- Cache on_read_ready and on_write_ready function references
- Use PyObject_CallFunctionObjArgs instead of PyObject_CallMethod
- Avoids PyImport and attribute lookup on every read/write callback
- Thread-safe lazy initialization with double-checked locking
* Add callable caching and simplify ASGI/WSGI code
- Add per-interpreter callable cache to avoid per-request module imports
- Cache module/callable for ASGI apps
- Cache runner module and _run_asgi_sync/_run_wsgi_sync functions
- ~3-5µs saved per request on cache hit
- Remove scope template cache (~200 lines)
- Thread-local 64-entry cache added complexity for marginal gain
- Direct scope building with interned keys is fast enough
- Remove lazy header conversion (~450 lines)
- LazyHeaderList type and iterator removed
- Always use eager header conversion
- Simpler code path, frameworks typically access headers eagerly
- Update documentation to reflect new optimization strategy
Net reduction: ~566 lines of code1 parent a46b940 commit a94d736
16 files changed
Lines changed: 969 additions & 996 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | 93 | | |
110 | 94 | | |
111 | 95 | | |
| |||
120 | 104 | | |
121 | 105 | | |
122 | 106 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | 107 | | |
127 | 108 | | |
128 | 109 | | |
| |||
237 | 218 | | |
238 | 219 | | |
239 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
240 | 230 | | |
241 | 231 | | |
242 | 232 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
123 | 193 | | |
124 | 194 | | |
125 | 195 | | |
| |||
977 | 1047 | | |
978 | 1048 | | |
979 | 1049 | | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
980 | 1053 | | |
981 | 1054 | | |
982 | 1055 | | |
| |||
1088 | 1161 | | |
1089 | 1162 | | |
1090 | 1163 | | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
1091 | 1167 | | |
1092 | | - | |
1093 | | - | |
1094 | 1168 | | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
1095 | 1172 | | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
1096 | 1207 | | |
1097 | 1208 | | |
1098 | 1209 | | |
| |||
1108 | 1219 | | |
1109 | 1220 | | |
1110 | 1221 | | |
1111 | | - | |
| 1222 | + | |
1112 | 1223 | | |
1113 | 1224 | | |
1114 | 1225 | | |
1115 | 1226 | | |
1116 | 1227 | | |
1117 | | - | |
1118 | 1228 | | |
1119 | 1229 | | |
1120 | 1230 | | |
| 1231 | + | |
1121 | 1232 | | |
1122 | 1233 | | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
1128 | 1239 | | |
1129 | 1240 | | |
1130 | | - | |
| 1241 | + | |
1131 | 1242 | | |
1132 | 1243 | | |
1133 | 1244 | | |
| |||
1705 | 1816 | | |
1706 | 1817 | | |
1707 | 1818 | | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
1708 | 1822 | | |
1709 | 1823 | | |
1710 | | - | |
1711 | | - | |
| 1824 | + | |
| 1825 | + | |
1712 | 1826 | | |
1713 | 1827 | | |
1714 | 1828 | | |
| |||
1728 | 1842 | | |
1729 | 1843 | | |
1730 | 1844 | | |
1731 | | - | |
| 1845 | + | |
1732 | 1846 | | |
1733 | | - | |
| 1847 | + | |
1734 | 1848 | | |
1735 | 1849 | | |
1736 | 1850 | | |
| |||
1759 | 1873 | | |
1760 | 1874 | | |
1761 | 1875 | | |
1762 | | - | |
| 1876 | + | |
1763 | 1877 | | |
1764 | | - | |
| 1878 | + | |
1765 | 1879 | | |
1766 | 1880 | | |
1767 | 1881 | | |
| |||
1789 | 1903 | | |
1790 | 1904 | | |
1791 | 1905 | | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
1792 | 1911 | | |
1793 | 1912 | | |
1794 | 1913 | | |
| |||
1822 | 1941 | | |
1823 | 1942 | | |
1824 | 1943 | | |
1825 | | - | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
1826 | 1952 | | |
1827 | 1953 | | |
1828 | 1954 | | |
| |||
3120 | 3246 | | |
3121 | 3247 | | |
3122 | 3248 | | |
3123 | | - | |
3124 | | - | |
3125 | | - | |
| 3249 | + | |
| 3250 | + | |
3126 | 3251 | | |
3127 | 3252 | | |
3128 | 3253 | | |
3129 | | - | |
| 3254 | + | |
3130 | 3255 | | |
3131 | 3256 | | |
3132 | | - | |
3133 | | - | |
3134 | | - | |
3135 | | - | |
| 3257 | + | |
| 3258 | + | |
| 3259 | + | |
| 3260 | + | |
| 3261 | + | |
| 3262 | + | |
| 3263 | + | |
| 3264 | + | |
| 3265 | + | |
| 3266 | + | |
| 3267 | + | |
3136 | 3268 | | |
3137 | 3269 | | |
3138 | 3270 | | |
| |||
3190 | 3322 | | |
3191 | 3323 | | |
3192 | 3324 | | |
3193 | | - | |
3194 | | - | |
3195 | | - | |
| 3325 | + | |
| 3326 | + | |
3196 | 3327 | | |
3197 | 3328 | | |
3198 | | - | |
| 3329 | + | |
3199 | 3330 | | |
3200 | 3331 | | |
3201 | | - | |
3202 | | - | |
3203 | | - | |
3204 | | - | |
| 3332 | + | |
| 3333 | + | |
| 3334 | + | |
| 3335 | + | |
| 3336 | + | |
| 3337 | + | |
| 3338 | + | |
| 3339 | + | |
| 3340 | + | |
| 3341 | + | |
3205 | 3342 | | |
3206 | 3343 | | |
3207 | 3344 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
240 | 241 | | |
241 | 242 | | |
242 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
243 | 249 | | |
244 | 250 | | |
245 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3659 | 3659 | | |
3660 | 3660 | | |
3661 | 3661 | | |
3662 | | - | |
3663 | | - | |
3664 | | - | |
3665 | | - | |
3666 | | - | |
3667 | | - | |
3668 | 3662 | | |
3669 | 3663 | | |
3670 | 3664 | | |
| |||
0 commit comments