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 lazy sequence support for delayed promises #10

Merged
merged 3 commits into from
Sep 30, 2024
Merged

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Sep 27, 2024

Purpose

Introduces the DelaySeq function to enhance the management of sequential execution of promise functions through a lazy, pull-based technique.

Rationale

Currently, the Delay function executes promise functions by iterating over a variadic slice. While this approach works well for a small number of promises, it can cause performance degradation and increased memory consumption when handling larger sequences. This issue might arise for instance when implementing native predicates that traverse solution spaces in databases -and more specifically, in the case of axone, when we query the state of the blockchain-, potentially generating a vast number of alternatives.

Implementation details

The DelaySeq function is implemented using a very and minimal pull mechanism based on a single function. This design enables clients to create an on-demand promise generator in a straightforward and efficient way. The impact of the change is also very slight and limited to the engine/promise file.

why Not Use Go 1.23 iterators?

Go 1.23 iterators operate on a push model, which inverts the control flow compared to the pull-based strategy used here. Although Go provides a mechanism to convert push-style iterators into pull-style ones, this comes with several implications. First, it creates a goroutine under the hood, and second, a cleanup mechanism must absolutely be called to prevent resource leaks. This makes it quite tricky to use in implementing promises in this context.

@ccamel ccamel self-assigned this Sep 27, 2024
Copy link

coderabbitai bot commented Sep 27, 2024

Walkthrough

The pull request introduces various modifications across multiple files in the codebase. Key changes include reorganization of import statements, removal of specific control flow statements (such as break), and significant refactoring of the Promise structure to enhance its execution model. Additionally, new test functions have been added to validate the behavior of the modified promise execution. The removal of certain fields from structs indicates adjustments in how data is managed within the interpreter and related components.

Changes

Files Change Summary
engine/builtin.go, engine/parser.go Import statements for the orderedmap package have been repositioned. Adjustments made to function calls, including prefixing with an underscore to ignore return values and simplifying slice appending logic.
engine/lexer.go Removal of a break statement in the fraction method, altering control flow for decimal digit handling.
engine/promise.go Refactoring of the Promise structure and its functions, including changes to the delayed field, introduction of NextFunc, and updates to several method signatures to accept PromiseFunc.
engine/promise_test.go Introduction of a new test function for delayed sequence executions and renaming of an existing test function.
engine/term.go Removal of the prefixMinus field from the WriteOptions struct and repositioning of the orderedmap import statement.
interpreter.go Removal of the loaded field from the Interpreter struct and repositioning of an import statement.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Promise
    participant NextFunc

    Caller->>Promise: Create Promise
    Promise->>NextFunc: Create Next Function
    NextFunc->>Promise: Return Next Function
    Promise->>Caller: Return Promise
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.

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

Copy link

codecov bot commented Sep 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #10   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files          23       23           
  Lines        6561     6579   +18     
=======================================
+ Hits         6449     6467   +18     
  Misses         81       81           
  Partials       31       31           
Files with missing lines Coverage Δ
engine/builtin.go 99.56% <100.00%> (-0.01%) ⬇️
engine/lexer.go 100.00% <ø> (ø)
engine/parser.go 99.51% <ø> (-0.01%) ⬇️
engine/promise.go 98.01% <100.00%> (+0.51%) ⬆️
engine/term.go 100.00% <ø> (ø)
engine/text.go 97.93% <100.00%> (ø)
interpreter.go 97.20% <ø> (ø)

@ccamel ccamel changed the title Feat/delay seq ✨ add promise.DelaySeq function Sep 27, 2024
@ccamel ccamel changed the title ✨ add promise.DelaySeq function ✨ add lazy sequence support for delayed promises. Sep 27, 2024
@ccamel ccamel changed the title ✨ add lazy sequence support for delayed promises. ✨ add lazy sequence support for delayed promises Sep 27, 2024
@ccamel ccamel marked this pull request as ready for review September 27, 2024 12:12
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: 4

