Skip to content

Refactor/coder create #62

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

Merged
merged 2 commits into from
May 2, 2025
Merged

Refactor/coder create #62

merged 2 commits into from
May 2, 2025

Conversation

kjdev
Copy link
Owner

@kjdev kjdev commented May 2, 2025

Summary by CodeRabbit

  • Refactor
    • Improved internal management of compression and decompression processes for more efficient resource handling. No changes to user-facing functionality or behavior.

Copy link

coderabbitai bot commented May 2, 2025

Walkthrough

This change refactors the Brotli PHP extension by introducing a unified php_brotli_context struct that encapsulates both encoder and decoder states, as well as associated buffers and pointers. The previous separate creation functions for encoder and decoder are replaced with context-based functions. All relevant function calls are updated to use the new context struct, and a context initialization function is added. The core compression and decompression logic remains unchanged, with only the context management interface being consolidated and modernized.

Changes

File(s) Change Summary
brotli.c Introduced php_brotli_context struct to encapsulate encoder/decoder states and buffers; added context initialization and creation functions; replaced encoder/decoder creation calls with context-based functions; updated all relevant code to use the new context struct.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PHP_Extension
    participant Brotli_Lib

    User->>PHP_Extension: Request compression/decompression
    PHP_Extension->>PHP_Extension: Initialize php_brotli_context
    alt Compression
        PHP_Extension->>PHP_Extension: php_brotli_context_create_encoder
    else Decompression
        PHP_Extension->>PHP_Extension: php_brotli_context_create_decoder
    end
    PHP_Extension->>Brotli_Lib: Perform compression/decompression using context
    Brotli_Lib-->>PHP_Extension: Return result
    PHP_Extension-->>User: Return processed data
Loading

Possibly related PRs

  • refactor: php_brotli_stream_data struct #61: Embeds the same php_brotli_context struct inside php_brotli_stream_data and updates related state/buffer management, directly related to this refactor.
  • Develop #58: Replaces resource-based context handles with PHP objects and registers classes for compression/decompression contexts, modifying the same struct and functions for unified context handling.
  • refactor: php_brotli_context struct #60: Refactors the php_brotli_context struct and its management functions, consolidating encoder and decoder states, directly connecting to this PR's changes.

Poem

In the warren of code, a context was born,
Combining both states, so no one’s forlorn.
Encoders and decoders, now side by side,
In a single struct, they happily reside.
With buffers and pointers, all neat and compact,
This rabbit hops forward—refactoring intact!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kjdev
Copy link
Owner Author

kjdev commented May 2, 2025

@coderabbitai review

Copy link

coderabbitai bot commented May 2, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
brotli.c (1)

111-143: ⚠️ Potential issue

Check return values of BrotliEncoderSetParameter to avoid silent failure

The three BrotliEncoderSetParameter calls may legitimately fail (e.g. if Brotli adds range checks in future versions).
Right now failure is ignored, leaving the context in an indeterminate state and leaking the freshly created encoder instance.

-    BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_QUALITY, quality);
-    BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_LGWIN, lgwin);
-    BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_MODE, mode);
+    if (!BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_QUALITY, quality) ||
+        !BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_LGWIN, lgwin)     ||
+        !BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_MODE, mode)) {
+        BrotliEncoderDestroyInstance(ctx->encoder);
+        ctx->encoder = NULL;
+        return FAILURE;
+    }

Fail‐fast behaviour makes caller-side error handling reliable and prevents memory leaks.

🧹 Nitpick comments (2)
brotli.c (2)

89-98: Consider tracking buffer capacity & improving documentation

Great to see the unified context struct – it will remove a lot of duplicated plumbing.
Two small suggestions:

  1. output is treated as a grow-able buffer in several places (erealloc, pointer arithmetics). Storing an additional size_t output_capacity (or re-using available_out) inside the struct would make it easier to reason about the currently allocated size and would guard against accidental overruns.

  2. A short comment on the lifetime semantics of each pointer (e.g. “owned by context / borrowed from caller”) will help future maintainers, especially now that the same struct is shared by encoder and decoder paths.


100-109: php_brotli_context_init() – zero the entire struct in one go

Manually assigning every member works, but using memset(ctx, 0, sizeof(*ctx)) at the top of this helper would:

  • keep the function correct if new fields are added later,
  • avoid the risk of missing a member, and
  • produce identical machine code after optimisation.

Not critical, yet a one-liner makes the intent unmistakable.

 static void php_brotli_context_init(php_brotli_context *ctx)
 {
-    ctx->encoder = NULL;
-    ctx->decoder = NULL;
-    ctx->available_in = 0;
-    ctx->next_in = NULL;
-    ctx->available_out = 0;
-    ctx->next_out = NULL;
-    ctx->output = NULL;
+    memset(ctx, 0, sizeof(*ctx));
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7fb22f3 and e450471.

📒 Files selected for processing (1)
  • brotli.c (8 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: ci (7.1-alpine, ghcr.io/kjdev/php, linux/amd64)
  • GitHub Check: ci (8.1-zts-alpine, system, apcu, php, linux/amd64)
  • GitHub Check: ci (7.2-alpine, ghcr.io/kjdev/php, linux/amd64)
  • GitHub Check: ci (7.4-alpine, ghcr.io/kjdev/php, linux/amd64)
  • GitHub Check: ci (8.3-alpine, apcu, php, linux/386)
  • GitHub Check: ci (8.2-zts-alpine, php, linux/amd64)
  • GitHub Check: ci (8.3-zts-alpine, system, apcu, php, linux/amd64)
  • GitHub Check: ci (8.4-alpine, system, apcu, php, linux/amd64)
  • GitHub Check: ci (8.2-zts-alpine, apcu, php, linux/amd64)
  • GitHub Check: ci (8.3-zts-alpine, php, linux/amd64)
  • GitHub Check: ci (8.2-alpine, php, linux/amd64)
  • GitHub Check: ci (8.4-alpine, php, linux/amd64)
  • GitHub Check: ci (8.2, x86, ts, vs16, true)
  • GitHub Check: ci (8.2, x86, ts, vs16, false)
  • GitHub Check: ci (8.2, x86, nts, vs16, true)
  • GitHub Check: ci (8.3, x64, ts, vs16, true)
  • GitHub Check: ci (8.3, x86, ts, vs16, false)
  • GitHub Check: ci (8.4, x64, ts, vs17, true)
  • GitHub Check: ci (8.4, x86, nts, vs17, true)
🔇 Additional comments (5)
brotli.c (5)

230-235: Good guard around encoder initialisation in output handler

The new call to php_brotli_context_create_encoder() simplifies output-handler setup and correctly aborts on failure – nice!


303-305: Restart branch mirrors start-up logic correctly

Re-initialising the context after a CLEAN event via the same helper keeps both branches consistent – looks good.


780-782: Stream opener — parameter propagation is sound

Both the write (compress) and read (decompress) paths now rely on the consolidated helpers. Error handling propagates FAILURE back to the caller and cleans up the partially-initialised stream; behaviour is identical to the old code. 👍

Also applies to: 791-792


1181-1187: Incremental compress init passes user-supplied mode verbatim

The wrapper correctly forwards mode to the helper; with the stricter validation added earlier this remains safe.


1351-1355: Incremental uncompress init keeps parity with compress init

Decoder context creation is now symmetric with the encoder path – no issues spotted.

@kjdev kjdev merged commit e450471 into master May 2, 2025
74 checks passed
@kjdev kjdev deleted the refactor/coder-create branch May 2, 2025 01:56
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.

1 participant