Skip to content
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

add type hashing #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

add type hashing #8

wants to merge 3 commits into from

Conversation

withinboredom
Copy link

@withinboredom withinboredom commented Apr 5, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a constraint caching mechanism to enhance type validation and class inheritance behavior, improving performance and reliability.
    • Added new functions for managing type constraints in class inheritance.
  • Bug Fixes

    • Enhanced attribute handling to ensure the integrity of the AST during compilation.
  • Refactor

    • Streamlined attribute handling and memory management to support robust execution and maintain consistent type-checking improvements.

Copy link

coderabbitai bot commented Apr 5, 2025

Walkthrough

The pull request introduces a new constraint_cache member in the compiler and executor global structures, along with corresponding allocation, initialization, and cleanup logic. It updates compilation functions to restore AST attributes and handle the nodiscard flag properly. The type-checking routines are enhanced by adding an extra parameter for the original type, and new functions are added to manage type constraints during class inheritance. Function declarations in headers are updated accordingly to support these modifications.

Changes

File(s) Change Summary
Zend/zend.c, Zend/zend_globals.h, Zend/zend_execute_API.c Added new constraint_cache member to global structures; implemented memory allocation, initialization (in startup/constructor), assignment (in executor init), and cleanup (in destructor/post-startup) for the cache.
Zend/zend_compile.c Updated function signatures and logic in zend_compile_typename_ex and zend_compile_func_decl_ex to restore AST attributes and handle the nodiscard flag more robustly.
Zend/zend_execute.c Modified zend_check_intersection_type_from_cache_slot to accept an additional original_type parameter; updated related calls in zend_check_type_slow for enhanced type checking.
Zend/zend_inheritance.c, Zend/zend_inheritance.h Introduced three new functions (zend_type_satisfied_by_class, zend_type_is_satisfied_by_class, zend_hash_type) to compute a hash for a type and to check/manage type constraints during class inheritance.

Sequence Diagram(s)

sequenceDiagram
    participant Startup
    participant CompilerGlobals
    participant ExecutorGlobals

    Startup->>CompilerGlobals: Allocate & initialize constraint_cache
    CompilerGlobals->>ExecutorGlobals: Assign constraint_cache in init_executor
    ExecutorGlobals->>System: Ready with initialized constraint_cache
Loading
sequenceDiagram
    participant InheritanceHandler
    participant ConstraintCache
    participant ClassEntry

    InheritanceHandler->>ConstraintCache: Compute hash via zend_hash_type(type)
    alt Constraint exists in cache
        ConstraintCache-->>InheritanceHandler: Return constraint
    else Not exists
        InheritanceHandler->>ConstraintCache: Allocate new HashTable and cache it
    end
    InheritanceHandler->>ClassEntry: Verify type constraint satisfaction
Loading

Poem

I'm a hopping rabbit in code so bright,
Skipping through caches in the glowing night.
New fields and functions, neat paths to trace,
Type checks and constraints keeping a steady pace.
With every line I hop in pure delight,
Celebrating these changes with a carrot bite!
🥕🐇 Happy coding under moonlight!


📜 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 f1ef5e8 and bae3c97.

