Fastest way to Serialize GeometryBase


This is not necessary a pure GH question, but maybe Grasshopper has some tricks to do that.
What I need is to convert a GeometryBase Object to a byte array - for sending over networks, (strings may be also fine).
I have a basic Serialization function (first results from Stackoverflow - using BinaryFormatter)

Now while that works, it is quite slow - 200ms for a mesh sphere with 8k verts… Deserialization on the other hand is very fast.
It seems I can’t use external libraries like protobuffer, because I cant edit the Geometrybase to add the proper tags needed for Serialization to work.

Does GH have some tricks? GH_ObjectWrapper? Or if not, is there a better way than the BinaryFormatter?

I am quite lost with this and would appreciate any help!

edit: the slow serialize code example:

private void RunScript(List<GeometryBase> x, ref object A) {
    result = new List <byte[]>();
    for(int i = 0; i < x.Count; i++) {
    A = result;

  // <Custom additional code> 
  List<byte[]> result;
  public static byte[] ObjectToByteArray(Object obj) {
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    using (var ms = new MemoryStream()) {
      bf.Serialize(ms, obj);
      return ms.ToArray();

How to serialize rhino models?
(David Rutten) #2

GH_Convert.CommonObjectToByteArray() and vice versa. You won’t see a difference in speed though, it also uses the ISerializable approach. But at least it’s easy to call.


thanks! and its compressed, which is nice
Just one more question: is Serialization with external libs even possible when I cant access the GeometryBase class to add tags? In other words - should i stop searching and live with the CommonObjectToByteArray() ?

edit - side note: CommonObjectToByteArray is 3-4 times slower then ObjectToByteArray() because of the compression (times become the same when using GH_Compression.Compress(ObjectToByteArray());

edit2: actually for most real world use cases the processing time is in the low ms range - only meshes take a long time…

(David Rutten) #4

It depends. All GeometryBase objects can be (de)serialized using the OpenNurbs standard (which is exposed via ISerializable) and I heavily recommend using that. However RhinoCommon offers sufficient access to certain types for you to write your own (de)serializer. Meshes are potentially one of these objects, especially if you only care about the simple stuff like vertex locations and faces and normals. However meshes can contain much more complicated data such as N-Gons, curvature data, texture coordinates, double precision vertices, and user strings, not to mention actual custom user data, which you cannot hope to serialize.

Going down this road of custom serialization will at the very least mean a lot of unnecessary work on your part, and quite possibly a bunch of annoying bugs and future compatibility issues.


then I will stick to the BinaryFormatter, also because i need a the data (userdata, etc.)