Skip to content

refactor: modularization and performance#5

Merged
paulocoghi merged 8 commits into
masterfrom
refactor/modularization--performance-optimizations--memory-allocations
Jul 20, 2026
Merged

refactor: modularization and performance#5
paulocoghi merged 8 commits into
masterfrom
refactor/modularization--performance-optimizations--memory-allocations

Conversation

@paulocoghi

@paulocoghi paulocoghi commented Jul 17, 2026

Copy link
Copy Markdown
Member

This PR is focused on better organizing the codebase and applying some initial performance and efficiency optimizations.

Architectural Changes

  • Splitting of large classes: Mongo::Client and Mongo::Collection were split into their respective subfolders and multiple smaller modules like client/retryable_writes.cr, collection/find_and_modify.cr.
  • Modular Unified Runner: The large spec/unified_runner.cr was split into multiple modules at spec/unified/, introducing a clean Dispatcher pattern that makes it a lot easier to expand and improve the driver.

Performance & Memory Optimizations (reduced allocations)

  • Socket Streaming: BSON parsing off the TCP socket no longer allocates intermediate arrays. It uses read_fully with IO::Memory
  • User-Space Pipes: GridFS now uses jgaskins/pipe instead of kernel-level IO.pipe. This has the potential to bring up to ~10x faster pipe throughput.
  • Lock-Free IDs: Replaced the OS-level Mutex for tracking request_ids with a lock-free Atomic(Int32).
  • GC Relief: Eliminated chained .map/.select array materialization, switching to single-pass compact_map and String.build.

Crystal 1.20/1.21 Modernization & Safety

  • Zero .not_nil!: All instances of .not_nil! have been removed. The driver relies entirely on Crystal's type narrowing and raises descriptive Mongo::Errors if anomalies occur.
  • Execution Contexts: Migrated to Sync::Mutex and removed all assumptions of single-thread fiber pinning, making the driver robust for Crystal 1.21 Parallel contexts.

Test Status: 324/324 Unified Test Format examples correctly passing (18m 55s execution time)

@paulocoghi paulocoghi changed the title refactor: update to Sync::Mutex and reduced allocations refactor: internal architecture, technical debt, and performance Jul 20, 2026
@paulocoghi paulocoghi changed the title refactor: internal architecture, technical debt, and performance refactor: modularization and performance Jul 20, 2026
@paulocoghi
paulocoghi merged commit 5551ceb into master Jul 20, 2026
1 check passed
@paulocoghi
paulocoghi deleted the refactor/modularization--performance-optimizations--memory-allocations branch July 20, 2026 19:55
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.

1 participant