Skip to content

Performance optimizations: O(1) lookups, single JSON load, parallel fetches#29

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/improve-code-efficiency
Draft

Performance optimizations: O(1) lookups, single JSON load, parallel fetches#29
Copilot wants to merge 3 commits intomasterfrom
copilot/improve-code-efficiency

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 27, 2025

Identified and fixed several performance bottlenecks across Python and JavaScript code.

bot_leaderboard.py

  • Eliminate redundant file I/O: JSON file was read 26 times (once per type × 2 modes). Now loaded once in main() and passed to all get_bot_leaderboard() calls.
  • Move invariants outside loops: datetime.now(), timedelta(days=7), and variant type check now computed once per function call instead of per-bot.
  • Optimize get_available_bots(): Use set comprehension with single sort on write instead of list operations with repeated membership checks.
# Before: O(n) membership check on list, 26 JSON reads
if d['id'] not in available_bots:  # list
type in ['bullet', 'blitz', 'rapid', 'classical']  # per iteration

# After: O(1) set lookup, 1 JSON read passed to all calls
if bot_id not in available_bots:  # set
is_standard_variant = type in STANDARD_VARIANTS  # frozenset, computed once

app.py

  • Change TYPES from list to frozenset for O(1) route validation

md2html.py

  • Pre-compile regex patterns at module level
  • Replace chained if/elif with dictionary lookup for filename mapping

js/bot-rank.js

  • Parallel fetching: Replace sequential for loop with await to Promise.all() for 13 leaderboard files
  • Hoist constants and helper functions outside doSearch() to avoid recreation per call
// Before: sequential fetches (~13 round trips in series)
for (var t = 0; t < TYPES.length; t++) {
    var res = await fetch('/bot_leaderboard/' + type + '.md');
}

// After: parallel fetches (~1 round trip time)
Promise.all(TYPES.map(type => fetchRankForType(type, usernameLower)))
Original prompt

Identify and suggest improvements to slow or inefficient code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 27, 2025 17:48
…tions, and add parallel fetching

Co-authored-by: TheYoBots <73843275+TheYoBots@users.noreply.github.com>
Co-authored-by: TheYoBots <73843275+TheYoBots@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to inefficient code Performance optimizations: O(1) lookups, single JSON load, parallel fetches Nov 27, 2025
Copilot AI requested a review from TheYoBots November 27, 2025 17:54
Copy link
Copy Markdown
Owner

@TheYoBots TheYoBots left a comment

Choose a reason for hiding this comment

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

Also please update this Pr to the latest revision please

'threeCheck'
]

# Use frozenset for O(1) membership checks in get_bot_leaderboard
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please remove this comment

print(f"Adding {bot_id} to available bots list")
except Exception as e:
print(e)
# Sort only once when writing to file
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Remove this comment as well

file_path = os.path.join(os.path.dirname(__file__), 'bot_leaderboard.json')
with open(file_path, 'r') as f:
bot_ratings = json.load(f)
# Reuse bot_ratings if passed to avoid redundant file reads
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment as well

get_available_bots()
get_all_bot_ratings()

# Load bot ratings once and reuse for all leaderboard generation
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment

row.appendChild(tdVariant);
row.appendChild(tdRank);
tbody.appendChild(row);
// Fetch all leaderboard files in parallel using Promise.all
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment

</footer>
"""

# Dictionary for filename display name mapping - O(1) lookup instead of multiple conditionals
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment

"racingKings.md": "racing kings.md"
}

# Pre-compile regex patterns for better performance
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment

else:
f = "racing kings.md"
h1_tag = generate_h1_tag(f)
# Use dictionary lookup with default fallback
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

remove this comment

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