Giter Site home page Giter Site logo

Comments (3)

jtv avatar jtv commented on June 6, 2024

I'll assume that getprimes() produces a bunch of rows, one column wide, holding an integer. I don't really see why you're going to the trouble of using a prepared query for this; just txn.exec() would be easier than preparing the query first and then calling txn.exec_prepared(). The simpler way can even be faster, under some circumstances.

The iterators you're passing there are iterators of rows, and you probably don't want to store those in your vector. What you want is the actual numbers that are inside them.

Is the loop per se really the problem that you're worried about? Are you trying to avoid the loop because you don't like having the loop in your code, or is your real concern the inefficiency of reallocating the vector's memory as it grows?

If it's the loop per se that you want to avoid (e.g. because you just don't like loops), then we can use a functional callback style with pqxx::result::for_each().

If you're worried about the repeated memory allocations, you can call std::vector::reserve() at the beginning of the loop. That way you allocate the vector's memory just once. But apply Knuth's Law β€” see whether this really is an issue, because std::vector was designed to minimise this overhead.

So here's a sketch with those two changes:

std::vector<int> Primes;
pqxx::result const r(txn.exec_prepared("getprimes"));
// Allocate all memory at once, now that we know how many primes we're getting.
Primes.reserve(r.size());
// Store query results in vector.  This converts each row to an arguments list
// (in this case just the one field) and calls the lambda we pass.
r.for_each([&Primes](int prime) { Primes.push_back(prime); });

However if you're retrieving a lot of results, this may actually be faster using a streaming query. Those don't use pqxx::result, which has the advantages that it can start delivering results before it's done executing the query, and that it tends to be faster overall for large result sets. It has the disadvantage that you won't know beforehand how many rows you're going to get.

Luckily std::vector::reserve() works just fine even if you don't get the exact right number. You can just use a reasonable guess. For example, you can "reserve" a number that you think is a reasonable allocation to make, even if it's more memory than you actually need. In that case you're still doing only one allocation, just an oversized one. Or if it's too small, you'll do multiple allocations, but not as many as before:

std::vector<int> Primes;
// Just an acceptable guess at how many primes we'll get.
Primes.reserve(10'000'000);
// Execute "select getprimes()", and don't wait for the whole query
// to complete, but start processing rows right away.  Convert each
// to a tuple of, in this case, just a single int.
for (auto [prime] : txn.stream<int>("select getprimes()"))
    Primes.push_back(prime);

from libpqxx.

pkfox avatar pkfox commented on June 6, 2024

Hi Jeroen, to answer your question first
I don't really see why you're going to the trouble of using a prepared query for this
It's because I don't know libpqxx very well and thought there must be a cleaner way , I know there is always a loop of some sort going on but if I can avoid a visible one I will
Thank you for your excellent suggestions, I like the lambda method best but both are nice to know. Thanks again.

from libpqxx.

jtv avatar jtv commented on June 6, 2024

OK, I'll close the ticket then.

from libpqxx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.