pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.52k stars 525 forks source link

Memory optimized FSM #379

Closed ajay2611 closed 4 years ago

ajay2611 commented 4 years ago

I have got a very simple class as below.

class Test:
    states = ['A', 'B', 'C']
    transitions = [['change',  'A',  'B']]

    def __init__(self, tid):
        self.tid = tid
        self.machine = Machine(model=self, states=Test.states, transitions=Test.transitions, initial='A')

Each object of the above class takes ~60KB(my actual class has more states and transitions than the above example) which is too much as I've to create around 3.5L objects. I can't use __slots__ in my Test class as Machine adds extra variables/methods to it.

Any ideas on how to do it in a memory-optimized way?

robinsonkwame commented 4 years ago

Do this help? transitions memory footprint is too large for my Django app and adding models takes too long.

aleneum commented 4 years ago

I am planning to extract the skeleton of core into a MinimalMachine without bells and whistles sooner or later since core has grown quite a lot since the early days. Please refer to the link @robinsonkwame has provided. The referenced discussion in the FAQ also mentions how to use Machine callbacks directly without the need of adding models to it (and thus creating the overhead).

Closing this due to inactivity. Feel free to comment anyways. I will reopen the issue if necessary.