Skip to content

refactor: php_brotli_context struct #60

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 1 commit into from
May 2, 2025
Merged

refactor: php_brotli_context struct #60

merged 1 commit into from
May 2, 2025

Conversation

kjdev
Copy link
Owner

@kjdev kjdev commented May 2, 2025

Summary by CodeRabbit

  • Refactor
    • Centralized and streamlined Brotli context management for improved consistency and maintainability. Context initialization and cleanup are now handled by dedicated functions, reducing manual resource handling.
  • Chores
    • Internal struct definitions were updated and encapsulated, with implementation details hidden from public interfaces. No changes to user-facing functionality.

Copy link

coderabbitai bot commented May 2, 2025

Walkthrough

This change refactors the internal management of Brotli compression contexts in the PHP extension. It introduces a new struct, _php_brotli_context, to centralize the encoder, decoder, and buffer management. Initialization and cleanup of this context are now handled by dedicated functions, replacing manual field assignments and memory management. The header file now forward-declares the context struct instead of exposing its full definition, encapsulating its implementation details. Function signatures remain unchanged, but all usages are updated to work with the new context structure and lifecycle functions.

Changes

File(s) Change Summary
brotli.c Introduced _php_brotli_context struct; added php_brotli_context_init and php_brotli_context_close for context lifecycle; updated all usages to use new struct and functions; replaced manual resource management with centralized functions.
php_brotli.h Removed explicit struct definitions for Brotli context and state context; added forward declaration for php_brotli_context; eliminated exposure of internal structure details.

Sequence Diagram(s)

sequenceDiagram
    participant PHP_User
    participant BrotliExtension
    participant BrotliContext

    PHP_User->>BrotliExtension: Request compress/decompress
    BrotliExtension->>BrotliContext: php_brotli_context_init()
    BrotliExtension->>BrotliContext: Use encoder/decoder, buffers
    BrotliExtension->>BrotliContext: php_brotli_context_close()
    BrotliContext-->>BrotliExtension: Resources cleaned up
    BrotliExtension-->>PHP_User: Return result
Loading

Possibly related PRs

  • kjdev/php-ext-brotli#58: Refactors Brotli context management to use PHP objects and updates related APIs, directly modifying the same context representation and lifecycle as this PR.

Poem

In fields of code where buffers grow,
A context struct now runs the show.
No more stray pointers, all is neat—
Init and close, a tidy feat!
Encoders, decoders, all in line,
Brotli’s bytes compressed just fine.
🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 940c67e and 8d82e9b.

📒 Files selected for processing (2)
  • brotli.c (13 hunks)
  • php_brotli.h (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • php_brotli.h
  • brotli.c
✨ 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: 0

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

145-154: Minor nit – use memset to zero the struct

php_brotli_context_init() manually assigns eight members. A single memset(ctx, 0, sizeof(*ctx)); (or ZEND_SECURE_ZERO) would reduce maintenance overhead when new fields are added.

-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;
-}
+static void php_brotli_context_init(php_brotli_context *ctx)
+{
+    memset(ctx, 0, sizeof(*ctx));
+}

1250-1256: Type-safety nit – cast input buffer to const uint8_t*

ctx->next_in is declared const uint8_t *, but in_buf is a char *.
Assigning without a cast is safe but produces an implicit-conversion warning on some compilers.

-    ctx->next_in = in_buf;
+    ctx->next_in = (const uint8_t *)in_buf;

1263-1274: Resource leak after FINISH?

After a BROTLI_OPERATION_FINISH, the encoder remains allocated until the context object is destroyed.
If user code keeps the object around but never reuses it, memory stays occupied unnecessarily. Consider automatically calling php_brotli_context_close() (or at least destroying ctx->encoder) when FINISH succeeds.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c897fd2 and 940c67e.

📒 Files selected for processing (2)
  • brotli.c (14 hunks)
  • php_brotli.h (1 hunks)
🔇 Additional comments (3)
php_brotli.h (1)

31-31:

✅ Verification successful

Opaque‐struct change looks good, but double-check for out-of-file field access

Forward-declaring _php_brotli_context tightens encapsulation – great move.
Please verify that no other compilation units still dereference members such as ctx->encoder / ctx->decoder directly; otherwise they will now fail to compile.


🏁 Script executed:

#!/bin/bash
# Find any use of php_brotli_context members outside brotli.c
rg --multiline --line-number $'php_brotli_context\\s*\\*[^;]*\\->' | grep -v 'brotli.c'

Length of output: 83


Let's inspect the struct’s members and look specifically for any encoder/decoder accesses outside brotli.c:


🏁 Script executed:

#!/bin/bash
# Show struct fields in php_brotli.h
rg --line-number 'struct _php_brotli_context' -A 10 php_brotli.h

# Search for encoder/decoder member access outside brotli.c
rg --line-number '->encoder' | grep -v 'brotli.c'
rg --line-number '->decoder' | grep -v 'brotli.c'

Length of output: 1334


Let’s rerun the member‐access search using fixed string matching and explicitly excluding the implementation file:


🏁 Script executed:

#!/bin/bash
# Search for any ctx->encoder or ctx->decoder outside brotli.c
rg -n -F '->encoder' . | grep -v 'brotli.c'
rg -n -F '->decoder' . | grep -v 'brotli.c'

Length of output: 897


Let’s retry with a plain grep search—which handles leading “->” cleanly—and exclude brotli.c explicitly:


🏁 Script executed:

#!/bin/bash
# Check for any ctx->encoder or ctx->decoder uses outside brotli.c
grep -R "->encoder" -n . | grep -v "brotli.c:"
grep -R "->decoder" -n . | grep -v "brotli.c:"

Length of output: 315


Let’s retry with grep -- to avoid option parsing on the ->:


🏁 Script executed:

#!/bin/bash
# Look for any ctx->encoder or ctx->decoder outside brotli.c
grep -R -n -- 'ctx->encoder' . | grep -v 'brotli\.c:'
grep -R -n -- 'ctx->decoder' . | grep -v 'brotli\.c:'

Length of output: 111


No external php_brotli_context field accesses detected—change approved.

A search for any ctx->encoder or ctx->decoder references outside brotli.c returned no results, confirming the opaque‐struct encapsulation introduces no breakage.

brotli.c (2)

124-133: Struct refactor LGTM

The consolidated _php_brotli_context clearly separates encoder/decoder state and keeps zend_object std as the last member, so existing XtOffsetOf(..., std) math remains valid.
No functional issues spotted.


156-176: Centralised cleanup handles partial initialisation

php_brotli_context_close() correctly guards each resource and NULLs pointers, making the function idempotent – nice work.

@kjdev kjdev force-pushed the refactor/context branch from 940c67e to 8d82e9b Compare May 2, 2025 00:35
@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.

@kjdev kjdev merged commit 8d82e9b into master May 2, 2025
74 checks passed
@kjdev kjdev deleted the refactor/context branch May 2, 2025 00:42
@coderabbitai coderabbitai bot mentioned this pull request Jun 13, 2025
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