Hasty Briefsbeta

Bilingual

The Scalar Select Anti-Pattern

a year ago
  • #batch-processing
  • #event-loop
  • #optimization
  • The article discusses the 'Scalar Select Anti-Pattern' in stateful services with event loops.
  • Event loops often need refactoring due to inefficiencies in handling multiple events.
  • Three main event sources in an LSP server: file modifications, client requests, and compilation outputs.
  • The 'scalar select' approach picks one event arbitrarily, leading to overhead when multiple events are ready.
  • Optimizations include prioritization, selective backpressure, elimination, and coalescing of events.
  • Prioritization suggests processing writes over reads over accepts.
  • Selective backpressure allows ignoring certain requests while processing others.
  • Elimination involves dropping redundant requests based on subsequent events.
  • Coalescing combines multiple similar events into a single processing batch.
  • The solution is to adopt a batched approach, processing all available events at once to improve efficiency under load.
  • The 'batch_stream' function is introduced to wait for and process all available events, not just one.