Skip to content

Conversation

@icecrasher321
Copy link
Collaborator

Summary

  • Only load workflow state once and pass it through
  • Only decrypt env vars in parallel once and pass them through

Type of Change

  • Other: Performance

Testing

Tested manually running

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Nov 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Nov 26, 2025 4:07am

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 26, 2025

Greptile Overview

Greptile Summary

Optimized workflow execution performance by eliminating redundant database calls and parallelizing environment variable decryption.

Key improvements:

  • Caches workflow state once in route.ts and passes it through to executeWorkflowCore, preventing duplicate DB queries
  • Parallelizes environment variable decryption in utils.ts using Promise.all instead of sequential for loop
  • Removes redundant decryption loop in execution-core.ts by using pre-decrypted values from getPersonalAndWorkspaceEnv
  • Converts nested async reduce operations to synchronous reduces since decryption is now done upfront

The changes maintain identical functionality while reducing execution time through better resource utilization.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • Score reflects well-designed performance optimizations that maintain functional equivalence: workflow state caching eliminates redundant DB calls without changing behavior, parallel decryption is a straightforward performance win with proper error handling preserved, and the conversion from async to sync reduces is safe since all async work (decryption) is now done upfront
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
apps/sim/app/api/workflows/[id]/execute/route.ts 5/5 Caches workflow state once to avoid duplicate DB calls in executeWorkflowCore and streaming paths
apps/sim/lib/environment/utils.ts 5/5 Parallelizes decryption of environment variables using Promise.all for better performance
apps/sim/lib/workflows/executor/execution-core.ts 5/5 Uses pre-decrypted env vars, converts async reduces to sync, removes redundant decryption loop

Sequence Diagram

sequenceDiagram
    participant Client
    participant Route as execute/route.ts
    participant Utils as environment/utils.ts
    participant Core as execution-core.ts
    participant DB as Database

    Client->>Route: POST /api/workflows/[id]/execute
    Route->>DB: Load workflow state (once)
    DB-->>Route: Workflow data (blocks, edges, loops, parallels)
    Note over Route: Cache workflow data in<br/>cachedWorkflowData
    
    Route->>Route: Create effectiveWorkflowStateOverride<br/>(user override OR cached data)
    
    alt Non-SSE Execution
        Route->>Core: executeWorkflowCore(workflowStateOverride)
        Core->>Utils: getPersonalAndWorkspaceEnv()
        Utils->>DB: Fetch encrypted env vars
        DB-->>Utils: Encrypted variables
        Note over Utils: Decrypt ALL vars in parallel<br/>using Promise.all
        Utils-->>Core: {encrypted, decrypted} env vars
        Note over Core: Use cached workflow state<br/>(no DB reload)
        Note over Core: Use pre-decrypted vars<br/>(no redundant decryption)
        Core->>Core: Process block states (sync reduce)
        Core-->>Route: Execution result
        Route-->>Client: JSON response
    else SSE Execution
        Route->>Route: resolveOutputIds(cachedWorkflowData.blocks)
        Note over Route: Use cached data<br/>(no DB reload)
        Route->>Core: executeWorkflowCore(workflowStateOverride)
        Core->>Utils: getPersonalAndWorkspaceEnv()
        Utils->>DB: Fetch encrypted env vars
        DB-->>Utils: Encrypted variables
        Note over Utils: Decrypt ALL vars in parallel<br/>using Promise.all
        Utils-->>Core: {encrypted, decrypted} env vars
        Note over Core: Use cached workflow state<br/>(no DB reload)
        Note over Core: Use pre-decrypted vars<br/>(no redundant decryption)
        Core->>Core: Process block states (sync reduce)
        Core-->>Route: Execution result (streaming)
        Route-->>Client: SSE stream
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@icecrasher321 icecrasher321 requested a review from Sg312 November 26, 2025 04:08
@icecrasher321 icecrasher321 merged commit 0830490 into staging Nov 26, 2025
9 checks passed
@waleedlatif1 waleedlatif1 deleted the improvement/perf-workflow-exec branch November 28, 2025 06:01
waleedlatif1 added a commit that referenced this pull request Nov 29, 2025
…pylon, intercom, mailchimp, loading optimizations (#2132)

* fix(memory-util): fixed unbounded array of gmail/outlook pollers causing high memory util, added missing db indexes/removed unused ones, auto-disable schedules/webhooks after 10 consecutive failures (#2115)

* fix(memory-util): fixed unbounded array of gmail/outlook pollers causing high memory util, added missing db indexes/removed unused ones, auto-disable schedules/webhooks after 10 consecutive failures

* ack PR comments

* ack

* improvement(teams-plan): seats increase simplification + not triggering checkout session (#2117)

* improvement(teams-plan): seats increase simplification + not triggering checkout session

* cleanup via helper

* feat(tools): added sentry, incidentio, and posthog tools (#2116)

* feat(tools): added sentry, incidentio, and posthog tools

* update docs

* fixed docs to use native fumadocs for llms.txt and copy markdown, fixed tool issues

* cleanup

* enhance error extractor, fixed posthog tools

* docs enhancements, cleanup

* added more incident io ops, remove zustand/shallow in favor of zustand/react/shallow

* fix type errors

* remove unnecessary comments

* added vllm to docs

* feat(i18n): update translations (#2120)

* feat(i18n): update translations

* fix build

---------

Co-authored-by: waleedlatif1 <waleedlatif1@users.noreply.github.com>

* improvement(workflow-execution): perf improvements to passing workflow state + decrypted env vars (#2119)

* improvement(execution): load workflow state once instead of 2-3 times

* decrypt only in get helper

* remove comments

* remove comments

* feat(models): host google gemini models (#2122)

* feat(models): host google gemini models

* remove unused primary key

* feat(i18n): update translations (#2123)

Co-authored-by: waleedlatif1 <waleedlatif1@users.noreply.github.com>

* feat(tools): added zendesk, pylon, intercom, & mailchimp (#2126)

* feat(tools): added zendesk, pylon, intercom, & mailchimp

* finish zendesk and pylon

* updated docs

* feat(i18n): update translations (#2129)

* feat(i18n): update translations

* fixed build

---------

Co-authored-by: waleedlatif1 <waleedlatif1@users.noreply.github.com>

* fix(permissions): add client-side permissions validation to prevent unauthorized actions, upgraded custom tool modal (#2130)

* fix(permissions): add client-side permissions validation to prevent unauthorized actions, upgraded custom tool modal

* fix failing test

* fix test

* cleanup

* fix(custom-tools): add composite index on custom tool names & workspace id (#2131)

---------

Co-authored-by: Vikhyath Mondreti <vikhyathvikku@gmail.com>
Co-authored-by: waleedlatif1 <waleedlatif1@users.noreply.github.com>
DarkShark-RAz pushed a commit to DarkShark-RAz/sim that referenced this pull request Nov 30, 2025
…w state + decrypted env vars (simstudioai#2119)

* improvement(execution): load workflow state once instead of 2-3 times

* decrypt only in get helper

* remove comments

* remove comments
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