📒 Files selected for processing (1)
  • Zend/zend.c (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • Zend/zend.c
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: LINUX_X64_DEBUG_ZTS_ASAN
  • GitHub Check: LINUX_X64_RELEASE_NTS
  • GitHub Check: WINDOWS_X64_ZTS
  • GitHub Check: MACOS_ARM64_DEBUG_NTS
  • GitHub Check: FREEBSD
  • GitHub Check: LINUX_X32_DEBUG_ZTS
  • GitHub Check: BENCHMARKING

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

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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 (4)
Zend/zend_execute.c (4)

1165-1167: Verify handling of the return value from zend_type_satisfied_by_class(...).

The call to zend_type_satisfied_by_class(original_type, ce); returns a boolean, but its result is not used here. If this is intentional (e.g., for side effects like caching), consider adding a short comment to clarify why you’re not checking or storing that result, ensuring future maintainers understand the design intent.


1178-1179: Use true instead of 1 to express boolean return.

Since the function returns a bool, returning true instead of 1 would improve clarity and consistency with the rest of the code.

Apply this small change:

-    return 1;
+    return true;

1188-1189: Consider DRYing up repeated intersection-type checks.

This block repeats a call to zend_check_intersection_type_from_cache_slot(...) in the same function. If possible, refactor to avoid duplication, or add a comment describing why it is necessary to repeat the call with different parameters and conditions.


1197-1197: Avoid ignoring the boolean return value from zend_type_satisfied_by_class(type, ce).

Like the earlier usage, the return value from zend_type_satisfied_by_class(...) is not captured. If this call is solely for side effects, consider a comment clarifying that the result is intentionally discarded. Otherwise, you may want to check or propagate the boolean outcome for consistency.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d80682e and b275f58.

📒 Files selected for processing (7)
  • Zend/zend.c (4 hunks)
  • Zend/zend_compile.c (2 hunks)
  • Zend/zend_execute.c (5 hunks)
  • Zend/zend_execute_API.c (1 hunks)
  • Zend/zend_globals.h (2 hunks)
  • Zend/zend_inheritance.c (1 hunks)
  • Zend/zend_inheritance.h (1 hunks)
👮 Files not reviewed due to content moderation or server errors (1)
  • Zend/zend_compile.c
🧰 Additional context used
🧬 Code Definitions (3)
Zend/zend_inheritance.h (3)
Zend/zend_execute.c (10)
  • bool (242-249)
  • bool (809-828)
  • bool (1031-1046)
  • bool (1048-1056)
  • bool (1090-1100)
  • bool (1171-1240)
  • bool (1242-1259)
  • bool (1261-1266)
  • bool (1268-1282)
  • bool (1284-1294)
Zend/zend_execute_API.c (2)
  • bool (725-728)
  • bool (1160-1168)
Zend/zend_inheritance.c (3)
  • bool (2044-2077)
  • bool (2208-2215)
  • bool (3879-3901)
Zend/zend_inheritance.c (1)
Zend/zend_inheritance.h (7)
  • zend_type_satisfied_by_class (32-32)
  • ce (38-38)
  • ce (42-42)
  • ce (46-46)
  • ce (47-47)
  • zend_hash_type (30-30)
  • zend_type_is_satisfied_by_class (31-31)
Zend/zend_execute.c (1)
Zend/zend_inheritance.h (6)
  • ce (38-38)
  • ce (42-42)
  • ce (46-46)
  • ce (47-47)
  • zend_type_satisfied_by_class (32-32)
  • zend_type_is_satisfied_by_class (31-31)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: WINDOWS_X64_ZTS
  • GitHub Check: LINUX_X64_DEBUG_ZTS_ASAN
  • GitHub Check: MACOS_ARM64_DEBUG_NTS
  • GitHub Check: LINUX_X64_RELEASE_NTS
  • GitHub Check: FREEBSD
  • GitHub Check: BENCHMARKING
  • GitHub Check: LINUX_X32_DEBUG_ZTS
🔇 Additional comments (14)
Zend/zend_globals.h (2)

97-97: Implementation of constraint_cache in compiler_globals looks good.

This adds a new HashTable to store type constraints cache in the compiler globals structure, which will be used for optimizing type constraint checks during compilation.


195-195: Implementation of constraint_cache in executor_globals looks good.

Adding the constraint cache to executor globals structure will make it accessible during runtime execution. This mirrors the same field in compiler_globals ensuring consistency between compile-time and runtime.

Zend/zend.c (4)

727-729: Proper initialization of constraint_cache.

The constraint cache is properly initialized as a HashTable with size 64 and no specific destructor. The allocation and initialization follow the same pattern as other caches in this function.


800-806: Complete and correct cleanup of constraint_cache.

This implementation correctly handles the destruction of the constraint cache:

  1. Iterates through each entry in the cache
  2. Destroys each HashTable entry
  3. Frees the allocated memory
  4. Destroys and frees the main cache itself

This ensures proper memory management and prevents memory leaks.


1042-1042: Memory allocation for constraint_cache during startup.

This line allocates memory for the constraint_cache during the compiler globals initialization phase, following the pattern used for other hash tables in this function.


1138-1139: Proper cleanup during post-startup.

These lines ensure the constraint_cache is properly freed and set to NULL during the post-startup phase, consistent with other cleanup operations in this function.

Zend/zend_execute_API.c (1)

151-151: Sharing constraint_cache between compiler and executor globals.

This line ensures that the executor globals point to the same constraint_cache as compiler globals, consistent with how function_table and class_table are handled. This allows runtime execution to access the type constraints cached during compilation.

Zend/zend_inheritance.h (1)

30-32: New type hashing and validation functions.

These functions add important capabilities for type handling:

  1. zend_hash_type: Creates a hash from a zend_type structure (marked as FASTCALL for optimization)
  2. zend_type_is_satisfied_by_class: Checks whether a class satisfies a specific type constraint
  3. zend_type_satisfied_by_class: Registers a class as satisfying a type constraint

These functions will improve type checking performance by caching type validation results, particularly in class inheritance scenarios.

Zend/zend_inheritance.c (3)

2197-2206: Implementation of type constraint caching.

This function records that a type is satisfied by a given class by storing the class name in a constraint cache. It uses a hash based on the type pointer to identify the constraint.


2208-2215: Check added for type constraint satisfaction.

Good implementation that checks if a type has been satisfied by a class by looking up the constraint in the cache and verifying the class existence.


2217-2220: Simple type hashing implementation.

The function uses the memory address of the type as its hash value. This is efficient but relies on memory address uniqueness for the lifetime of the type.

Zend/zend_execute.c (3)

1143-1143: Add documentation or checks for the new original_type parameter.

A new parameter zend_type *original_type has been introduced to this function signature. It may be worth updating any associated comments or doc blocks to clarify its purpose and ensure that the value passed in is valid (e.g., not NULL). If there's a scenario in which original_type could be NULL, consider adding defensive checks.


1147-1147: Initialization of ce to NULL is fine but double-check usage flow.

Declaring zend_class_entry *ce = NULL; at this point is fine for clarity, but verify that it's always assigned before use. Ensure that error paths do not rely on an uninitialized ce.


1184-1184: Confirm usage of type vs. original_type.

Here, the function forwards type as the first argument to zend_check_intersection_type_from_cache_slot(...). Verify that this is consistent with the new parameter's intended usage, rather than original_type, to avoid any subtle mismatch in logic or data.

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)
Zend/zend_execute.c (3)

