From 136a09776d438754089eeb561f9d3317f53c8c1d Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 16 Jul 2015 15:16:36 -0400 Subject: [PATCH] PERF: Reuse one cursor in asset finder lookups. Instead of creating a new cursor with each query, use the same cursor throughout the lifetime of the finder instance to remove any overhead from creating a new cursor in tight loops. --- zipline/assets/assets.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/zipline/assets/assets.py b/zipline/assets/assets.py index 19985319..da290919 100644 --- a/zipline/assets/assets.py +++ b/zipline/assets/assets.py @@ -150,7 +150,7 @@ class AssetFinder(object): self._asset_type_cache = {} def create_db_tables(self): - c = self.conn.cursor() + c = self.cursor c.execute(""" CREATE TABLE equities( @@ -202,7 +202,7 @@ class AssetFinder(object): except KeyError: pass - c = self.conn.cursor() + c = self.cursor # Python 3 compatibility required forcing to int for sid = 0. t = (int(sid),) query = 'select asset_type from asset_router where sid=:sid' @@ -246,7 +246,7 @@ class AssetFinder(object): except KeyError: pass - c = self.conn.cursor() + c = self.cursor c.row_factory = Row t = (int(sid),) c.execute(EQUITY_BY_SID_QUERY, t) @@ -275,7 +275,7 @@ class AssetFinder(object): except KeyError: pass - c = self.conn.cursor() + c = self.cursor t = (int(sid),) c.row_factory = Row c.execute(FUTURE_BY_SID_QUERY, t) @@ -319,7 +319,7 @@ class AssetFinder(object): if as_of_date is not None: as_of_date = pd.Timestamp(normalize_date(as_of_date)) - c = self.conn.cursor() + c = self.cursor if as_of_date: # If one SID exists for symbol, return that symbol @@ -403,7 +403,7 @@ class AssetFinder(object): except SymbolNotFound: return None else: - c = self.conn.cursor() + c = self.cursor fuzzy = symbol.replace(self.fuzzy_char, '') t = (fuzzy, as_of_date.value, as_of_date.value) query = ("select sid from equities " @@ -459,7 +459,7 @@ class AssetFinder(object): Raised when a future chain could not be found for the given root symbol. """ - c = self.conn.cursor() + c = self.cursor t = {'root_symbol': root_symbol, 'as_of_date': as_of_date.value, 'knowledge_date': knowledge_date.value} @@ -486,7 +486,7 @@ class AssetFinder(object): @property def sids(self): - c = self.conn.cursor() + c = self.cursor query = 'select sid from asset_router' c.execute(query) return [r[0] for r in c.fetchall()] @@ -756,7 +756,7 @@ class AssetFinder(object): except KeyError: fuzzy = None asset = Equity(**entry) - c = self.conn.cursor() + c = self.cursor t = (asset.sid, asset.symbol, asset.asset_name, @@ -783,7 +783,7 @@ class AssetFinder(object): elif asset_type.lower() == 'future': asset = Future(**entry) - c = self.conn.cursor() + c = self.cursor t = (asset.sid, asset.symbol, asset.asset_name, @@ -862,6 +862,7 @@ class AssetFinder(object): self.metadata_cache = {} self.conn = sqlite3.connect(':memory:') + self.cursor = self.conn.cursor() self.create_db_tables() def insert_metadata(self, identifier, **kwargs):