feat: add slice($start, $end) method for range extraction#40
Conversation
Implement Judy::slice() that returns a new Judy array of the same type containing entries within the inclusive range [$start, $end]. Supports all 5 array types with proper deep-copy for MIXED value types.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant new core feature to the Judy PHP extension: the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a slice() method for efficient range extraction from Judy arrays. However, it contains a critical stack-based buffer overflow vulnerability in the handling of string-keyed arrays (JudySL) due to the use of a fixed-size stack buffer for retrieving keys, which does not account for keys exceeding the buffer size. Additionally, there's a potential critical bug in the error handling path that could lead to a double-free, the element counter for integer-keyed array types (BITSET, INT_TO_INT, INT_TO_MIXED) is not being updated, and there is some code duplication in the string-keyed array handling that could be refactored. Specific comments have been added with suggestions to address these points.
Test Results
Benchmark Summary (Judy vs PHP Array)Ratio = Judy / Array. Bold = Judy wins (≤0.95x). Plain = Array is faster/smaller. Time (Write / Read) — Linux
Memory — Linux
Time (Write / Read) — Windows
Memory — Windows
Raw benchmark dataWrite Time — Linux
Read Time — Linux
Memory — Linux
Write Time — Windows
Read Time — Windows
Memory — Windows
|
- Hoist JSLI call + PJERR check out of the if/else for STRING_TO_INT vs STRING_TO_MIXED, reducing duplication - Remove redundant judy_free_array_internal() in alloc_error path; zval_ptr_dtor already triggers free_obj cleanup
Summary
Judy::slice(mixed $start, mixed $end): Judymethod that returns a new Judy array containing entries within the inclusive range[$start, $end]goto alloc_errorImplementation details
$start/$endare integer indicesJ1F/J1Nto iterate,J1Sto insert$start/$endare integer indicesJLF/JLNto iterate,JLIto insert$start/$endare integer indicesJLF/JLNto iterate,JLI+ZVAL_COPY$start/$endare strings (lexicographic)JSLF/JSLN+strcmpboundary$start/$endare strings (lexicographic)JSLF/JSLN+strcmp+ZVAL_COPYEdge cases handled
$start > $end: returns empty array (no iteration)TypeErrorNew helper
judy_create_result()is a generic factory that creates an initialized Judy result object for any type (replaces type-specific helpers for new code).Test plan
tests/slice_bitset_001.phpt-- middle range, full range, single element, no matchtests/slice_int_to_int_001.phpt-- middle range, full range, single element, no matchtests/slice_int_to_mixed_001.phpt-- mixed values, deep copy independence, null valuestests/slice_string_to_int_001.phpt-- lexicographic range, single key, all, no matchtests/slice_string_to_mixed_001.phpt-- lexicographic range, deep copy independencetests/slice_empty_001.phpt-- empty source, reversed range, type errors