Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a serialization library #45

Open
brendanlong opened this issue Nov 11, 2018 · 4 comments
Open

Use a serialization library #45

brendanlong opened this issue Nov 11, 2018 · 4 comments

Comments

@brendanlong
Copy link
Contributor

Right now we serialize and deserialize very, very manually (reading and writing one byte at a time). We should see if we can replace that code with something like cstruct. This should make the code more readable, and also make it more efficient.

(I also looked into bitstring and faraday, but they seem lower-level and I don't know if we want that? Faraday is probably more efficient..)

@brendanlong
Copy link
Contributor Author

I did some experimentation with Angstrom and my basic parser was an order of magnitude slower than Pgx's current code. I might try a little bit more experimentation with flambda. It's also awkward to write since OCaml doesn't have unsigned types, so we either need to use the integers library, which uses C code and is significantly slower, or pass around uint32's as int64's 🤮

@brendanlong
Copy link
Contributor Author

We should probably look into how PG'OCaml and Postgres_async do serialization and see if we can come up with a base parser/serializer library that they could both theoretically use too.

@brendanlong
Copy link
Contributor Author

Nevermind, it looks like if we add Async overhead into account, Angstrom_async actually reads data more efficiently than Pgx does, which massively swamps the difference in parse speed:

Estimated testing time 14s (14 benchmarks x 1s). Change using '-quota'.
┌─────────────────────────────────────────────────────────┬────────────┬─────────┬──────────┬──────────┬────────────┐
│ Name                                                    │   Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├─────────────────────────────────────────────────────────┼────────────┼─────────┼──────────┼──────────┼────────────┤
│ Authentication_ok.parse (string)                        │   441.74ns │ 281.26w │          │          │     10.68% │
│ Authentication_ok.parse (bigstring)                     │   403.78ns │ 272.24w │          │          │      9.77% │
│ Authentication_ok_int.parse (string)                    │   190.38ns │ 221.11w │          │          │      4.60% │
│ Authentication_ok_int.parse (bigstring)                 │   123.72ns │ 212.07w │          │          │      2.99% │
│ Authentication_ok_int_lift.parse (string)               │   158.35ns │ 155.09w │          │          │      3.83% │
│ Authentication_ok_int_lift.parse (bigstring)            │    93.14ns │ 146.00w │          │          │      2.25% │
│ Authentication_ok_int_lift.parse (unbuffered bigstring) │    96.23ns │ 169.00w │          │          │      2.33% │
│ Authentication_ok_int_lift.parse (async)                │ 3_497.32ns │ 678.75w │    0.35w │    0.35w │     84.59% │
│ Authentication_ok_int64.parse (string)                  │   205.84ns │ 251.00w │          │          │      4.98% │
│ Authentication_ok_int64.parse (bigstring)               │   146.00ns │ 242.00w │          │          │      3.53% │
│ Authentication_ok_int_simple.parse (string)             │   183.80ns │ 211.00w │          │          │      4.45% │
│ Authentication_ok_int_simple.parse (bigstring)          │   122.37ns │ 202.00w │          │          │      2.96% │
│ Pgx                                                     │    23.40ns │  22.00w │          │          │      0.57% │
│ Pgx_async                                               │ 4_134.51ns │ 639.19w │    0.29w │    0.29w │    100.00% │
└─────────────────────────────────────────────────────────┴────────────┴─────────┴──────────┴──────────┴────────────┘

I'll clean this up and put up a basic repo sometime soon.

@brendanlong
Copy link
Contributor Author

I started working on this here: https://github.com/brendanlong/ocaml-postgres-protocol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant