onelson / jq-rs

Rust crate to provide programmatic access to `jq`.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] howto reuse jqProgram

hansbogert opened this issue · comments

I'd probably need to reuse the jqprogram, not having invested a lot of time in rust and in particular this library, I'm really not sure how you would do the following.

If you have a need to run the same jq program multiple times it is highly recommended to retain a pre-compiled JqProgram and reuse it.

/update
To clarify, I'd like to reuse the jqProgram across program runs, so I'd probably have to embed the jqProgram somehow or load from disk (?)

So, the quoted text is referring to using jq_rs::compile() which initializes a jq program which can be run against different inputs over time. This won't really help you across different program runs - you'll always have to pay the cost of compiling the jq program at least once per run.

I should add an example of this type of usage.

In the meantime, I hope this helps explain how you'd use it...

fn main() {
  // compile the jq program once
  let mut prog = jq_rs::compile(".name").unwrap();
  // run the program over some json...
  assert_eq!("\"alice\"", prog.run(r#"{"name": "alice"}"#).unwrap());
  // run the program over some other json...
  assert_eq!("\"bob\"", prog.run(r#"{"name": "bob"}"#).unwrap());
}

The reason reusing an already compiled jq program is attractive is because this compilation step is quite slow - or at least it's slow at the v1.6 tag for jq. Apparently there was a big slowdown between v1.5 and v1.6, and some steps were later taken to mitigate this in jq master. We'll reap the benefit here whenever they decide to tag v1.7 (but I don't have a sense of when that might be).

Thank you for your elaborate answer(s). Just to be really certain, there is no way to serialize (for lack of a better word) the jqProgram and reload it in consecutive invocations of program runs, i.e., libjq does not provide this functionality?

Just to be really certain, there is no way to serialize (for lack of a better word) the jqProgram and reload it in consecutive invocations of program runs, i.e., libjq does not provide this functionality?

That's correct, as far as I know.