Skip to content

Commit

Permalink
feat: use IS_CHANGED as cache key if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
JettHu committed Mar 18, 2024
1 parent 46956f4 commit 5bf2949
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions comfy/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
import torch
import math
import struct
Expand All @@ -7,7 +8,7 @@
from PIL import Image
import logging

from cachetools import LRUCache, cachedmethod
from cachetools import LRUCache, cachedmethod, keys
from threading import Lock


Expand Down Expand Up @@ -523,6 +524,15 @@ def find_attr(name: str):
fn_name = find_attr(ENTRY_POINT_METHOD)
fn = find_attr(fn_name)

key_fn = keys.methodkey
with suppress(Exception):
is_change_fn = find_attr("IS_CHANGED").__func__

def kf(self, *args, **kwargs):
return keys.hashkey(is_change_fn(self, *args, **kwargs))

key_fn = kf

from comfy.cli_args import args

maxsize = args.node_cache_config.get(name, DEFAULT_CACHE_SIZE)
Expand All @@ -532,6 +542,8 @@ def find_attr(name: str):
# Set entry-point method
dct[ENTRY_POINT_METHOD] = fn_name
# Add cache decorator to entry-point method
dct[fn_name] = cachedmethod(lambda self: self.__node_cache__, lock=lambda self: self.__node_cache_lock__)(fn)
dct[fn_name] = cachedmethod(
lambda self: self.__node_cache__, key=key_fn, lock=lambda self: self.__node_cache_lock__
)(fn)
logging.debug(f"decorator <class {name}> <FUNCTION {fn_name}> with cache<size: {maxsize}>")
return super().__new__(cls, name, bases, dct)

0 comments on commit 5bf2949

Please sign in to comment.