Skip to content

Cache fieldnames and header-written in function output_to_csv#253

Open
bodoque-01 wants to merge 1 commit intoamanusk:masterfrom
bodoque-01:cache-headernames
Open

Cache fieldnames and header-written in function output_to_csv#253
bodoque-01 wants to merge 1 commit intoamanusk:masterfrom
bodoque-01:cache-headernames

Conversation

@bodoque-01
Copy link
Copy Markdown

Cache CSV fieldnames and header-written state in output_to_csv

I noticed that, under the helper_functions file, output_to_csv is called on every refresh cycle from animate_graph. On each call it was:

  1. Rebuilding the fieldnames list from the OrderedDict keys
  2. Constructing a new csv.DictWriter with those fieldnames
  3. Calling os.path.isfile() to decide whether to write the CSV header

The changed code in the PR caches the fieldnames list and a header_written flag as module-level globals in output_to_csv, so they are computed once on the first call and reused on every subsequent call.
It avoids rebuilding the fieldnames list and performing an os.path.isfile check on every refresh cycle.

The fieldnames are derived from Source.name and Source.available_sensors, both of which are set once during __init__ and never mutated at RUNTIME. The set of sources in self.view.summaries is also fixed after main_window() constructs it. As far as I understand and have tested, this makes the fieldnames safe to cache.

Changes

  • Added _csv_fieldnames module-level cache: computed on the first call to output_to_csv, reused on all subsequent calls
  • Added _csv_header_written module-level flag: after the first call determines whether the header needs writing (via os.path.isfile / os.path.getsize), the flag is set and filesystem checks are skipped on all future calls

@bodoque-01
Copy link
Copy Markdown
Author

Small change, but I've been using the TUI and saw that little opportunity to optimize something small lol

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