Skip to content

Optimize ora_regexp_count: drop wasteful text_to_cstring + strlen - #1454

Merged
gaoxueyu merged 1 commit into
IvorySQL:masterfrom
xuhuicai:optimize/ora_regexp_count_remove_cstring_copy
Jul 28, 2026
Merged

gaoxueyu merged 1 commit into
IvorySQL:masterfrom
xuhuicai:optimize/ora_regexp_count_remove_cstring_copy

Conversation

@xuhuicai

@xuhuicai xuhuicai commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Optimization

The ora_regexp_count function had unnecessary full string copy and scanning when counting newline characters \n:

  • Old code: text_to_cstring(s) (palloc + memcpy full copy) + strlen(str) (full scan)
  • New code: Use VARDATA_ANY(s) to point to the original bytes directly + reuse the existing src_text_len (line 767)

Each call saves: 1 palloc + 1 memcpy + 1 full-string strlen.

Behavioral difference

Only matters for the edge case where the string contains embedded \0 — the old strlen truncates at \0, while the new code uses the real byte length, which is more accurate. Embedded \0 in text type is extremely rare. Multi-byte safe: \n (0x0A) does not appear in UTF-8 continuation bytes.

#1455

Summary by CodeRabbit

  • Bug Fixes
    • Improved regular expression count handling for text containing newline characters.
    • Ensured newline occurrences are counted accurately across the full text, including when the text ends with a newline.
    • Removed issues caused by relying on string termination when processing text values.

Replace text_to_cstring(s) + strlen(str) with VARDATA_ANY(s) +
src_text_len to avoid one palloc, one memcpy, and one strlen per
call. The source byte pointer and its length were already available.
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The four-argument ora_regexp_count path now scans the text payload directly using its declared length, counts newline characters, and detects a trailing newline without converting the value to a C-string.

Changes

Regexp count newline scanning

Layer / File(s) Summary
Direct text payload scanning
contrib/ivorysql_ora/src/builtin_functions/character_datatype_functions.c
ora_regexp_count iterates over VARDATA_ANY(s) using src_text_len, counting '\n' bytes and detecting when the final payload byte is a newline.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: optimizing ora_regexp_count by removing text_to_cstring and strlen.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@xuhuicai

Copy link
Copy Markdown
Contributor Author

Related to #1455

@hs-liuxh

hs-liuxh commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for your contribution! The change itself looks good, I'll review it shortly.

One small request: since IvorySQL is an international community, could you please translate the PR description and the linked issue #1455 into English? That will let more people join the review.

@xuhuicai

Copy link
Copy Markdown
Contributor Author

Done. Both the PR description and issue #1455 have been updated to English. Thanks for the reminder.

@hanjianqiao

Copy link
Copy Markdown
Collaborator

Nice work! No behavioral difference here though — IvorySQL doesn't support CHR(0). Merging shortly.

@gaoxueyu
gaoxueyu merged commit 3348206 into IvorySQL:master Jul 28, 2026
9 of 12 checks passed
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.

4 participants