Boost logo

Boost Users :

Subject: [Boost-users] [msm] Why does it take so many bytes to represent a state machine?
From: 谢任中 (joshchia_at_[hidden])
Date: 2012-11-13 15:11:10


Hi,

I'm using msm to store the state of many small objects. Each one is around
24 bytes on its own. However, I just found out from experimentation that a
MSM state machine takes many bytes to store. For the following program, the
print out is:
entering: State1
104
48

This indicates that the very simple one-state, no-transition, machine is
104 bytes, and takes 48 bytes even to serialized. The
front::state_machine_def, however, is only 1 byte, though I suspect thet
real state is stored only in the 104-byte back::state_machine.

Even the SimpleTutorial.cpp example, when I adapt it to print the size,
shows 128 bytes.

Theoretically, all this information take no more than a few bits to store,
and one byte is generous. What's the internal representation of a state
machine and how can I optimize the size of the representation? This matters
for my use case because I have many small objects for which I want to
maintain state, that are only ~20 bytes each. I thought perhaps I could
mitigate this size problem by storing the serialized bytes but even the
serialized bytes are many.

I'm using boost 1.51 and gcc 4.7.2 on 64-bit Linux.

Josh

#include <iostream>
#include <strstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/state_machine_def.hpp>

using namespace std;

namespace mpl = boost::mpl;
namespace msm = boost::msm;
using namespace msm::front;

struct Event {
};

struct state_ : public msm::front::state_machine_def<state_> {
    struct State1 : public msm::front::state<> {
        // optional entry/exit methods
        template <class Event,class FSM>
        void on_entry(Event const&, FSM& f) { std::cout << "entering:
State1" << std::endl; }
        template <class Event,class FSM>
        void on_exit(Event const&,FSM& ) { std::cout << "leaving: State1"
<< std::endl; }
    };
    typedef State1 initial_state;
    struct transition_table : mpl::vector<
> {};
};

typedef msm::back::state_machine<state_> State;

int main() {
    State state;
    state.start();
    cout << sizeof(state) << endl;
    ostrstream os;
    {
        boost::archive::text_oarchive oa(os);
        oa << state;
    }
    cout << os.pcount() << endl;
    return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net