Commit e67deb2
committed
OrderBook: stop snapshotting the whole book on every consume
ConsumeVolume copied the entire quotes dictionary with quotes.ToArray() on every
match, just so emptied price levels could be removed while iterating. That is a
per-match allocation proportional to book depth on a hot path.
Iterate the live dictionary instead and collect the emptied prices, removing them
in a finally block. The dictionary is never mutated during its own enumeration,
and because disposing the iterator runs the finally, the removal still happens
even though the matcher abandons the enumerator early (it breaks out as soon as
the order is filled) - which is the reason the snapshot was introduced in the
first place. The only remaining allocation is the affected-orders list that is
returned to the caller, and the small removal list, allocated lazily only when a
level is actually emptied.
Behaviour is unchanged: 203 matching-engine and market-emulator tests pass.1 parent 41df256 commit e67deb2
1 file changed
Lines changed: 35 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
385 | 384 | | |
386 | | - | |
387 | | - | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
388 | 388 | | |
389 | | - | |
| 389 | + | |
390 | 390 | | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | 391 | | |
395 | | - | |
396 | | - | |
397 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
398 | 409 | | |
399 | | - | |
400 | | - | |
401 | 410 | | |
402 | 411 | | |
403 | 412 | | |
| |||
423 | 432 | | |
424 | 433 | | |
425 | 434 | | |
426 | | - | |
| 435 | + | |
427 | 436 | | |
428 | 437 | | |
429 | 438 | | |
430 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
431 | 448 | | |
432 | 449 | | |
0 commit comments