Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #1150 from quantopian/vegas-baby
Browse files Browse the repository at this point in the history
Use __slots__ in Order object to save memory
  • Loading branch information
jbredeche committed Apr 21, 2016
2 parents 842c47b + a1f19dc commit 2179553
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions zipline/finance/order.py
Expand Up @@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import copy
import math
import uuid

Expand All @@ -37,6 +36,13 @@


class Order(object):
# using __slots__ to save on memory usage. Simulations can create many
# Order objects and we keep them all in memory, so it's worthwhile trying
# to cut down on the memory footprint of this object.
__slots__ = ["id", "dt", "reason", "created", "sid", "amount", "filled",
"commission", "_status", "stop", "limit", "stop_reached",
"limit_reached", "direction", "type"]

def __init__(self, dt, sid, amount, stop=None, limit=None, filled=0,
commission=None, id=None):
"""
Expand Down Expand Up @@ -70,11 +76,11 @@ def make_id(self):
return uuid.uuid4().hex

def to_dict(self):
py = copy(self.__dict__)
for field in ['type', 'direction', '_status']:
del py[field]
py['status'] = self.status
return py
dct = {name: getattr(self, name)
for name in self.__slots__
if name not in {'type', 'direction', '_status'}}
dct['status'] = self.status
return dct

def to_api_obj(self):
pydict = self.to_dict()
Expand Down

0 comments on commit 2179553

Please sign in to comment.