Skip to content

Improvements and tweaks#649

Open
sreckoskocilic wants to merge 5 commits intoswar:masterfrom
sreckoskocilic:minor_performance_improvements
Open

Improvements and tweaks#649
sreckoskocilic wants to merge 5 commits intoswar:masterfrom
sreckoskocilic:minor_performance_improvements

Conversation

@sreckoskocilic
Copy link
Copy Markdown

@sreckoskocilic sreckoskocilic commented Mar 3, 2026

I have analyzed my demo app along with external libraries used. It is using nba_api lib to keep track on recent games and player stats. Probably it would be good to make a revision on the fixes provided in this PR, especially the one reducing redundant json.loads by implementing dict_cache. Notable is reducing get_parameters() calls of get_dict() from 3 to 1 and find_players() compiles re only one per search now.

2nd batch of optimizations:

  • Added pre-computed cached lists for get_players(), get_active_players(), get_inactive_players(), get_teams() (and its WNBA variants) - cached data is returned instantly instead of being rebuild with every call
  • Added @functools.lru_cache(maxsize=128) for _compile_regex() in players.py and teams.py - regex pattern is compiled and cached
  • Optimize HTTP parameter sorting in http.py from sorted(list) to tuple(sorted(...)) - tuples are faster to handle and iterate
  • Replaced append loops with list comprehensions (_get_players, _get_active_players, _get_inactive_players, _get_teams, _find_players, _find_teams, _find_teams_by_year_founded)
  • Cache json in NBAResponse.get_json() - avoid re-dump of cached dict
  • get_normalized_dict() - Fixed redundant key lookups using set intersection once, added _normalized_dict_cache, use .get() to avoid duplicate lookups

sreckoskocilic and others added 4 commits March 2, 2026 09:00
- refactor get_parameters update {k:v} anti-pattern
- improve find_player_by_id with single dict get() call
- find_players search speedup
…mmit

- status check in raise_exception_on_error raises on HTTP 4xx/5xx before checking json validity (previously a 429 or 500 would silently pass)
…ecks)

- added strip_accents for find_teams
- ImportError should not be silenced as it could indicate a broken module (parser with syntax error)
@sreckoskocilic sreckoskocilic requested a review from swar as a code owner March 3, 2026 08:10
endpoint_parser = get_parser_for_endpoint(self._endpoint, raw_data)
for name, dataset in endpoint_parser.get_data_sets().items():
data[name] = self._build_rows(dataset["headers"], dataset["data"])
except (KeyError, ImportError):
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.

Why remove the import error here? To be honest, I'm not sure why it's even catching it here in the first place

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If the import of get_parser_for_endpoint fails, that's a critical issue that should raise an exception, not silently
continue with an empty result

@swar
Copy link
Copy Markdown
Owner

swar commented Mar 10, 2026

No major complaints from my initial pass. I need to do a deeper dive here and then push this one through. All the changes you suggested make sense

if raise_exception_on_error and not data.valid_json():
raise Exception("InvalidResponse: Response is not in a valid JSON format.")
if raise_exception_on_error:
if status_code is not None and status_code >= 400:
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.

Why the additional breakout of the exception?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The code now checks the HTTP status code first (any >= 400) raising an Exception with the status code, before
validating JSON. This ensures HTTP errors are properly caught.

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