🧹 Outside diff range and nitpick comments (3)
engine/builtin.go (1)

Line range hint 2844-2954: Approve with suggestions: New Length function implementation.

The new Length function is a comprehensive implementation for determining the length of a list in Prolog. It handles various cases including partial lists, non-lists, and potential infinite lists. This aligns well with the PR objectives of improving the management of sequential execution.

Suggestions for improvement:

  1. Consider adding more inline comments to explain the complex logic, especially in the main switch statement.
  2. The error handling for resourceFiniteMemory might benefit from a brief explanation of when and why this error could occur.

Overall, this is a solid implementation that seems to address many edge cases. Good job!

engine/promise_test.go (1)

77-114: Add comments to clarify complex test logic for better readability

The test function contains intricate logic within nested functions and test cases. Adding explanatory comments would enhance readability and maintainability. Consider detailing:

  • The purpose and flow of the NextFunc, and how it generates promise functions.
  • The reasoning behind the condition v%((v%4)+1) == 0.
  • How the expected wantOk and wantRes values are derived for each test case.
engine/promise.go (1)

36-36: Clarify the documentation comment for the Delay function

The current comment:

// Should be used with reasonable quantity of k, otherwise prefer DelaySeq.

could be rephrased for better clarity. Consider:

// Use `Delay` when the number of promise functions (`k`) is small.
// For larger sequences, prefer using `DelaySeq` to avoid performance issues.

This makes it clearer when to use Delay versus DelaySeq.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7819528 and 7eaf42d.

📒 Files selected for processing (8)
  • engine/builtin.go (3 hunks)
  • engine/lexer.go (0 hunks)
  • engine/parser.go (1 hunks)
  • engine/promise.go (6 hunks)
  • engine/promise_test.go (2 hunks)
  • engine/term.go (1 hunks)
  • engine/text.go (2 hunks)
  • interpreter.go (1 hunks)
💤 Files not reviewed due to no reviewable changes (1)
  • engine/lexer.go
🧰 Additional context used
📓 Learnings (1)
interpreter.go (1)
Learnt from: bdeneux
PR: axone-protocol/prolog#3
File: interpreter.go:20-20
Timestamp: 2024-07-16T12:32:15.725Z
Learning: The `loaded` field in the `Interpreter` struct of the `interpreter.go` file is retained despite being currently unused because it was added by the original author of the library and might be used in future updates.
🔇 Additional comments (10)
engine/term.go (3)

7-8: LGTM: Import statement repositioned.

The orderedmap import has been moved and an empty line added for better organization. This change doesn't affect functionality and improves code readability.


Line range hint 1-144: Summary of changes in engine/term.go

  1. The orderedmap import has been repositioned for better organization.
  2. The prefixMinus field has reportedly been removed from the WriteOptions struct, but this change is not visible in the provided code and requires verification.

These changes appear to be minor refactoring efforts. However, please ensure that the removal of prefixMinus (if confirmed) doesn't negatively impact the functionality of the WriteOptions struct or any dependent code.


Line range hint 22-33: Verify the removal of prefixMinus field and its impact.

The AI summary mentions the removal of the prefixMinus field from the WriteOptions struct, but this change is not visible in the provided code. Please confirm if this field has indeed been removed.

If confirmed, ensure that:

  1. All references to prefixMinus have been removed or updated throughout the codebase.
  2. The removal doesn't negatively impact the writing or formatting of terms.
  3. Any logic that depended on prefixMinus has been appropriately adjusted.

To verify the removal and its impact, run the following script:

✅ Verification successful

Verified: The prefixMinus field has been successfully removed from WriteOptions. No remaining references or impacts were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the removal of prefixMinus and check for any remaining references

# Test 1: Check if prefixMinus is still present in WriteOptions
echo "Test 1: Checking for prefixMinus in WriteOptions"
ast-grep --lang go --pattern 'type WriteOptions struct {
  $$$
  prefixMinus $_
  $$$
}'

