- Without: 1 second "got prompt" delay
- With: 0.1 second "got prompt" delay 🤯
A server-side ComfyUI extension that rewrites the submitted prompt graph before validation to constant-fold switch/selector nodes and optionally prune now-unreachable branches.
This targets the common performance bottleneck in large workflows where ComfyUI’s prompt validation recursively traverses linked upstream nodes even when a conditional branch will not execute.
This extension is designed to be used in tandem with node packs that provide conditional routing:
- Akatz-Loop-Nodes — provides
LazySwitch,LazyIndexSwitch,LazyConditional - ComfyUI-KJNodes — provides
LazySwitchKJ
It can also fold non-lazy switch-like nodes when their decision input resolves to a prompt-time constant.
- Constant-folding: if a switch decision is constant at prompt submission time, it rewires downstream links so consumers connect directly to the selected branch.
- Pruning (optional): if
PRUNE=1, it removes nodes that become unreachable upstream of the execution targets (output nodes or partial-execution targets).
Tip
This extension does not change your workflow file on disk; it simply modifies the prompt dict sent to validation/execution.
LazySwitch/LazySwitchKJ(booleanswitch,on_true,on_false)LazyIndexSwitch(integerindex,value0..valueN)LazyConditional(conditionN,valueN,else)
If a node exposes one of these exact input signatures, it is treated as a switch and can be folded when the decision is constant:
switch,on_true,on_falsecondition,if_true,if_falseindexwithvalue0,value1, ...
Copy this folder to:
ComfyUI/custom_nodes/ComfyUI-GraphConstantFolder
Restart ComfyUI. You should see:
[GraphConstantFolder] installed on_prompt handler (constant-fold: lazy switches + switch-like nodes)
You can use either environment variables or the local config file.
GRAPH_CONSTANT_FOLDER_ENABLED=1(orGCF_ENABLED=1)GRAPH_CONSTANT_FOLDER_PRUNE=1(orGCF_PRUNE=1)GRAPH_CONSTANT_FOLDER_DEBUG=1(orGCF_DEBUG=1)GRAPH_CONSTANT_FOLDER_VERBOSE=1(orGCF_VERBOSE=1)
Edit graph_constant_folder_config.json next to graph_constant_folder.py:
{
"ENABLED": 1,
"DEBUG": 0,
"VERBOSE": 0,
"PRUNE": 1
}Environment variables override the config.
By default, the extension resolves prompt-time constants through:
- literal values in the prompt JSON
Reroute-like pass-through nodes- constant/primitive/literal nodes (by class name match) only when they have no linked inputs
To extend the constant-source class matcher, set:
GRAPH_CONSTANT_FOLDER_CONST_CLASS_TYPES="(?i)(primitive|constant|literal|impact.*primitive|rgthree.*primitive)"
This does not evaluate boolean logic (AND/OR/compare), so it remains conservative.
[GraphConstantFolder] on_prompt: nodes=203, switch_candidates=23, foldable=11, prune=1, verbose=1
[GraphConstantFolder] rewrote nodes=20, pruned=105, dt_ms=4.36
Prompt executed in 0.06 seconds
Without the extension, the same ~200 node workflow incurs a "got prompt" delay of about 0.5 seconds.