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

ncplayer ought use direct mode when invoked with -k #1342

Closed
dankamongmen opened this issue Feb 9, 2021 · 17 comments · Fixed by #1347
Closed

ncplayer ought use direct mode when invoked with -k #1342

dankamongmen opened this issue Feb 9, 2021 · 17 comments · Fixed by #1347
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request perf sweet sweet perf
Milestone

Comments

@dankamongmen
Copy link
Owner

A surprising number of people seem to be using ncplayer as a one-shot image display tool (ncls is probably closer to what they want, but that's not obvious). And indeed, I can see this particular use case becoming a benchmark. So let's improve ncplayer for this case.

I'd think the best current invocation to be ncplayer -q -k -t0 file. this doesn't print the frame number/time, exits immediately, and doesn't use the alternate screen.

ncls file takes consistently about 3/4 the time of ncplayer -q -k -t0 file:

[schwarzgerat](1) $ ( for i in `seq 0 3 ` ; do time ./ncls ../data/worldmap.png ; done ) | grep real

real	0m0.053s
user	0m0.050s
sys	0m0.005s

real	0m0.050s
user	0m0.036s
sys	0m0.017s

real	0m0.051s
user	0m0.031s
sys	0m0.021s

real	0m0.048s
user	0m0.036s
sys	0m0.013s
[schwarzgerat](1) $ 

vs

[schwarzgerat](1) $ ( for i in `seq 0 3 ` ; do time ./ncplayer -q -t0 -k ../data/worldmap.png 2> /dev/null ; done ) | grep real  

real	0m0.079s
user	0m0.032s
sys	0m0.016s

real	0m0.081s
user	0m0.033s
sys	0m0.016s

real	0m0.081s
user	0m0.037s
sys	0m0.012s

real	0m0.079s
user	0m0.039s
sys	0m0.008s
[schwarzgerat](1) $ 

also, ncplayer really, really wants to print banners, which surely don't speed up anything.

@dankamongmen dankamongmen added documentation Improvements or additions to documentation enhancement New feature or request perf sweet sweet perf labels Feb 9, 2021
@dankamongmen dankamongmen added this to the 2.3.0 milestone Feb 9, 2021
@dankamongmen dankamongmen self-assigned this Feb 9, 2021
@dankamongmen
Copy link
Owner Author

also, ncplayer -s scale seems to be broken?

@dankamongmen
Copy link
Owner Author

also, ncplayer -s scale seems to be broken?

it was fixed by 898f34f, though perhaps it remains broken for the ASCII case? hrmm.

@dankamongmen
Copy link
Owner Author

also, ncplayer -s scale seems to be broken?

it was fixed by 898f34f, though perhaps it remains broken for the ASCII case? hrmm.

nope, that worked too. not sure what was going on. stop smoking crack, nicholas.

@dankamongmen
Copy link
Owner Author

note that supplying -s scale doesn't cut down the time in any measurable way, despite reducing the output size significantly. so that's not the only difference here. what's costing us those 20 milliseconds?

@dankamongmen
Copy link
Owner Author

well, it's got to go through the entire render cycle etc...heh, according to perf a significant amount of our execution time is all in the dynamic loader. i mean, really:

+    7.29%     6.77%  ncplayer  ld-2.31.so                  [.] do_lookup_x                                                                                                                                                                                                                                                                           
+    4.08%     0.00%  ncplayer  [unknown]                   [.] 0x4c56415741e58948                                                                                                                                                                                                                                                                    
+    4.08%     0.00%  ncplayer  ld-2.31.so                  [.] _dl_sysdep_start                                                                                                                                                                                                                                                                      
+    4.08%     0.00%  ncplayer  ld-2.31.so                  [.] dl_main                                                                                                                                                                                                                                                                               
+    4.03%     0.00%  ncplayer  [unknown]                   [k] 0000000000000000                                                                                                                                                                                                                                                                      
+    3.37%     0.00%  ncplayer  [unknown]                   [.] 0x0000002efedf02df                                                                                                                                                                                                                                                                    
+    3.19%     0.00%  ncplayer  [unknown]                   [.] 0x0000ff0d00001fa1                                                                                                                                                                                                                                                                    
+    2.84%     2.84%  ncplayer  libm-2.31.so                [.] __cos_fma                                                                                                                                                                                                                                                                             
+    2.79%     1.35%  ncplayer  ld-2.31.so                  [.] _dl_relocate_object                                                                                                                                                                                                                                                                   
+    2.41%     2.41%  ncplayer  libm-2.31.so                [.] __sin_fma                                                                                                                                                                                                                                                                             
+    2.02%     1.83%  ncplayer  ld-2.31.so                  [.] _dl_lookup_symbol_x                                                                                                                                                                                                                                                                   
+    1.81%     0.18%  ncplayer  [kernel.vmlinux]            [k] entry_SYSCALL_64_after_hwframe                                                                                                                                                                                                                                                        
+    1.63%     0.00%  ncplayer  [kernel.vmlinux]            [k] do_syscall_64                                                                                                                                                                                                                                                                         
+    1.61%     0.00%  ncplayer  ld-2.31.so                  [.] _dl_map_object                                                                                                                                                                                                                                                                        
+    1.39%     1.39%  ncplayer  ld-2.31.so                  [.] strcmp                                                                                                                                                                                                                                                                                
     1.34%     1.34%  ncplayer  [kernel.vmlinux]            [k] filemap_map_pages                                                                                                                                                                                                                                                                     
+    1.22%     0.00%  ncplayer  ld-2.31.so                  [.] mmap64                                                                                                                                                                                                                                                                                
+    1.22%     0.00%  ncplayer  [unknown]                   [.] 0xfdf5fad106f324fa                                                                                                                                                                                                                                                                    
+    1.20%     0.00%  ncplayer  [unknown]                   [k] 0x000000010008c53a                                                                                                                                                                                                                                                                    
+    1.20%     0.00%  ncplayer  libc-2.31.so                [.] __memset_avx2_unaligned_erms                                                                                                                                                                                                                                                          
+    1.18%     1.18%  ncplayer  libz.so.1.2.11              [.] adler32_z                                                                                                                                                                                                                                                                             
+    1.17%     1.17%  ncplayer  libopenmpt.so.0.1.1         [.] 0x0000000000124c0e                                                                                                                                                                                                                                                                    
+    1.17%     0.00%  ncplayer  libopenmpt.so.0.1.1         [.] 0x00007f5e16235c0e                                                                                                                                                                                                                                                                    
+    1.13%     1.13%  ncplayer  libopenmpt.so.0.1.1         [.] 0x0000000000124bff                                                                                                                                                                                                                                                                    
+    1.13%     0.00%  ncplayer  libopenmpt.so.0.1.1         [.] 0x00007f5e16235bff                                                                                                                                                                                                                                                                    
+    0.79%     0.79%  ncplayer  [kernel.vmlinux]            [k] n_tty_write                                                                                                                                                                                                                                                                           
+    0.79%     0.00%  ncplayer  libc-2.31.so                [.] __GI___libc_write                                                                                                                                                                                                                                                                     
+    0.79%     0.00%  ncplayer  [kernel.vmlinux]            [k] ksys_write                                                                                                                                                                                                                                                                            
+    0.79%     0.00%  ncplayer  [kernel.vmlinux]            [k] vfs_write                                                                                                                                                                                                                                                                             
+    0.79%     0.00%  ncplayer  [kernel.vmlinux]            [k] new_sync_write                                                                                                                                                                                                                                                                        
+    0.79%     0.00%  ncplayer  [kernel.vmlinux]            [k] file_tty_write.constprop.0                                                                                                                                                                                                                                                            
     0.78%     0.78%  ncplayer  libz.so.1.2.11              [.] 0x000000000000a498                                                                                                                                                                                                                                                                    
+    0.78%     0.00%  ncplayer  libz.so.1.2.11              [.] 0x00007f5e191e8498                                                                                                                                                                                                                                                                    
+    0.77%     0.77%  ncplayer  libopenmpt.so.0.1.1         [.] 0x0000000000124c13                                                                                                                                                                                                                                                                    
+    0.77%     0.00%  ncplayer  libopenmpt.so.0.1.1         [.] 0x00007f5e16235c13                                                                                                                                                                                                                                                                    
+    0.77%     0.00%  ncplayer  [unknown]                   [.] 0x000055a3be1700d8                                                                                                                                                                                                                                                                    
     0.76%     0.76%  ncplayer  libz.so.1.2.11              [.] 0x0000000000009e64                                                                                                                                                                                                                                                                    
+    0.76%     0.00%  ncplayer  libz.so.1.2.11              [.] 0x00007f5e191e7e64                                                                                                                                                                                                                                                                    
     0.76%     0.76%  ncplayer  libopenmpt.so.0.1.1         [.] 0x0000000000124c18                                                                                                                                                                                                                                                                    
+    0.76%     0.00%  ncplayer  libopenmpt.so.0.1.1         [.] 0x00007f5e16235c18                                                                                                                                                                                                                                                                    
     0.57%     0.57%  ncplayer  [kernel.vmlinux]            [k] clear_page_rep                                                                                                                                                                                                                                                                        
     0.52%     0.52%  ncplayer  [kernel.vmlinux]            [k] handle_mm_fault                                                                                                                                                                                                                                                                       
     0.51%     0.35%  ncplayer  [kernel.vmlinux]            [k] kfree                                                                                                                                                                                                                                                                                 
+    0.51%     0.00%  ncplayer  [kernel.vmlinux]            [k] __x64_sys_exit_group                                                                                                                                                                                                                                                                  
+    0.51%     0.00%  ncplayer  [kernel.vmlinux]            [k] do_group_exit                                                                                                                                                                                                                                                                         
+    0.51%     0.00%  ncplayer  [kernel.vmlinux]            [k] do_exit                                                                                                                                                                                                                                                                               
+    0.51%     0.00%  ncplayer  [kernel.vmlinux]            [k] mmput                                                                                                                                                                                                                                                                                 
+    0.51%     0.00%  ncplayer  [kernel.vmlinux]            [k] exit_mmap                       

jesus, even worse for ncls:

+    6.69%     6.36%  ncls  ld-2.31.so                  [.] do_lookup_x        ◆
+    5.47%     0.00%  ncls  [unknown]                   [k] 0000000000000000   
+    4.77%     0.00%  ncls  [unknown]                   [.] 0x4c56415741e58948 
+    4.77%     0.00%  ncls  ld-2.31.so                  [.] _dl_sysdep_start   
+    4.77%     0.00%  ncls  ld-2.31.so                  [.] dl_main            
+    3.78%     3.78%  ncls  libm-2.31.so                [.] __sin_fma          
+    3.43%     0.00%  ncls  [unknown]                   [.] 0x0000002efedf02df 
+    2.93%     1.14%  ncls  ld-2.31.so                  [.] _dl_relocate_object
+    2.45%     0.00%  ncls  [unknown]                   [.] 0x0000ff0d00001fa1 
+    1.83%     0.00%  ncls  [kernel.vmlinux]            [k] entry_SYSCALL_64_af
+    1.83%     0.00%  ncls  [kernel.vmlinux]            [k] do_syscall_64      
+    1.68%     1.68%  ncls  libm-2.31.so                [.] __cos_fma          
+    1.67%     0.00%  ncls  [unknown]                   [.] 0xfdf5fad106f324fa 
+    1.53%     0.66%  ncls  [kernel.vmlinux]            [k] handle_mm_fault    
+    1.34%     1.16%  ncls  ld-2.31.so                  [.] strcmp             
+    1.32%     1.32%  ncls  ld-2.31.so                  [.] _dl_lookup_symbol_x
+    1.21%     1.21%  ncls  [kernel.vmlinux]            [k] clear_page_rep     
+    1.19%     0.00%  ncls  ld-2.31.so                  [.] mmap64             
+    0.95%     0.95%  ncls  [kernel.vmlinux]            [k] copy_page          
+    0.87%     0.00%  ncls  ld-2.31.so                  [.] _dl_map_object     
+    0.87%     0.00%  ncls  [kernel.vmlinux]            [k] asm_exc_page_fault 
+    0.87%     0.00%  ncls  [kernel.vmlinux]            [k] exc_page_fault     
+    0.87%     0.00%  ncls  [kernel.vmlinux]            [k] do_user_addr_fault 
     0.80%     0.80%  ncls  libopenmpt.so.0.1.1         [.] 0x0000000000124c09 
+    0.80%     0.00%  ncls  libopenmpt.so.0.1.1         [.] 0x00007f51e63edc09 
     0.80%     0.80%  ncls  libopenmpt.so.0.1.1         [.] 0x0000000000124c18 
+    0.80%     0.00%  ncls  libopenmpt.so.0.1.1         [.] 0x00007f51e63edc18 
+    0.69%     0.00%  ncls  [kernel.vmlinux]            [k] __alloc_pages_nodem
+    0.69%     0.00%  ncls  [kernel.vmlinux]            [k] get_page_from_freel
+    0.69%     0.00%  ncls  [kernel.vmlinux]            [k] kernel_init_free_pa
     0.58%     0.25%  ncls  libc-2.31.so                [.] __memmove_avx_unali
+    0.57%     0.00%  ncls  [kernel.vmlinux]            [k] alloc_pages_vma    
     0.54%     0.54%  ncls  [kernel.vmlinux]            [k] memset           

is that all libav's fault? can we do something about that?

@dankamongmen
Copy link
Owner Author

commenting out the entire display sequence (i.e. identifying the stream codec, requiring some i/o and decoding) has us taking 40ms pretty reliably:

[schwarzgerat](0) $ for i in `seq 1 3` ; do time ./ncplayer -q -t0 ../data/worldmap.png ; done

real	0m0.037s
user	0m0.025s
sys	0m0.012s

real	0m0.042s
user	0m0.027s
sys	0m0.015s

real	0m0.040s
user	0m0.024s
sys	0m0.016s
[schwarzgerat](0) $

so half our time is in that, and half our time is in the display.

@dankamongmen
Copy link
Owner Author

probably the fastest solution is to use direct mode when -k is provided, honestly. that would also eliminate the annoying "feature" of always being blitted to the top of the terminal.

@dankamongmen
Copy link
Owner Author

probably the fastest solution is to use direct mode when -k is provided, honestly. that would also eliminate the annoying "feature" of always being blitted to the top of the terminal.

kinda problematic for video, though, no?

@dankamongmen dankamongmen changed the title improve performance of ncplayer in one-shot case ncplayer ought use direct mode when invoked with -k Feb 10, 2021
@dankamongmen
Copy link
Owner Author

we see a bit more variance if we restrict things to notcurses_init() and notcurses_stop(). it looks like the minimum time for that minimum program is ~30ms:

[schwarzgerat](0) $ for i in `seq 1 3` ; do time ./ncplayer -q -t1 ../data/worldmap.png ; done

real	0m0.031s
user	0m0.020s
sys	0m0.011s

real	0m0.026s
user	0m0.021s
sys	0m0.004s

real	0m0.026s
user	0m0.021s
sys	0m0.004s
[schwarzgerat](0) $ for i in `seq 1 3` ; do time ./ncplayer -q -t1 ../data/worldmap.png ; done

real	0m0.034s
user	0m0.026s
sys	0m0.008s

real	0m0.032s
user	0m0.028s
sys	0m0.005s

real	0m0.033s
user	0m0.028s
sys	0m0.005s

@dankamongmen
Copy link
Owner Author

holy crap, we can do a notcurses-core program (with a render) in just a few ms:

real	0m0.006s
user	0m0.001s
sys	0m0.005s
real	0m0.005s
user	0m0.000s
sys	0m0.005s
real	0m0.005s
user	0m0.005s
sys	0m0.000s

so maybe it really is the atrociously bloated dependency stack that dominates that shared 40ms? is there anything we can do about that?

@dankamongmen
Copy link
Owner Author

ugh, yeah, try setting LD_DEBUG=bindings,detail, and watch the deluge of shitwork the loader does due to ffmpeg. argh.

@dankamongmen
Copy link
Owner Author

-Bsymbolic-functions shows no effect

@dankamongmen
Copy link
Owner Author

no help from -Bdirect nor -Wl,-O1, either.

@dankamongmen
Copy link
Owner Author

I notice that on my system, /usr/bin/ffmpeg takes ~80ms when run with no arguments, whereas /usr/local/bin/ffmpeg takes ~5ms. furthermore, ldd on the former shows libav and friends, but it does not on the latter. is the latter maybe statically linking libav, and dynamically linking everything else? hrmm, yes, i think so:

[schwarzgerat](1) $ time /usr/bin/ffmpeg 2> e

real	0m0.079s
user	0m0.265s
sys	0m0.882s
[schwarzgerat](1) $ time /usr/local/bin/ffmpeg 2> e

real	0m0.010s
user	0m0.005s
sys	0m0.005s
[schwarzgerat](1) $ ls -l /usr/bin/ffmpeg /usr/local/bin/ffmpeg 
-rwxr-xr-x 1 root root   293160 2021-01-26 13:54 /usr/bin/ffmpeg
-rwxr-xr-x 1 root root 21410992 2021-01-27 18:08 /usr/local/bin/ffmpeg
[schwarzgerat](0) $ 

eeeeenteresting.

@dankamongmen
Copy link
Owner Author

yep, i was able to cut 20ms (25% of total runtime) off with a static link against libav, dynamically linking the rest (just like the ffprobe above):

[schwarzgerat](0) $ /usr/bin/c++  -O2 -g -DNDEBUG CMakeFiles/ncplayer.dir/src/player/play.cpp.o CMakeFiles/ncplayer.dir/src/compat/compat.c.o -o ncplayer  -Wl,-rpath,/home/dank/src/dankamongmen/notcurses/build: libnotcurses++.a libnotcurses.a libnotcurses-core.a -ltinfo -lunistring -lreadline ~/src/ffmpeg/libavformat/*.a ~/src/ffmpeg/libavcodec/*.a ~/src/ffmpeg/libavutil/*.a ~/src/ffmpeg/libswscale/*.a ~/src/ffmpeg/libswresample/*.a -lz -lX11 -ldl -lva -lva-drm -lva-x11 -lvdpau -lbz2 -llzma -lopus -pthread
[schwarzgerat](0) $ for i in `seq 1 3` ; do time ./ncplayer -q -t0 -k ../data/worldmap.png > e; done





real	0m0.060s
user	0m0.020s
sys	0m0.008s

real	0m0.051s
user	0m0.017s
sys	0m0.001s

real	0m0.058s
user	0m0.018s
sys	0m0.008s
[schwarzgerat](0) $ 

damn i'm good! but how can we take advantage of this?

@dankamongmen
Copy link
Owner Author

ok, i think we've got it

2021-02-14-193300_802x1417_scrot

@dankamongmen
Copy link
Owner Author

AND we have met our performance goals, cutting time when using -k by 50%, down to 40ms!!! yay! i'm the fuckin' best!

[schwarzgerat](0) $ for i in `seq 1 3` ; do time ./ncplayer -q -t0 -k ../data/worldmap.png > e; done

real	0m0.045s
user	0m0.037s
sys	0m0.008s

real	0m0.042s
user	0m0.034s
sys	0m0.008s

real	0m0.041s
user	0m0.033s
sys	0m0.008s
[schwarzgerat](0) $ 

dankamongmen added a commit that referenced this issue Feb 15, 2021
dankamongmen added a commit that referenced this issue Feb 15, 2021
@dankamongmen dankamongmen modified the milestones: 3.0.0, 2.3.0 Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request perf sweet sweet perf
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant