Skip to content

Commit 3364e7c

Browse files
committed
perf(langgraph): improve performance of checking for last superstep
1 parent a527fc7 commit 3364e7c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

.changeset/yellow-crabs-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/langgraph": patch
3+
---
4+
5+
Fix performance regression due to deferred nodes

libs/langgraph/src/pregel/algo.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ export const increment = (current?: number) => {
9292
return current !== undefined ? current + 1 : 1;
9393
};
9494

95+
function triggersNextStep(
96+
updatedChannels: Set<string>,
97+
triggerToNodes: Record<string, string[]> | undefined
98+
) {
99+
if (triggerToNodes == null) return false;
100+
101+
for (const chan of updatedChannels) {
102+
if (triggerToNodes[chan]) return true;
103+
}
104+
105+
return false;
106+
}
107+
95108
// Avoids unnecessary double iteration
96109
function maxChannelMapVersion(
97110
channelVersions: Record<string, number | string>
@@ -348,6 +361,7 @@ export function _applyWrites<Cc extends Record<string, BaseChannel>>(
348361
const channel = onlyChannels[chan];
349362
if (channel.isAvailable() && !updatedChannels.has(chan)) {
350363
const updated = channel.update([]);
364+
351365
if (updated && getNextVersion !== undefined) {
352366
checkpoint.channel_versions[chan] = getNextVersion(maxVersion);
353367

@@ -359,12 +373,7 @@ export function _applyWrites<Cc extends Record<string, BaseChannel>>(
359373
}
360374

361375
// If this is (tentatively) the last superstep, notify all channels of finish
362-
if (
363-
bumpStep &&
364-
!Object.keys(triggerToNodes ?? {}).some((channel) =>
365-
updatedChannels.has(channel)
366-
)
367-
) {
376+
if (bumpStep && !triggersNextStep(updatedChannels, triggerToNodes)) {
368377
for (const chan in onlyChannels) {
369378
if (!Object.prototype.hasOwnProperty.call(onlyChannels, chan)) continue;
370379

0 commit comments

Comments
 (0)