Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move bsearch function to C-code #2945

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Commits on Feb 29, 2024

  1. refactor: move bsearch function to C-code

    This commit fixes issue jqlang#527 and move the bsearch function to a native
    C-code.
    
    The performance is a bit better:
    
    Testing script:
    ```bash
    clear
    if [[ `uname` == Darwin ]]; then
        MAX_MEMORY_UNITS=KB
    else
        MAX_MEMORY_UNITS=MB
    fi
    
    export TIMEFMT='%J   %U  user %S system %P cpu %*E total'$'\n'\
    'avg shared (code):         %X KB'$'\n'\
    'avg unshared (data/stack): %D KB'$'\n'\
    'total (sum):               %K KB'$'\n'\
    'max memory:                %M '$MAX_MEMORY_UNITS''$'\n'\
    'page faults from disk:     %F'$'\n'\
    'other page faults:         %R'
    
    echo "JQ code bsearch"
    time /usr/bin/jq -n '[range(30000000)] | bsearch(3000)'
    
    echo "C code bsearch"
    time ./jq -n '[range(30000000)] | bsearch(3000)'
    ````
    
    Results:
    
    ```
    JQ code bsearch
    3000
    /usr/bin/jq -n '[range(30000000)] | bsearch(3000)'   8.63s  user 0.77s system 98% cpu 9.542 total
    avg shared (code):         0 KB
    avg unshared (data/stack): 0 KB
    total (sum):               0 KB
    max memory:                823 MB
    page faults from disk:     1
    other page faults:         432828
    C code bsearch
    3000
    ./jq -n '[range(30000000)] | bsearch(3000)'   8.44s  user 0.74s system 99% cpu 9.249 total
    avg shared (code):         0 KB
    avg unshared (data/stack): 0 KB
    total (sum):               0 KB
    max memory:                824 MB
    page faults from disk:     0
    other page faults:         432766
    ```
    
    The results may be better if we can use jvp_array_read, and there is no
    need to copy/free the input array in each iteration. I guess that is
    like that for API pourposes when the libjq is in use with multiple
    threads in place.
    
    Signed-off-by: Eloy Coto <[email protected]>
    eloycoto committed Feb 29, 2024
    Configuration menu
    Copy the full SHA
    81a5b64 View commit details
    Browse the repository at this point in the history
  2. style: fixes from suggestions.

    Co-authored-by: itchyny <[email protected]>
    Signed-off-by: Eloy Coto <[email protected]>
    eloycoto and itchyny committed Feb 29, 2024
    Configuration menu
    Copy the full SHA
    097fc63 View commit details
    Browse the repository at this point in the history
  3. fix: bsearch prevent overflow on mid calculation

    Signed-off-by: Eloy Coto <[email protected]>
    eloycoto committed Feb 29, 2024
    Configuration menu
    Copy the full SHA
    5c0aaba View commit details
    Browse the repository at this point in the history