perf(langgraph): improve performance of checking for last superstep (#1625)

This commit is contained in:
David Duong
2025-09-09 17:11:45 +02:00
committed by GitHub
parent a527fc7aeb
commit c6f75b6ace
2 changed files with 20 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@langchain/langgraph": patch
---
Fix performance regression due to deferred nodes
+15 -6
View File
@@ -92,6 +92,19 @@ export const increment = (current?: number) => {
return current !== undefined ? current + 1 : 1;
};
function triggersNextStep(
updatedChannels: Set<string>,
triggerToNodes: Record<string, string[]> | undefined
) {
if (triggerToNodes == null) return false;
for (const chan of updatedChannels) {
if (triggerToNodes[chan]) return true;
}
return false;
}
// Avoids unnecessary double iteration
function maxChannelMapVersion(
channelVersions: Record<string, number | string>
@@ -348,6 +361,7 @@ export function _applyWrites<Cc extends Record<string, BaseChannel>>(
const channel = onlyChannels[chan];
if (channel.isAvailable() && !updatedChannels.has(chan)) {
const updated = channel.update([]);
if (updated && getNextVersion !== undefined) {
checkpoint.channel_versions[chan] = getNextVersion(maxVersion);
@@ -359,12 +373,7 @@ export function _applyWrites<Cc extends Record<string, BaseChannel>>(
}
// If this is (tentatively) the last superstep, notify all channels of finish
if (
bumpStep &&
!Object.keys(triggerToNodes ?? {}).some((channel) =>
updatedChannels.has(channel)
)
) {
if (bumpStep && !triggersNextStep(updatedChannels, triggerToNodes)) {
for (const chan in onlyChannels) {
if (!Object.prototype.hasOwnProperty.call(onlyChannels, chan)) continue;