Improve rapid generation performance via UI throttling (#7084)

* changes

* add changeset

---------

Co-authored-by: Ali Abid <aliabid@Alis-MacBook-Pro.local>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
aliabid94 2024-01-22 11:53:19 -08:00 committed by GitHub
parent e8b2d8b2f8
commit 94aa271ab1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 3 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---
feat:Improve rapid generation performance via UI throttling

View File

@ -305,6 +305,43 @@
});
}
function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number
): (...funcArgs: Parameters<T>) => void {
let lastFunc: ReturnType<typeof setTimeout>;
let lastRan: number;
let lastThis: any;
let lastArgs: IArguments | null;
return function (this: any, ...args: Parameters<T>) {
if (!lastRan) {
func.apply(this, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastThis = this;
lastArgs = arguments;
lastFunc = setTimeout(
() => {
if (Date.now() - lastRan >= limit) {
if (lastArgs) {
func.apply(lastThis, Array.prototype.slice.call(lastArgs));
}
lastRan = Date.now();
}
},
Math.max(limit - (Date.now() - lastRan), 0)
);
}
};
}
const refresh = throttle(() => {
rootNode = rootNode;
}, 50);
async function handle_update(
data: any,
fn_index: number,
@ -317,7 +354,7 @@
output.props.value_is_output = true;
});
rootNode = rootNode;
refresh();
await tick();
data?.forEach((value: any, i: number) => {
const output = instance_map[outputs[i]];
@ -340,7 +377,7 @@
output.props.value = value;
}
});
rootNode = rootNode;
refresh();
}
let submit_map: Map<number, ReturnType<typeof app.submit>> = new Map();
@ -355,7 +392,7 @@
obj.props = {};
}
obj.props[prop] = val;
rootNode = rootNode;
refresh();
}
let handled_dependencies: number[][] = [];