Skip to content

Drop extra copy in get_last_error_text#2044

Open
jakirkham wants to merge 3 commits intorapidsai:mainfrom
jakirkham:drop_xtra_cy_err_cp
Open

Drop extra copy in get_last_error_text#2044
jakirkham wants to merge 3 commits intorapidsai:mainfrom
jakirkham:drop_xtra_cy_err_cp

Conversation

@jakirkham
Copy link
Copy Markdown
Member

In get_last_error_text, the assignment of const char* to bytes results in a copy as bytes must own its memory. After the bytes object is decoded to a Python str. However it is possible to decode directly from const char* to str avoiding the copy to the temporary bytes object. Hence this code makes that change.

Further Python and Cython def functions always return None if nothing else is returned. Hence the logic can be simplified here to only focus on returning the error text.

Cython can run `decode` directly on `const char*`. So do that instead of
copying to `bytes` first before calling `decode`. This saves a copy.
Python/Cython `def` functions always `return None` if no other `return`
is encountered. So simplify the logic here to only `return` the error
text `str` if is `not NULL`.
@jakirkham jakirkham requested a review from a team as a code owner April 29, 2026 21:38
@jakirkham jakirkham added improvement Improves an existing functionality non-breaking Introduces a non-breaking change labels Apr 29, 2026
@jakirkham jakirkham changed the title Drop xtra cy err cp Drop extra copy in get_last_error_text Apr 29, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 129f7b4b-a412-496c-878c-4b32c0829705

📥 Commits

Reviewing files that changed from the base of the PR and between f4e7894 and 243e4e1.

📒 Files selected for processing (1)
  • python/cuvs/cuvs/common/exceptions.pyx
🚧 Files skipped from review as they are similar to previous changes (1)
  • python/cuvs/cuvs/common/exceptions.pyx

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Streamlined error-text handling: decoding now happens directly when an error is present.
    • Adjusted control flow so the error-text path no longer uses an early-return, reducing intermediate steps and simplifying behavior.

Walkthrough

Refactor of get_last_error_text in the exceptions module: the function now decodes and returns the C error string directly when c_err is non-NULL; the previous explicit NULL early-return was removed so the function falls through when c_err is NULL.

Changes

Cohort / File(s) Summary
Exception Error Handling
python/cuvs/cuvs/common/exceptions.pyx
get_last_error_text simplified to decode and return the C error text directly from c_err when non-NULL; removed intermediate bytes variable and the explicit NULL early-return (function now falls through on NULL).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: removing an unnecessary copy operation in the get_last_error_text function, which is the core objective of the changeset.
Description check ✅ Passed The description is directly related to the changeset, explaining the optimization of avoiding a copy via bytes and the simplified control flow, which matches the PR objectives and code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Review rate limit: 8/10 reviews remaining, refill in 10 minutes and 28 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improves an existing functionality non-breaking Introduces a non-breaking change

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant