Skip to content
Myr

improve performance of `unique` and `unique_by` (#3254)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Previously, `unique` and `unique_by` filters are implemented using `group_by` and then `map(.[0])`. This commit re-implements in C, to avoid unnecessary boxing of grouping, and improves the performance.

authored by itchyny · committed by GitHub · parent 8ba03f7 · Browse files · 4 files+45 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+45 added−3 removed

  • other:+45 added−3 removed

4 files+45 added−3 removed

modified:src/builtin.c+20 added
877 unmodified lines
878 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");879 }880}881882static jv f_unique(jq_state *jq, jv input) {883 if (jv_get_kind(input) == JV_KIND_ARRAY) {884 return jv_unique(input, jv_copy(input));885 } else {886 return type_error(input, "cannot be sorted, as it is not an array");887 }888}889890static jv f_unique_by_impl(jq_state *jq, jv input, jv keys) {891 if (jv_get_kind(input) == JV_KIND_ARRAY &&892 jv_get_kind(keys) == JV_KIND_ARRAY &&893 jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {894 return jv_unique(input, keys);895 } else {896 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");897 }898}899900#ifdef HAVE_LIBONIG901static int f_match_name_iter(const UChar* name, const UChar *name_end, int ngroups,902 int *groups, regex_t *reg, void *arg) {903 jv captures = *(jv*)arg;
1025 unmodified lines
1929 CFUNC(f_nan, "nan", 1),1930 CFUNC(f_sort, "sort", 1),1931 CFUNC(f_sort_by_impl, "_sort_by_impl", 2),1932 CFUNC(f_group_by_impl, "_group_by_impl", 2),1933 CFUNC(f_unique, "unique", 1),1934 CFUNC(f_unique_by_impl, "_unique_by_impl", 2),1935 CFUNC(f_bsearch, "bsearch", 2),1936 CFUNC(f_min, "min", 1),1937 CFUNC(f_max, "max", 1),1938 CFUNC(f_min_by_impl, "_min_by_impl", 2),
134 unmodified lines
modified:src/builtin.jq+1 added-2 removed
2 unmodified lines
3def map(f): [.[] | f];4def select(f): if f then . else empty end;5def sort_by(f): _sort_by_impl(map([f]));6def group_by(f): _group_by_impl(map([f]));7def unique: group_by(.) | map(.[0]);8def unique_by(f): group_by(f) | map(.[0]);7def unique_by(f): _unique_by_impl(map([f]));8def max_by(f): _max_by_impl(map([f]));9def min_by(f): _min_by_impl(map([f]));10def add(f): reduce f as $x (null; . + $x);11def add: add(.[]);
241 unmodified lines
modified:src/jv_aux.c+22 added
725 unmodified lines
726 }727 jv_mem_free(entries);728 return ret;729}730731jv jv_unique(jv objects, jv keys) {732 assert(jv_get_kind(objects) == JV_KIND_ARRAY);733 assert(jv_get_kind(keys) == JV_KIND_ARRAY);734 assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys)));735 int n = jv_array_length(jv_copy(objects));736 struct sort_entry* entries = sort_items(objects, keys);737 jv ret = jv_array();738 jv curr_key = jv_invalid();739 for (int i = 0; i < n; i++) {740 if (jv_equal(jv_copy(curr_key), jv_copy(entries[i].key))) {741 jv_free(entries[i].key);742 } else {743 jv_free(curr_key);744 curr_key = entries[i].key;745 ret = jv_array_append(ret, entries[i].object);746 }747 }748 jv_free(curr_key);749 jv_mem_free(entries);750 return ret;751}
modified:src/jv.h+2 added-1 removed
263 unmodified lines
264jv jv_delpaths(jv, jv);265jv jv_keys(jv /*object or array*/);266jv jv_keys_unsorted(jv /*object or array*/);267int jv_cmp(jv, jv);268jv jv_group(jv, jv);268jv jv_sort(jv, jv);269jv jv_group(jv, jv);270jv jv_unique(jv, jv);271272#ifdef __cplusplus273}274#endif
24 unmodified lines