llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Does it make sense to optimize strlen in this function with for loops?

Open GermanAizek opened this issue 9 months ago • 4 comments

Function ggml_graph_dump_dot use 2 strlen() in ggml.c Function ggml_visit_parents use 2 strlen() in ggml.c Function ggml_cpy_impl use 1 strlen() in ggml.c

Example:

Before:

image

After:

image

GermanAizek avatar May 13 '24 23:05 GermanAizek

ggml_graph_dump_dot only used for debugging, so it is not important.

ggml_visit_parents is called by ggml_build_forward_expand, which is called every time llama_decode is called. Probably OK to remove strlen here, but performance gain will be very, very minor.

ggml_cpy_impl is not called very often, not important to optimized.

IMO, I think it's really not worth doing this. Writing strlen(...) == 0 is still more self explanatory (easier for new contributors to read the code)

ngxson avatar May 14 '24 14:05 ngxson

@ngxson, I can only fix for ggml_visit_parents, but in comments I specify previous condition with strlen function so that it is readable.

GermanAizek avatar May 14 '24 17:05 GermanAizek

If you want, another idea would be to add a macro IS_STRING_NOT_EMPTY / IS_STRING_EMPTY and re-use it through out the code base

ngxson avatar May 14 '24 17:05 ngxson

We do not want to apply optimizations that serve no real purpose but make the code harder to read. And anyway, the compiler is perfectly capable of optimizing a strlen(s) == 0, eg. https://godbolt.org/z/ocPEv39b7

slaren avatar May 14 '24 17:05 slaren

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 28 '24 01:06 github-actions[bot]