Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to read lines longer than 1023 bytes #580

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

arvidjonasson
Copy link

This pull request includes changes to improve the handling of empty byte sequences in kk_bytes_cat and the reading of lines from standard input in kk_os_read_line.

Enhancements to kk_os_read_line function:

Improvements to kk_bytes_cat function:

  • Added checks to handle cases where either of the byte sequences is empty, such that the function returns the non-empty sequence and drops the empty one. Leads to an early return in these cases.
    (kklib/src/bytes.c)

Considerations

  • Runtime for kk_os_read_line can degrade to O(n^2) for extremely long lines. However, this seems very unlikely, and I believe this to be a lesser issue than lines being incomplete for lengths >1023 bytes. To solve this we would have to scale buf by a factor >1 for each iteration in the loop, which can not be done safely on the stack. Thus it's hard to find a balance between solution complexity, constant overhead and algorithmic complexity. My approach prioritizes solution complexity and constant overhead as it stays true to the previous approach. An approach prioritizing solution complexity and algorithmic complexity can be achieved using the POSIX/Dynamic Memory TR getline(3) function.

- Add check to see if one of the inputs `b1` and `b2` has a length of zero.
- If yes, then directly returns the input with non-zero length.
@arvidjonasson arvidjonasson changed the title Add support to read lines longer than 1023 bytes, and improve performance for certain concatenations. Add support to read lines longer than 1023 bytes Oct 4, 2024
kklib/src/os.c Show resolved Hide resolved
kklib/src/os.c Outdated Show resolved Hide resolved
@daanx
Copy link
Member

daanx commented Oct 4, 2024

Thanks for the PR -- looks good :-)

…024 bytes

- Update `kk_os_read_line` to dynamically allocate and resize the buffer for longer lines.
- Fix a minor discrepancy where `1023` was passed as buffer size to `fgets` instead of `sizeof buf` (1024).
- Fix a minor discrepancy where a redundant null terminator was written at end of buf.
- Remove redundant call to `strlen` by allocating string with `kk_string_alloc_from_qutf8n` instead of `kk_string_alloc_from_qutf8`.
@arvidjonasson
Copy link
Author

Thanks for the review!

I submitted a new version of kk_os_read_line. This should answer your concerns about it and improve the code quality.
Please send suggestions / concerns :)

@arvidjonasson arvidjonasson requested a review from daanx October 5, 2024 18:28
@daanx
Copy link
Member

daanx commented Oct 5, 2024

Thanks for the quick update -- I'll take a look soon. These things always turn out to have surprising edge cases or portability concerns.

@TimWhiting TimWhiting added this to the Small Release milestone Dec 21, 2024
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.

3 participants