From c8b2a9c08386a5b18e5321cb5201d5e196ce0299 Mon Sep 17 00:00:00 2001 From: Brian Sam-Bodden Date: Tue, 30 Sep 2025 12:37:07 -0700 Subject: [PATCH] perf: optimize CLIENT SETINFO calls in SearchIndex.from_existing() Reduces duplicate CLIENT SETINFO calls when creating an index from an existing Redis index by tracking client validation state. When a client is validated in from_existing(), the _client_validated flag is set to prevent redundant validation in the property getter. Addresses part of #358 by minimizing Redis command overhead during index initialization. --- redisvl/index/index.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/redisvl/index/index.py b/redisvl/index/index.py index 3dc1732b..4d7ee9b5 100644 --- a/redisvl/index/index.py +++ b/redisvl/index/index.py @@ -406,7 +406,7 @@ def __init__( self._connection_kwargs = connection_kwargs or {} self._lock = threading.Lock() - self._validated_client = False + self._validated_client = kwargs.pop("_client_validated", False) self._owns_redis_client = redis_client is None if self._owns_redis_client: weakref.finalize(self, self.disconnect) @@ -449,6 +449,8 @@ def from_existing( elif redis_client: # Validate client type and set lib name RedisConnectionFactory.validate_sync_redis(redis_client) + # Mark that client was already validated to avoid duplicate calls + kwargs["_client_validated"] = True if not redis_client: raise ValueError("Must provide either a redis_url or redis_client") @@ -1163,7 +1165,7 @@ def __init__( self._connection_kwargs = connection_kwargs or {} self._lock = asyncio.Lock() - self._validated_client = False + self._validated_client = kwargs.pop("_client_validated", False) self._owns_redis_client = redis_client is None if self._owns_redis_client: weakref.finalize(self, sync_wrapper(self.disconnect)) @@ -1199,6 +1201,8 @@ async def from_existing( elif redis_client: # Validate client type and set lib name await RedisConnectionFactory.validate_async_redis(redis_client) + # Mark that client was already validated to avoid duplicate calls + kwargs["_client_validated"] = True if redis_client is None: raise ValueError(