1165-1167: Consider handling the return value from zend_type_satisfied_by_class.
Currently, the result of zend_type_satisfied_by_class is ignored. If it returns false, we’re not logging or acting on it. Verify if the function call is for side effects only or if the boolean result needs to be utilized.


1184-1184: Directly returning from intersection type check.
Returning immediately helps performance by skipping further checks. Confirm that no additional coverage is needed for subsequent logic (e.g., fallback union branches).


1197-1197: Unused return value from zend_type_satisfied_by_class.
The function’s boolean return is never checked. If the intention is purely side-effect-based, clarify with comments; otherwise, ensure correct handling of a potential false outcome.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b275f58 and f1ef5e8.

📒 Files selected for processing (1)
  • Zend/zend_execute.c (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: WINDOWS_X64_ZTS
  • GitHub Check: MACOS_ARM64_DEBUG_NTS
  • GitHub Check: LINUX_X64_DEBUG_ZTS_ASAN
  • GitHub Check: LINUX_X64_RELEASE_NTS
  • GitHub Check: LINUX_X32_DEBUG_ZTS
  • GitHub Check: BENCHMARKING
  • GitHub Check: FREEBSD
🔇 Additional comments (3)
Zend/zend_execute.c (3)

1143-1143: Added original_type parameter in function signature.
This new parameter appears essential for additional checks, but ensure non-null usage is always valid. Verify if extra validations or null checks are necessary to avoid unexpected crashes.


1177-1180: Early return on class satisfaction.
This snippet correctly short-circuits if zend_type_is_satisfied_by_class is true, avoiding unnecessary checks. However, ensure no required side effects are skipped by this early return.


1188-1188: Short-circuit for union case with intersection type.
If this call succeeds, the code returns without proceeding. Ensure the function’s behavior is correct for other union parts and confirm no unintended skipping of needed validations.

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