Skip to content

refactor: php_brotli_stream_data struct #61

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

Conversation

kjdev
Copy link
Owner

@kjdev kjdev commented May 2, 2025

Summary by CodeRabbit

  • Refactor
    • Improved internal handling of Brotli stream data for more efficient resource management and simplified maintenance. No changes to user-facing features or functionality.

Copy link

coderabbitai bot commented May 2, 2025

Walkthrough

This change refactors the management of Brotli encoder and decoder states within the codebase by consolidating multiple separate fields into a unified context structure. The php_brotli_stream_data struct now contains a single php_brotli_context member, replacing individual pointers and buffer fields. All functions that previously accessed or managed encoder/decoder states and buffers directly are updated to use this new context. Cleanup and initialization routines are also centralized through the context, streamlining resource management and reducing code redundancy.

Changes

File(s) Change Summary
brotli.c Refactored php_brotli_stream_data to embed a php_brotli_context struct, replacing separate encoder/decoder state pointers and buffer management fields. Updated all references and resource management logic to use the unified context.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Stream
    participant php_brotli_context

    User->>Stream: Open Brotli stream
    Stream->>php_brotli_context: Initialize context
    User->>Stream: Read/Write data
    Stream->>php_brotli_context: Use encoder/decoder via context
    User->>Stream: Close stream
    Stream->>php_brotli_context: Cleanup all resources
Loading

Possibly related PRs

  • refactor: php_brotli_context struct #60: Builds upon the refactor by integrating the unified php_brotli_context struct into stream data and updating related state/buffer management.
  • Develop #58: Related refactor that changes Brotli context management to use PHP objects and updates APIs, modifying context/state handling at a different abstraction layer.

Poem

In burrows deep where code does flow,
A single context now does grow.
No more scattered states to find—
Just one neat struct, all combined!
Cleanup’s simple, bugs take flight,
Brotli streams run smooth and light.
🐇✨

✨ 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

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

609-619: 🛠️ Refactor suggestion

⚠️ Potential issue

php_stream_write return value is ignored – potential silent data loss

Inside the flush loop we write compressed data but never verify that the full buffer actually reached the underlying stream. If php_stream_write() returns < out_size, the remaining bytes are silently discarded.

-                php_stream_write(self->stream, output, out_size);
+                if (php_stream_write(self->stream, output, out_size) != out_size) {
+                    php_error_docref(NULL, E_WARNING,
+                                     "brotli: short write on compressed stream");
+                    efree(output);
+                    php_brotli_context_close(&self->ctx);
+                    return EOF;
+                }

Applying the same guard in other write sites (e.g. php_brotli_compress_write) will make the stream more robust.

♻️ Duplicate comments (1)
brotli.c (1)

670-676: Repeat of unchecked php_stream_write

As in the close routine, the incremental write path should validate that the entire chunk was written. See the earlier diff suggestion for a minimal fix.

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

545-560: Repeated heap (re)allocations inside hot path – consider reusing buffers

php_brotli_decompress_read() mallocs a fresh input and output buffer for every read call, then frees them a few lines later. In heavy streaming scenarios this hurts performance and fragments the heap.

A low-impact optimisation is to promote these two buffers into self->ctx (similar to ctx->output on the compress side) and reuse them until the stream is closed.


564-565: Minor: rename out_size to avoid shadow-ing/confusion

size_t out_size = (size_t)(self->ctx.next_out - output); is clear, but note the same identifier name is used in other scopes (e.g. php_brotli_compress_close). Consider chunk_size or written here to avoid shadow-ing during refactors.


576-579: Zero-byte read edge case

If php_stream_read() returns 0 (EOF or error) we still pass input to Brotli with available_in == 0. This is harmless but unnecessary; an early break would avoid an extra decoder round-trip.


777-783: Init path covers zero-initialisation, but ecalloc makes it double work

self is allocated with ecalloc, which already zeroes the memory. Immediately calling php_brotli_context_init(&self->ctx) repeats the work. This is harmless but redundant.

-    self = ecalloc(sizeof(*self), 1);
-    ...
-    php_brotli_context_init(&self->ctx);
+    self = ecalloc(sizeof(*self), 1); /* already zeroed */
+    /* php_brotli_context_init(&self->ctx);  -- not strictly necessary */
📜 Review details

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

📥 Commits

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

📒 Files selected for processing (1)
  • brotli.c (8 hunks)
🔇 Additional comments (5)
brotli.c (5)

480-484: Encapsulation improvement looks good

Replacing the scattered encoder/decoder & buffer fields with one php_brotli_context ctx; greatly simplifies php_brotli_stream_data. The change enhances maintainability and reduces the risk of partial-cleanup leaks because a single php_brotli_context_close() now handles all resources.


507-508: Single-point cleanup is correct

Switching to php_brotli_context_close(&self->ctx) centralises the teardown logic and prevents future drift between close paths. Nicely done.


638-639: Good use of unified teardown

php_brotli_context_close(&self->ctx); here mirrors the decompression path and prevents leaks of the encoder instance or stale output buffer.


781-783: Encoder creation failure path is correct

The new code passes &self->ctx.encoder which guarantees the pointer in the context remains NULL on failure – preventing accidental double free later.


792-796: Symmetric handling for decoder creation looks good

Decoder creation mirrors the encoder path and integrates neatly with the unified context.

@kjdev kjdev merged commit 7fb22f3 into master May 2, 2025
74 checks passed
@kjdev kjdev deleted the refactor/stream_data branch May 2, 2025 01:33
@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