项目作者: mtrempoltsev

项目描述 :
Fast and simple C++ serializer
高级语言: C++
项目地址: git://github.com/mtrempoltsev/pods.git
创建时间: 2018-01-11T17:58:13Z
项目社区:https://github.com/mtrempoltsev/pods

开源协议:MIT License

下载


Build Status
Build status
Coverage Status

Plain Old Data Serializer (PODS)

Feature List

  • header only
  • high performance (perhaps binary serialization is the fastest on the Earth)
  • optional values
  • supported archive formats:
    • JSON
    • MsgPack
  • serialization from/to:
    • memory buffer
    • resizable memory buffer
    • standard C++ streams

Benchmarks

Comparison with memcpy

MacBook Pro, 2017
3.5 GHz Intel Core i7
16 Gb 2133 MHz LPDDR3

  1. data size: 11520
  2. serialized data size: 11632
  3. memcpy
  4. total: 27465 Mb
  5. total time: 2396.19 ms
  6. speed: 11461.9 Mb/s
  7. serialization
  8. total: 27732 Mb
  9. total time: 3502.93 ms
  10. speed: 7916.8 Mb/s
  11. deserialization
  12. total: 27732 Mb
  13. total time: 3200.15 ms
  14. speed: 8665.83 Mb/s

Comparison with other libraries

  1. performing 100000 iterations
  2. thrift-binary: version = 0.10.0
  3. thrift-binary: size = 17017 bytes
  4. thrift-binary: time = 2259 milliseconds
  5. protobuf: version = 3001000
  6. protobuf: size = 16116 bytes
  7. protobuf: time = 2797 milliseconds
  8. capnproto: version = 6001
  9. capnproto: size = 17768 bytes
  10. capnproto: time = 486 milliseconds
  11. boost: version = 106200
  12. boost: size = 17470 bytes
  13. boost: time = 1365 milliseconds
  14. msgpack: version = 2.1.3
  15. msgpack: size = 13402 bytes
  16. msgpack: time = 3638 milliseconds
  17. cereal: size = 17416 bytes
  18. cereal: time = 1034 milliseconds
  19. avro: size = 16384 bytes
  20. avro: time = 4780 milliseconds
  21. flatbuffers: size = 17632 bytes
  22. flatbuffers: time = 433 milliseconds
  23. yas: version = 6.0.2
  24. yas: size = 17416 bytes
  25. yas: time = 317 milliseconds
  26. pods: size = 17012 bytes
  27. pods: time = 235 milliseconds

Using PODS

MsgPack serialization

  1. #include <iostream>
  2. #include <pods/pods.h>
  3. #include <pods/msgpack.h>
  4. #include <pods/buffers.h>
  5. // just a struct for serialization
  6. struct Server
  7. {
  8. std::string address; // no default value
  9. uint16_t port = 8080; // default value
  10. PODS_SERIALIZABLE(
  11. PODS_MDR(address), // mandatory field
  12. PODS_OPT(port)) // optional field
  13. };
  14. int main(int /*argc*/, char** /*argv*/)
  15. {
  16. const Server original;
  17. pods::ResizableOutputBuffer out;
  18. pods::MsgPackSerializer<decltype(out)> serializer(out);
  19. if (serializer.save(original) != pods::Error::NoError)
  20. {
  21. std::cerr << "serialization error\n";
  22. return EXIT_FAILURE;
  23. }
  24. Server loaded = {};
  25. pods::InputBuffer in(out.data(), out.size());
  26. pods::MsgPackDeserializer<decltype(in)> deserializer(in);
  27. if (deserializer.load(loaded) != pods::Error::NoError)
  28. {
  29. std::cerr << "deserialization error\n";
  30. return EXIT_FAILURE;
  31. }
  32. if (original.address != loaded.address
  33. || original.port != loaded.port)
  34. {
  35. std::cerr << "corrupted archive\n";
  36. return EXIT_FAILURE;
  37. }
  38. return EXIT_SUCCESS;
  39. }

JSON serialization

  1. #include <iostream>
  2. #include <pods/pods.h>
  3. #include <pods/json.h>
  4. #include <pods/buffers.h>
  5. // just a struct for serialization
  6. struct Server
  7. {
  8. std::string address; // no default value
  9. uint16_t port = 8080; // default value
  10. PODS_SERIALIZABLE(
  11. PODS_MDR(address), // mandatory field
  12. PODS_OPT(port)) // optional field
  13. };
  14. struct ServerList
  15. {
  16. std::vector<Server> servers =
  17. {
  18. // this is default values
  19. Server { "localhost", 8080 },
  20. Server { "my.com", 2018 }
  21. };
  22. PODS_SERIALIZABLE(
  23. PODS_MDR(servers))
  24. };
  25. int main(int /*argc*/, char** /*argv*/)
  26. {
  27. const ServerList original;
  28. pods::ResizableOutputBuffer out;
  29. pods::PrettyJsonSerializer<decltype(out)> serializer(out);
  30. if (serializer.save(original) != pods::Error::NoError)
  31. {
  32. std::cerr << "serialization error\n";
  33. return EXIT_FAILURE;
  34. }
  35. ServerList loaded;
  36. loaded.servers.clear();
  37. pods::InputBuffer in(out.data(), out.size());
  38. pods::JsonDeserializer<decltype(in)> deserializer(in);
  39. if (deserializer.load(loaded) != pods::Error::NoError)
  40. {
  41. std::cerr << "deserialization error\n";
  42. return EXIT_FAILURE;
  43. }
  44. const std::string json(out.data(), out.size());
  45. std::cout << json << '\n';
  46. return EXIT_SUCCESS;
  47. }
Output:
  1. {
  2. "servers": [
  3. {
  4. "address": "localhost",
  5. "port": 8080
  6. },
  7. {
  8. "address": "my.com",
  9. "port": 2018
  10. }
  11. ]
  12. }

Standard streams

  1. #include <iostream>
  2. #include <sstream>
  3. #include <pods/pods.h>
  4. #include <pods/msgpack.h>
  5. #include <pods/streams.h>
  6. // just a struct for serialization
  7. struct Server
  8. {
  9. std::string address; // no default value
  10. uint16_t port = 8080; // default value
  11. PODS_SERIALIZABLE(
  12. PODS_MDR(address), // mandatory field
  13. PODS_OPT(port)) // optional field
  14. };
  15. int main(int /*argc*/, char** /*argv*/)
  16. {
  17. const Server original;
  18. std::stringstream buffer;
  19. pods::OutputStream out(buffer);
  20. pods::MsgPackSerializer<decltype(out)> serializer(out);
  21. if (serializer.save(original) != pods::Error::NoError)
  22. {
  23. std::cerr << "serialization error\n";
  24. return EXIT_FAILURE;
  25. }
  26. Server loaded = {};
  27. pods::InputStream in(buffer);
  28. pods::MsgPackDeserializer<decltype(in)> deserializer(in);
  29. if (deserializer.load(loaded) != pods::Error::NoError)
  30. {
  31. std::cerr << "deserialization error\n";
  32. return EXIT_FAILURE;
  33. }
  34. return EXIT_SUCCESS;
  35. }