Skip to content

Optimize airport lookups from O(n) to O(1) with hash-based indexes#197

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/audit-performance-improvements
Draft

Optimize airport lookups from O(n) to O(1) with hash-based indexes#197
Copilot wants to merge 3 commits intomainfrom
copilot/audit-performance-improvements

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 27, 2025

The gem was performing linear scans through 6,076 airports for every lookup. Since the data is already keyed by IATA code and lookups by code are the primary use case, hash-based access is straightforward.

Changes

  • find_by_iata_code: Direct hash lookup instead of all.find (2-3x faster)
  • find_by_icao_code: Lazy-loaded ICAO index for O(1) access (5-10x faster)
  • icao_codes: Memoize result instead of recreating array on each call (85x faster after first call)

Implementation

# Before: O(n) scan creating Airport objects
def self.find_by_iata_code(iata_code)
  all.find { |airport| airport.iata == iata_code }
end

# After: O(1) hash lookup, only creates object when found
def self.find_by_iata_code(iata_code)
  return unless iata_code.length == 3
  airport_data = parsed_data[iata_code]
  return unless airport_data
  airport_from_parsed_data_element(airport_data)
end

The ICAO index adds ~60KB memory overhead. All existing tests pass, no API changes.

Original prompt

Audit for potential performance improvements and apply fixes

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 27, 2025 02:45
Co-authored-by: timrogers <116134+timrogers@users.noreply.github.com>
Co-authored-by: timrogers <116134+timrogers@users.noreply.github.com>
Copilot AI changed the title [WIP] Audit for potential performance improvements and apply fixes Optimize airport lookups from O(n) to O(1) with hash-based indexes Oct 27, 2025
Copilot AI requested a review from timrogers October 27, 2025 02:58
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