# Test 2: Search for any remaining references to prefixMinus
echo "Test 2: Searching for references to prefixMinus"
rg 'prefixMinus' --type go

# Test 3: Check for any recent commits mentioning prefixMinus
echo "Test 3: Checking recent commits"
git log -n 5 --grep="prefixMinus"

Length of output: 436

engine/text.go (1)

114-114: Approved: Removal of unused variable assignment

This change removes the assignment of the unused arg variable, which is a good practice for code cleanliness. The modification doesn't affect the logic of the function, as the second return value from piArg was apparently not used in the subsequent code.

interpreter.go (3)

Line range hint 1-13: LGTM: Import statement reorganization.

The reorganization of import statements and the addition of an empty line after them improve code readability and align with Go's standard formatting practices.


Line range hint 1-300: Summary and request for additional context

The changes in this file are minimal, primarily involving the reorganization of import statements and the removal of the loaded field from the Interpreter struct. While these changes improve code cleanliness, they don't directly relate to the PR objectives of adding lazy sequence support for delayed promises.

Could you please provide more context on how these changes contribute to the overall goal of the PR? Are there other files where the main functionality changes (e.g., the introduction of DelaySeq) are implemented?

To get a better understanding of the changes across the codebase, please run the following script:

#!/bin/bash
# List all modified files in this PR
git diff --name-only origin/main

# Search for the new DelaySeq function
rg --type go 'func.*DelaySeq'

This will help us identify where the main changes for lazy sequence support are implemented.


Line range hint 18-21: Please clarify the removal of the loaded field.

The loaded field has been removed from the Interpreter struct. While removing unused fields generally improves code cleanliness, a previous learning note indicated that this field was retained for potential future use. Could you please clarify:

  1. The reasoning behind removing this field now?
  2. How does this removal relate to the introduction of lazy sequence support mentioned in the PR objectives?
  3. Are there any potential impacts on existing or planned functionality?

To ensure this change doesn't affect other parts of the codebase, please run the following script:

✅ Verification successful

Removal of loaded field verified.

The loaded field has been successfully removed from the Interpreter struct, and no references to it exist in the codebase. This removal enhances code cleanliness and does not impact current or planned functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any references to the 'loaded' field in the Interpreter struct
rg --type go 'Interpreter.*loaded'

Length of output: 271

engine/parser.go (2)

12-13: LGTM: Import statement reorganization

The relocation of the orderedmap import statement is a minor change that improves code organization without affecting functionality.


Line range hint 1024-1097: Verify the impact of removing the break statement

The AI summary mentions the removal of a break statement from the term0 function. However, this change is not visible in the provided code snippet. The removal of a break statement can significantly alter the control flow of a function.

Please provide more context about this change, including:

  1. The exact location of the removed break statement.
  2. The reasoning behind its removal.
  3. Any compensating changes made to ensure the function still behaves as expected.

Additionally, it's crucial to verify that the term0 function still operates correctly after this change.

To help verify the change, you can run the following script to compare the old and new versions of the term0 function:

This script will show the differences between the old and new versions of the term0 function, helping to identify the removed break statement and any other changes.

engine/builtin.go (1)

2430-2430: LGTM: Simplified stream appending.

This change simplifies the code by directly appending all elements from vm.streams.elems to the streams slice. It's a good optimization that doesn't alter the underlying logic or functionality.

engine/builtin.go Show resolved Hide resolved
engine/promise_test.go Show resolved Hide resolved
engine/promise.go Show resolved Hide resolved
engine/promise.go Show resolved Hide resolved
Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

Looks good, nice one :)

Copy link

@bdeneux bdeneux left a comment

Choose a reason for hiding this comment

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

Nice 🤩

@ccamel ccamel merged commit a7e37dc into main Sep 30, 2024
2 checks passed
@ccamel ccamel deleted the feat/delay-seq branch September 30, 2024 13:12
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.

3 participants