PERF: Remove alias_dt transform in favor of property on SIDData.

Adding a copy of the Event's dt field as datetime via the
`alias_dt` generator, so that the API was forgiving and allowed
both datetime and dt on a SIDData object, was creating noticeable
overhead, even on an noop algorithms.

Instead of incurring the cost of copying the datetime value and
assigning it to the Event object on every event that is passed
through the system, add a property to SIDData which acts as an
alias `datetime` to `dt`.

Eventually support for `data['foo'].datetime` may be removed,
and could be considered deprecated.
This commit is contained in:
Eddie Hebert
2014-03-07 10:26:43 -05:00
parent 778da20468
commit a203f69635
5 changed files with 19 additions and 16 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ class RecordDateSlippage(slippage.FixedSlippage):
self.latest_date = None
def simulate(self, event, open_orders):
self.latest_date = event['datetime']
self.latest_date = event.dt
result = super(RecordDateSlippage, self).simulate(event, open_orders)
return result
+1 -3
View File
@@ -51,7 +51,6 @@ from zipline.protocol import Event
from zipline.gens.composites import (
date_sorted_sources,
sequential_transforms,
alias_dt
)
from zipline.gens.tradesimulation import AlgorithmSimulator
@@ -263,10 +262,9 @@ class TradingAlgorithm(object):
with_tnfms = sequential_transforms(date_sorted,
*self.transforms)
with_alias_dt = alias_dt(with_tnfms)
with_benchmarks = date_sorted_sources(benchmark_return_source,
with_alias_dt)
with_tnfms)
# Group together events with the same dt field. This depends on the
# events already being sorted.
-9
View File
@@ -47,12 +47,3 @@ def sequential_transforms(stream_in, *transforms):
stream_in)
return stream_out
def alias_dt(stream_in):
"""
Alias the dt field to datetime on each message.
"""
for message in stream_in:
message['datetime'] = message['dt']
yield message
+15
View File
@@ -117,6 +117,21 @@ class SIDData(object):
if initial_values:
self.__dict__ = initial_values
@property
def datetime(self):
"""
Provides an alias from data['foo'].datetime -> data['foo'].dt
`datetime` was previously provided by adding a seperate `datetime`
member of the SIDData object via a generator that wrapped the incoming
data feed and added the field to each equity event.
This alias is intended to be temporary, to provide backwards
compatibility with existing algorithms, but should be considered
deprecated, and may be removed in the future.
"""
return self.dt
def __getitem__(self, name):
return self.__dict__[name]
+2 -3
View File
@@ -236,7 +236,7 @@ class BatchTransform(object):
Point of entry. Process an event frame.
"""
# extract dates
dts = [event.datetime for event in itervalues(data._data)]
dts = [event.dt for event in itervalues(data._data)]
# we have to provide the event with a dt. This is only for
# checking if the event is outside the window or not so a
# couple of seconds shouldn't matter. We don't add it to
@@ -439,8 +439,7 @@ class BatchTransform(object):
# with CUSTOM data events, there may be different fields
# per sid. So the allowable keys are the union of all events.
union = set.union(*sid_keys)
unwanted_fields = set(['portfolio', 'sid', 'dt', 'type',
'datetime', 'source_id'])
unwanted_fields = set(['portfolio', 'sid', 'dt', 'type', 'source_id'])
return union - unwanted_fields
def _get_field_names(self, event):