From d1a07cbdc1363bf2f8846fa5c84a81c6e95435d4 Mon Sep 17 00:00:00 2001 From: Muh Muhten Date: Tue, 26 Feb 2019 05:35:59 -0500 Subject: Make tostream much more efficient (`path(.[])` is a streaming `keys`!) --- src/builtin.jq | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index 7916d7d8..f45802f6 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -215,22 +215,9 @@ def fromstream(i): {x: null, e: false} as $init | else setpath(["e"]; $i[0]|length==1) end ; if .e then .x else empty end); def tostream: - {string:true,number:true,boolean:true,null:true} as $leaf_types | - . as $dot | - if $leaf_types[$dot|type] or length==0 then [[],$dot] - else - # We really need a _streaming_ form of `keys`. - # We can use `range` for arrays, but not for objects. - keys_unsorted as $keys | - $keys[-1] as $last| - ((# for each key - $keys[] | . as $key | - $dot[$key] | . as $dot | - # recurse on each key/value - tostream|.[0]|=[$key]+.), - # then add the closing marker - [[$last]]) - end; + path(def r: (.[]?|r), .; r) as $p | + getpath($p) | + reduce path(.[]?) as $q ([$p, .]; [$p+$q]); # Assuming the input array is sorted, bsearch/1 returns -- cgit v1.2.3