Flatbuffers and bit flags

1,586 views
Skip to first unread message

Alastair Murray

unread,
Jul 4, 2014, 1:11:58 PM7/4/14
to flatb...@googlegroups.com
It's quite common in games to store a set of properties using bit flags.
e.g.
#define Property1   (1<<0)
#define Property2   (1<<1)
unsigned int mFlags = 0;
mFlags |= Property1 | Property2;
if( mFlags & Property1 ) { ... }
etc.

I often have c++ objects that may have 30 of these kinds of boolean properties that only occupy 4 bytes in memory.
From my tests, the only way this can be done in Flatbuffers whilst still supporting json is by converting the flags to booleans, and having each one occupy a single byte.
So an object with 30 properties takes up to 30 bytes of storage rather than 4.

I'm finding this to be a severe limitation for my use of Flatbuffers.
Is it possible to add this kind of functionality to support bit flags? Or alternatively to allow booleans to take up 1 bit rather than 1 byte?
Or are there any other usage suggestions that could help in this scenario?


Wouter van Oortmerssen

unread,
Jul 7, 2014, 1:06:19 PM7/7/14
to Alastair Murray, flatb...@googlegroups.com
The best way to do that currently is to use an enum:

enum MyType : int { A = 1, B = 2, C = 4, D = 8 }

etc. Now, having to write power of two ints manually is a pain, but at least this will be efficient for now.

One feature I could consider to add is to allow the "enum" keyword to be written as "flags" instead, and it then doing (1 << N) on the constant for you.




--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alastair Murray

unread,
Jul 9, 2014, 9:13:07 PM7/9/14
to flatb...@googlegroups.com
The main problem with this approach that I can see is that it doesn't handle multiple flags very nicely in JSON.
For example, if the value of MyType was (A | C) the JSON output is MyType : 5 which is not very human readable.

If Flatbuffers handled the ORs and enum was allowed to be specified as flags, then this might be a workable system, although I feel it might get messy.

For example, when ideal JSON might look like:
{
  Property1: true,
  ...
  Property32: true,
}

What we'd have to use would be:
{
  Flags : Property1 | Property32
}

When the number of properties is large, this would become messy. Plus flags cannot be set to off - instead they have to be removed completely.


Wouter van Oortmerssen

unread,
Jul 10, 2014, 12:37:32 PM7/10/14
to Alastair Murray, flatb...@googlegroups.com
There is a bigger problem that it doesn't handle enums symbolically at all in JSON, never mind flags.

I am not sure what the right solution there is, as I could actually output and parse symbolic names (or strings), but then this would produce JSON data that is not interpretable by other JSON readers. At least the current integers are "portable".

Would love to hear other opinions.

Wouter van Oortmerssen

unread,
Jul 21, 2014, 8:02:50 PM7/21/14
to flatb...@googlegroups.com
For what it's worth, there is now a bit_flags feature to conveniently create a flags enum (see schema documentation). This doesn't address all of your concerns, but its a start.
Reply all
Reply to author
Forward
0 new messages