Cache fieldnames and header-written in function output_to_csv#253
Open
bodoque-01 wants to merge 1 commit intoamanusk:masterfrom
Open
Cache fieldnames and header-written in function output_to_csv#253bodoque-01 wants to merge 1 commit intoamanusk:masterfrom
bodoque-01 wants to merge 1 commit intoamanusk:masterfrom
Conversation
Author
|
Small change, but I've been using the TUI and saw that little opportunity to optimize something small lol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cache CSV fieldnames and header-written state in
output_to_csvI noticed that, under the
helper_functionsfile,output_to_csvis called on every refresh cycle fromanimate_graph. On each call it was:fieldnameslist from theOrderedDictkeyscsv.DictWriterwith those fieldnamesos.path.isfile()to decide whether to write the CSV headerThe changed code in the PR caches the
fieldnameslist and aheader_writtenflag as module-level globals inoutput_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.isfilecheck on every refresh cycle.The fieldnames are derived from
Source.nameandSource.available_sensors, both of which are set once during__init__and never mutated at RUNTIME. The set of sources inself.view.summariesis also fixed aftermain_window()constructs it. As far as I understand and have tested, this makes the fieldnames safe to cache.Changes
_csv_fieldnamesmodule-level cache: computed on the first call tooutput_to_csv, reused on all subsequent calls_csv_header_writtenmodule-level flag: after the first call determines whether the header needs writing (viaos.path.isfile/os.path.getsize), the flag is set and filesystem checks are skipped on all future calls