Skip to content

Avoid redundant seek/read in LineIterator.__next__ - #6366

Open
RudraDudhat2509 wants to merge 3 commits into
aws:masterfrom
RudraDudhat2509:fix/lineiterator-avoid-redundant-seek-read
Open

RudraDudhat2509 wants to merge 3 commits into
aws:masterfrom
RudraDudhat2509:fix/lineiterator-avoid-redundant-seek-read

Conversation

@RudraDudhat2509

@RudraDudhat2509 RudraDudhat2509 commented Sep 30, 2026 •

Copy link
Copy Markdown

Follow-up to #6279, splitting out the non-blocking nit from the AI code review since @rsareddy0329 suggested a separate PR for it.

remainder was re-read via self.buffer.seek(self.read_pos) + self.buffer.read(), but line (already read a few lines above in the same iteration) holds the exact same bytes, since nothing touches the buffer or read_pos between that read and the StopIteration handler. Just returns line directly now, no behavior change.

All 21 tests in test_iterators.py pass, including test_line_iterator_no_trailing_newline_at_end which covers this exact path.

line already holds the trailing remainder read a few lines above, no
need to seek back and read the buffer again for the same bytes.

Follow-up to aws#6279, per Claude's non-blocking review comment and
rsareddy0329's suggestion to split it into a separate PR.
@rsareddy0329
rsareddy0329 deployed to manual-approval October 7, 2026 00:45 — with GitHub Actions Active
@rsareddy0329
rsareddy0329 deployed to manual-approval October 7, 2026 00:45 — with GitHub Actions Active
@rsareddy0329
rsareddy0329 deployed to manual-approval October 7, 2026 00:45 — with GitHub Actions Active
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Claude finished @rsareddy0329's task in 38s —— View job


PR Review: Avoid redundant seek/read in LineIterator.__next__

  • Read the PR diff (sagemaker-core/src/sagemaker/core/iterators.py)
  • Verify correctness of the equivalence
  • Check for edge cases and test coverage

Verdict: ✅ Looks correct — safe to merge

The change replaces a redundant seek + read with a direct return of the already-read line:

# before
self.buffer.seek(self.read_pos)
remainder = self.buffer.read()
self.read_pos += len(remainder)
return remainder

# after
self.read_pos += len(line)
return line

I traced the equivalence and it holds:

  • line comes from self.buffer.readline() at self.read_pos (line 170–171).
  • This branch is only reached when line does not end with \n (the if line and line[-1] == ord("\n") check failed above). That means there is no newline between read_pos and EOF, so readline() consumed everything to the end of the buffer — identical to what buffer.read() from read_pos would return.
  • Nothing mutates the buffer or read_pos between the readline() and this handler: the only thing that happens in between is next(self.byte_iterator) raising StopIteration (caught here), and buffer writes only occur later on the success path (lines 205–206). So line is still the exact remainder.
  • Therefore len(line) == len(remainder), read_pos advances identically, and the returned bytes are the same. No behavior change.

The guard self.read_pos < self.buffer.getbuffer().nbytes guarantees line is non-empty in this path, so returning it (rather than an empty bytes) matches the old behavior.

Minor observations (non-blocking)

  • Correctly returns the full line here (not line[:-1]), matching the old remainder since there's no trailing newline to strip. 👍
  • Test coverage: test_line_iterator_no_trailing_newline_at_end already exercises this exact path, so the existing suite is adequate for this change.

No correctness, security, or performance concerns. Nice small cleanup that removes one unnecessary buffer seek/read per trailing-partial-line case.
· fix/lineiterator-avoid-redundant-seek-read

This branch was successfully deployed

1 active deployment
manual-approval — fea33bc1 Deployed Oct 7, 2026 by rsareddy0329 via wait-for-approval #1648
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