Skip to content

Avoid thread fanout for single-item parallel maps#1160

Merged
jwils merged 1 commit intomainfrom
joshuaw/avoid-single-msearch-thread
Apr 30, 2026
Merged

Avoid thread fanout for single-item parallel maps#1160
jwils merged 1 commit intomainfrom
joshuaw/avoid-single-msearch-thread

Conversation

@jwils
Copy link
Copy Markdown
Collaborator

@jwils jwils commented Apr 30, 2026

Summary

  • add a fast path so ElasticGraph::Support::Threading.parallel_map uses normal map for exactly one item
  • keep multi-item enumerables on the existing thread-per-item path
  • add a benchmark plus captured results for the single-item fanout case
  • cover single-item arrays, single-entry hashes, multi-entry hashes, and enumerables without size

Context

This avoids creating a Ruby thread for single-client msearch fanout, which is common for deployments that route queries through one datastore cluster.

Benchmark

bundle exec ruby benchmarks/threading/parallel_map_single_item.rb

  • single item array: after path 3.56M i/s vs before 24.3k i/s (146x faster)
  • single entry hash: after path 3.23M i/s vs before 27.6k i/s (117x faster)
  • Thread.new calls for 1,000 single-item maps: before 1000, after 0
  • multi-item array: same-ish within benchmark error
  • empty array: current implementation is slower in this microbenchmark (208.29ns vs 128.74ns) because it pays the one-item shape check, but neither path creates threads

Testing

  • bundle exec rspec elasticgraph-support/spec/unit/elastic_graph/support/threading_spec.rb
  • script/run_gem_specs elasticgraph-support
  • script/lint elasticgraph-support/lib/elastic_graph/support/threading.rb elasticgraph-support/spec/unit/elastic_graph/support/threading_spec.rb benchmarks/threading/parallel_map_single_item.rb
  • script/type_check
  • bundle exec ruby benchmarks/threading/parallel_map_single_item.rb

Generated with Codex

@jwils jwils force-pushed the joshuaw/avoid-single-msearch-thread branch 2 times, most recently from f2bb494 to a223d8e Compare April 30, 2026 14:25
@jwils jwils marked this pull request as ready for review April 30, 2026 14:48
Copy link
Copy Markdown
Collaborator

@myronmarston myronmarston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Great find! Left a few minor nits.

Comment thread benchmarks/threading/parallel_map_single_item.rb Outdated
Comment thread benchmarks/threading/parallel_map_single_item.rb
Comment thread elasticgraph-support/lib/elastic_graph/support/threading.rb Outdated
Comment thread elasticgraph-support/lib/elastic_graph/support/threading.rb Outdated
@jwils jwils force-pushed the joshuaw/avoid-single-msearch-thread branch from a223d8e to 32550f1 Compare April 30, 2026 18:37
@jwils jwils force-pushed the joshuaw/avoid-single-msearch-thread branch from 32550f1 to c9d763f Compare April 30, 2026 19:47
@jwils jwils merged commit dd16218 into main Apr 30, 2026
25 checks passed
@jwils jwils deleted the joshuaw/avoid-single-msearch-thread branch April 30, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants