Hi,

I've inherited an old code base of "C" code, which was maintained by
relatively inexperience team. One of the common pattern that I've seen was:

for (int i=0 ; i < strlen(s) ; i++) { ... }

Which has O(n^2) performance, given O(n) performance on s. Acceptable on
strings up to 400-500 characters, but painful when large strings - 4k.

My question: Is there a path to add new performance-related diagnostics for
those cases,  in general, any function with expected O(n) in the
condition/step of a loop (for ; while ; do ... while) should trigger this
warning. The most common pattern that I've seen - I believe other pattern
also exists - with strchr, strstr, ...

Ideally, this will come with an annotation that can be placed on function
to say "I'm expensive, should not be called in a tight loop"

[[GNU:expensive]] const int count_something(const char *x) ;

which will result in a performance warning on
for (int i=0 ; i<count_something(s) ; i++)

Looking for feedback, and suggestion on how to get something like that
implement - is this something that will be useful ?

Yair

Reply via email to