haskellari / postgresql-libpq

Low-level Haskell bindings for libpq
BSD 3-Clause "New" or "Revised" License
20 stars 27 forks source link

Avoid copies when passing binary parameters #22

Closed robx closed 2 years ago

robx commented 2 years ago

Currently, the library copies every input parameter when passing it to libpq via Data.ByteString.useAsCString. This PR avoids those copies for the case of binary parameters, by using Data.ByteString.Unsafe.unsafeUseAsCString.

I claim that this is safe because:

There are a few preparatory commits to factor out duplicate code to withParams, and to avoid a bit of unnecessary work there.

Context is https://github.com/PostgREST/postgrest/issues/2261. We see significant decrease of memory use with this change: https://github.com/PostgREST/postgrest/pull/2349

robx commented 2 years ago

I've split out the first commit in #23, will file follow-up PRs if/as that one gets merged.

robx commented 2 years ago

Apologies for the re-review request, meant to request review for #23.

robx commented 2 years ago

This PR does multiple things at once. Please split.

Did you see #23? Is that what you had in mind?

phadej commented 2 years ago

Please reread https://github.com/haskellari/postgresql-libpq/blob/master/CONTRIBUTING.md

robx commented 2 years ago

Sorry, that's because I was repurposing the branch to file a PR against a fork of this repo. Closing.

phadej commented 2 years ago

fork? interesting...

robx commented 2 years ago

There's no desire to fork, I just want to move forward with this in PostgREST in some way, and the best choice seems to be to build against a (hopefully temporary) fork. Some discussion over here if you're interested: https://github.com/PostgREST/postgrest/issues/2422