git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] fsm-listen-darwin: combine bit operations

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 20, 2023, 17:52 UTC
Message-ID
<xmqqwn5hw0t5.fsf@gitster.g>
In-Reply-To
<021ab1ab-b90a-5a24-23c4-44e46d87c476@jeffhostetler.com>
Jeff Hostetler <git@jeffhostetler.com> writes:
Show 15 quoted lines
>>     static int ef_is_dropped(const FSEventStreamEventFlags ef)
>>   {
>> -	return (ef & kFSEventStreamEventFlagMustScanSubDirs ||
>> -		ef & kFSEventStreamEventFlagKernelDropped ||
>> -		ef & kFSEventStreamEventFlagUserDropped);
>> +	return (ef & (kFSEventStreamEventFlagMustScanSubDirs |
>> +		      kFSEventStreamEventFlagKernelDropped |
>> +		      kFSEventStreamEventFlagUserDropped));
>>   }
>
> Technically, the returned value is slightly different, but
> the only caller is just checking for non-zero, so it doesn't
> matter.
>
> So this is fine.

But is it worth the code churn and reviewer bandwidth? Don't we have better things to spend our time on?

I would not be surprised if a smart enough compiler used the same transformartion as this patch does manually as an optimization.

Then it matters more which one of the two is more readable by our developers. And the original matches how we humans would think, I would imagine. ef might have MustScanSubdirs bit, KernelDropped bit, or UserDropped bit and in these cases we want to say that ef is dropped. Arguably, the original is more readble, and it would be a good change to adopt if there is an upside, like the updated code resulting in markedly more efficient binary.

So, this might be technically fine, but I am not enthused to see these kind of code churning patches with dubious upside. An optimization patch should be able to demonstrate its benefit with a solid benchmark, or at least a clear difference in generated code.

In fact.

Compiler explorer godbolt.org tells me that gcc 12 with -O2 compiles the following two functions into identical assembly. The !! prefix used in the second example is different from the postimage of what Seija posted, but this being a file-scope static function, I would expect the compiler to notice that the actual value would not matter to the callers, only the truth value, does.

* Input *
int one(unsigned int num) {
    return ((num & 01) ||
            (num & 02) || (num & 04));
}
int two(unsigned int num) {
    return !!((num) & (01|02|04));
}
* Assembly *
one(unsigned int):
        xor     eax, eax
        and     edi, 7
        setne   al
        ret
two(unsigned int):
        xor     eax, eax
        and     edi, 7
        setne   al
        ret
Previous: Jeff HostetlerNext: Jeff Hostetler
Message 4 of 5 in “fsm-listen-daarwin: combine bit operations”
  1. fsm-listen-daarwin: combine bit operationsRose via GitGitGadget, Jan 17, 2023
  2. fsm-listen-darwin: combine bit operationsRose via GitGitGadget, Jan 17, 2023
  3. Jeff HostetlerJan 20, 2023
  4. Junio C HamanoJan 20, 2023
  5. Jeff HostetlerJan 20, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.