Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1427226iob; Sat, 14 May 2022 08:42:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEa0MxqQmHOuZGMkBmAgvDSupN09ZEAq1UdGLYHGdSdk+p4MBl80FULegBItWG1XC4TpG0 X-Received: by 2002:a05:600c:3512:b0:394:7c3b:53d1 with SMTP id h18-20020a05600c351200b003947c3b53d1mr19744432wmq.197.1652542938895; Sat, 14 May 2022 08:42:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652542938; cv=none; d=google.com; s=arc-20160816; b=qZakqfxSojSexT0RXmnJ+KGhgXGZQxdI9I9rBO8QF9dsE2SosYStpEHaauG/cDIkg+ CeW2snzz+TojobP+UxzzGvyAd63ihS8mS8PS5OZi/R2oDiDLf8T6KyP+Ic4MWfcvB02/ 6sTMW6A0AqGuwg0iVcnMpKgiYzn3FeqUkSjrp9Pm6izUFTCv1aNZuvLqZrYwcpxwGxjU KUnR2FeLgNjAysN7ITH+aThVVWTsHNIO0Ww+CPSnv/qSpJGTDV2s6B/y82U1Fewv/3kJ bu2+bzyG+6CU2Axr6P4yTuTG2xPmGieVBcss6ssCgpzkHlO015wExa8i+/HzERnWhe6c z46Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=4vtyw82gu0YWV4gUHDkTWkOdf5pLOXcWgWx74KiU7mY=; b=igJ6EhQvxsYGXP2MgOfHCOsecjLrGKvjLjYwOV8QoUj62QINRT9iUOZjCSuB0YYXae mspWATJVhBBykuuaGIz/8ZUWhE19SdHF3H+NL4LEsmG4DV9t8IH9htyLpGSI3EkDOhzl tSc+BY1i+XQTcHlBBYXyKF3phwBoR6OVGwzdXq6G2aGMNkj1IvAYymT++MWGYJhlZaaI tzYkY5kSPpQiARNgwHlWNpFJRlBnCj1RdSTKTiWJSGkgYd0VIJ5QvmVxAlIMSFcIfUId 5g5/B5FW8EyhdFHyGDm0jEtSqp6PgJ1HnY54rbIEr+7VhceoHnB1oGsGyexpgZZEeKmV kydw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=l4JajCGk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f7-20020a5d64c7000000b0020cfa50f4b3si2738889wri.65.2022.05.14.08.41.19; Sat, 14 May 2022 08:42:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=l4JajCGk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232285AbiENMZA (ORCPT + 99 others); Sat, 14 May 2022 08:25:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230393AbiENMY6 (ORCPT ); Sat, 14 May 2022 08:24:58 -0400 Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 455AD180; Sat, 14 May 2022 05:24:57 -0700 (PDT) Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-2ebf4b91212so113645547b3.8; Sat, 14 May 2022 05:24:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=4vtyw82gu0YWV4gUHDkTWkOdf5pLOXcWgWx74KiU7mY=; b=l4JajCGkrCp4fqrYhekGpBJKjGWsOji219DoF1cntoiS+FtxthRPXgOWoSCaNJzy4S 97nzSNv0V6Su+6jN2OvD2gV2cHftg4cBvgMIDPgF7jrUqB3C5bKZ1qYnVV1m1hnWiRqR cGvQwba1h0kujPXR4dUxA07T5mLLck3hCGJUdjYPJ0RGpArRb+CDuTtG86E2hJXWSp5X 1iN2QiUZCrcHSVj6b2xfMDI9aLVieQj08aPmsv1lQBcF8cjBnBpOmtXVBp3Ms5MUNbC1 wKwXA6nqEUNMyKYD4TU/bG3wLxwOCyTi9mEGLx/akMalbzwHPj1mebic3OTdh/tu7xWZ BE6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=4vtyw82gu0YWV4gUHDkTWkOdf5pLOXcWgWx74KiU7mY=; b=sn547nuu8LEeWfXmkre8+mno6l5fRRr0PAL7mI6lZxWRPRuhmjoJS+ZvzYHCxXf3Nz HCUHD9+h+Z+w1OpstILNvEfktz+T8eNYHXstndD6vieuZUTtfy7WVWpXWUhF6uiIXQIS Q2YoXWiGOdN4iICzd8UnxiCcqoYrXEjnR2JaHY+UqtZ9jkaDuflCI/b4JGLFEwlvyGqc RjNAQwFFD0yZ85/bIQb24orEeIPIIMggRbAwy3fq5FTFV8Kao+sxP2pIvbBQs1hlKq26 jg8hMXKeek0++klqRYM7B1GT7V5kfxmPnRUzU5mXgwh6w9T7mro7xMmRH6+pCbo9FIeQ TlmA== X-Gm-Message-State: AOAM532gtOzLhr0PEtI+MR2PZNYSGA/FBOhZNJG3t/OBF1n+s8r+tMrG B1apjTCCy8HcLsBYsidfTREju3cDKZyeoU9flvAKfoj+uPvW8w== X-Received: by 2002:a81:2dc5:0:b0:2f5:c6c8:9ee5 with SMTP id t188-20020a812dc5000000b002f5c6c89ee5mr10438204ywt.518.1652531096471; Sat, 14 May 2022 05:24:56 -0700 (PDT) MIME-Version: 1.0 References: <20220512182921.193462-1-max@enpas.org> <20220513205907.6d5473ff.max@enpas.org> <20220514131128.5e647fb8.max@enpas.org> In-Reply-To: <20220514131128.5e647fb8.max@enpas.org> From: Vincent Mailhol Date: Sat, 14 May 2022 21:24:45 +0900 Message-ID: Subject: Re: [PATCH v6] can, tty: can327 CAN/ldisc driver for ELM327 based OBD-II adapters To: Max Staudt Cc: Wolfgang Grandegger , Marc Kleine-Budde , linux-can@vger.kernel.org, Greg Kroah-Hartman , Oliver Neukum , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue. 14 May 2022 =C3=A0 20:11, Max Staudt wrote: > On Sat, 14 May 2022 12:14:24 +0900 > Vincent Mailhol wrote: > > > But I still think it is possible to do pointer arithmetic. > > > > len =3D strnchr(elm->rxbuf, elm->rxfill, '\r') - elm->rxbuf; > > (I let you check that I did not do an off by one mistake). > > > > The above should also work with memchr(). Although the C standard > > doesn't allow pointer arithmetic on void *, GNU C adds an extension > > for that: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > > > > As I said before, your for loop is not fundamentally wrong, this is > > just not my prefered approach. You have my suggestion, choose what you > > prefer. > > Yeah, this is the arithmetic that I'd like to avoid here. In my > opinion, it is clearer with indices. > > If I were searching through a UTF-8 string (i.e. with variable width > chars), that'd be another matter entirely IMHO, and I'd rely on C > library functions that know more about UTF that I do. But it's really > just naked ASCII bytes this time. > > > ...unless memchr() may be faster than the loop? Could this happen? It depends on many factors, the length of the string, the architecture on which you compile it, whether the compiler decides to inline it or not=E2=80=A6 For long strings, there are some tricks to scan through the memory using 64 bits operation instead of doing it bytes per bytes. But there is a preparation overhead which makes it not worth it for small buffers. You can look at glibc implementation for reference: https://github.com/lattera/glibc/blob/master/string/memchr.c Of course, the kernel does not use glibc but often compilers __builtin_memchr() instead (one more time, it depends on the architecture). But it will give you an idea of why memchr() can be faster than the loop. Finally, the compiler might also detect your loop and take the initiative to replace it with a call to memchr(). You have to check the assembly to see if this is the case. For a device which speaks on UART which by nature is slow, optimizations are not critical, so readability is the priority, and is why I prefer using the library functions.