Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Improves buffering performance

AuthorDave Jarvis <email>
Date2026-02-22 11:16:55 GMT-0800
Commit14a8347684b183636c200e516e0a39a7eb57bc60
Parent2f03b08
git/BufferedReader.php
public function seek( int $offset, int $whence = SEEK_SET ): bool {
- $success = $this->handle !== false && \fseek(
- $this->handle,
- $whence === SEEK_CUR
- ? (int)\ftell( $this->handle )
- - $this->bufferLen
- + $this->bufferPos
- + $offset
- : $offset,
- $whence === SEEK_CUR ? SEEK_SET : $whence
- ) === 0;
+ $current = $this->tell();
+ $target = $whence === SEEK_CUR ? $current + $offset : $offset;
+ $bufSt = $current - $this->bufferPos;
+ $bufEn = $bufSt + $this->bufferLen;
+ $success = false;
- if( $success ) {
- $this->buffer = '';
- $this->bufferPos = 0;
- $this->bufferLen = 0;
+ if( $whence !== SEEK_END && $target >= $bufSt && $target <= $bufEn ) {
+ $this->bufferPos = $target - $bufSt;
+ $success = true;
+ } else {
+ $seekTgt = $whence === SEEK_END ? $offset : $target;
+ $seekWh = $whence === SEEK_END ? SEEK_END : SEEK_SET;
+ $success = $this->handle !== false
+ && \fseek( $this->handle, $seekTgt, $seekWh ) === 0;
+
+ if( $success ) {
+ $this->buffer = '';
+ $this->bufferPos = 0;
+ $this->bufferLen = 0;
+ }
}
Delta19 lines added, 14 lines removed, 5-line increase