Improve hr performance - #885
Open
rumpelsepp wants to merge 2 commits into
Open
Conversation
`_parse_file_structure()` built the record-offset table starting from
the mmap's current position instead of the file start, so lazily
triggering it after any forward reads (as `-r`/--reverse does) produced
a truncated table. On top of that, `records(reverse=True)` defaulted to
starting at record 0 and relied on Python's negative-index wraparound
to reach later records, so `hr -r` never actually started from the end
of the file. Together these made `hr -r` silently emit duplicate,
missing, or wrongly ordered records on any real log file.
Also switch `_record_offsets` from a list to `array("Q")`, matching
cursed_hr.py's approach, which cuts memory and time for building the
offset table (used by -t/--tail, -r/--reverse and len()) roughly in
half.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Replace three hot-path spots in PenlogRecord that run once per record
when hr reads a penlog file, with cheaper stdlib-only equivalents:
- PenlogPriority.to_level(): match-statement -> precomputed dict
lookup. Also used by gallia's live console logging, not just hr.
- PenlogRecord.parse_json(): PenlogPriority(value) enum construction
-> precomputed {value: member} dict, and six "key in dict" + subscript
pairs -> .get(). Both are behavior-preserving (same ValueError on an
invalid priority, same None-on-missing-field semantics).
- _format_record(): dt.strftime("%b %d %H:%M:%S.%f") -> manual f-string
with a fixed month-abbreviation table. Behavior change: timestamps
always render English month abbreviations now, instead of respecting
the OS locale.
Benchmark: `hr -p trace` over a synthetic 2,000,000-record/420MB penlog
file, output redirected to /dev/null, best of repeated runs:
before: 11.10s
after: 8.85s (~20% faster, byte-identical output aside from the
intentional month-name change)
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
I tried coding with AI and I could optimize hr:
Replace three hot-path spots in PenlogRecord that run once per record
when hr reads a penlog file, with cheaper stdlib-only equivalents:
lookup. Also used by gallia's live console logging, not just hr.
-> precomputed {value: member} dict, and six "key in dict" + subscript
pairs -> .get(). Both are behavior-preserving (same ValueError on an
invalid priority, same None-on-missing-field semantics).
with a fixed month-abbreviation table. Behavior change: timestamps
always render English month abbreviations now, instead of respecting
the OS locale.
Benchmark:
hr -p traceover a synthetic 2,000,000-record/420MB penlogfile, output redirected to /dev/null, best of repeated runs:
before: 11.10s
after: 8.85s (~20% faster, byte-identical output aside from the
intentional month-name change)