[PATCH 1/3] uclient-fetch: Make request_types sorted to optimize search

Paul Oranje por at oranjevos.nl
Fri Apr 7 05:45:21 PDT 2023


Op 06-04-2023 om 23:39 schreef Sergey Ponomarev:
> Signed-off-by: Sergey Ponomarev <stokito at gmail.com>
> ---
>   uclient-http.c | 17 +++++++++++------
>   1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/uclient-http.c b/uclient-http.c
> index c2bba6b..80d4495 100644
> --- a/uclient-http.c
> +++ b/uclient-http.c
> @@ -40,11 +40,11 @@ enum auth_type {
>   };
>   
>   enum request_type {
> +	REQ_DELETE,
>   	REQ_GET,
>   	REQ_HEAD,
>   	REQ_POST,
>   	REQ_PUT,
> -	REQ_DELETE,
>   	__REQ_MAX
>   };
>   
> @@ -57,12 +57,13 @@ enum http_state {
>   	HTTP_STATE_ERROR,
>   };
>   
> +// Must be sorted aplhabeticaly
>   static const char * const request_types[__REQ_MAX] = {
> +	[REQ_DELETE] = "DELETE",
>   	[REQ_GET] = "GET",
>   	[REQ_HEAD] = "HEAD",
>   	[REQ_POST] = "POST",
>   	[REQ_PUT] = "PUT",
> -	[REQ_DELETE] = "DELETE",
>   };
>   
>   struct uclient_http {
> @@ -991,11 +992,15 @@ uclient_http_set_request_type(struct uclient *cl, const char *type)
>   		return -1;
>   
>   	for (i = 0; i < ARRAY_SIZE(request_types); i++) {
> -		if (strcmp(request_types[i], type) != 0)
> +		int c = strcmp(request_types[i], type);
> +		if (c < 0) {
>   			continue;
> -
> -		uh->req_type = i;
> -		return 0;
> +		} else if (c == 0) {
> +			uh->req_type = i;
> +			return 0;
> +		} else {
> +			return -1;
> +		}
>   	}
>   
>   	return -1;

Hi Sergey,

Seems okay, but one remark. An else branch after statements like 
continue or return is not needed. Without the else the code saves 
indents and becomes clearer.

So:
  	for (i = 0; i < ARRAY_SIZE(request_types); i++) {
-		if (strcmp(request_types[i], type) != 0)
+		int c = strcmp(request_types[i], type);
+		if (c < 0) {
  			continue;
-
-		uh->req_type = i;
-		return 0;
+		}
+		if (c == 0) {
+			uh->req_type = i;
+			return 0;
+		}
+		return -1;
  	}
	
  	return -1;

Also it looks like the return -1 after the loop can/should be dropped as 
that (iff request_types is sorted and contains > 0 elements) can never 
be reached. Or the return at the end of the loop block could be dropped 
as it provides a neglectible optimalisation.

Regards, Paul





More information about the openwrt-devel mailing list