From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752850AbbKYUcB (ORCPT ); Wed, 25 Nov 2015 15:32:01 -0500 Received: from smtprelay0024.hostedemail.com ([216.40.44.24]:35054 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751438AbbKYUb7 (ORCPT ); Wed, 25 Nov 2015 15:31:59 -0500 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::,RULES_HIT:41:355:379:541:599:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2828:2890:2914:3138:3139:3140:3141:3142:3353:3622:3865:3866:3867:3868:3870:3871:3872:3874:4042:4321:4385:5007:6119:6261:7903:8603:8660:9010:10004:10400:10848:11026:11232:11473:11658:11914:12043:12296:12517:12519:12679:12740:13069:13148:13161:13229:13230:13311:13357:14659:21080:21088:30001:30012:30054:30056:30070:30079:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:3,LUA_SUMMARY:none X-HE-Tag: veil10_4b550f1421723 X-Filterd-Recvd-Size: 2580 Message-ID: <1448483516.20113.61.camel@perches.com> Subject: Re: [PATCH] HID: debug: improve hid_debug_event() From: Joe Perches To: Rasmus Villemoes , Jiri Kosina Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 25 Nov 2015 12:31:56 -0800 In-Reply-To: <1448368427-1669-1-git-send-email-linux@rasmusvillemoes.dk> References: <1448368427-1669-1-git-send-email-linux@rasmusvillemoes.dk> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.16.5-1ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2015-11-24 at 13:33 +0100, Rasmus Villemoes wrote: > The code in hid_debug_event() causes horrible code generation. First, > we do a strlen() call for every byte we copy (we're doing a store to > global memory, so gcc has no way of proving that strlen(buf) doesn't > change). Second, since both i, list->tail and HID_DEBUG_BUFSIZE have > signed type, the modulo computation has to take into account the > possibility that list->tail+i is negative, so it's not just a simple > and. > > Fix the former by simply not doing strlen() at all (we have to load > buf[i] anyway, so testing it is almost free) and the latter by > changing i to unsigned. This cuts 29% (69 bytes) of the size of the > function. [] > diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c [] > @@ -659,13 +659,13 @@ EXPORT_SYMBOL_GPL(hid_dump_device); > /* enqueue string to 'events' ring buffer */ > void hid_debug_event(struct hid_device *hdev, char *buf) > { > - int i; > + unsigned i; > struct hid_debug_list *list; > unsigned long flags; > > spin_lock_irqsave(&hdev->debug_list_lock, flags); > list_for_each_entry(list, &hdev->debug_list, node) { > - for (i = 0; i < strlen(buf); i++) > + for (i = 0; buf[i]; i++) > list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] = > buf[i]; > list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE; trivia: The code might look nicer if (list->tail + i) % HID_DEBUG_BUFSIZE was stored into a temporary. Maybe use an if >= BUFSIZE to avoid a % Something like: int pos = list->tail; for (i = 0; buf[i]; i++) { list->hid_debug_buf[pos++] = buf[i]; if (pos >= HID_DEBUG_BUFSIZE) pos = 0; } list->tail = pos;