Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1su1aB-003Vke-KR for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Sep 2024 03:23:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1su1aA-001YiU-9N for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Sep 2024 03:23:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1su1aA-001YiL-04 for pgsql-hackers@lists.postgresql.org; Fri, 27 Sep 2024 03:23:50 +0000 Received: from mail-io1-xd31.google.com ([2607:f8b0:4864:20::d31]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1su1a7-001Fk2-HB for pgsql-hackers@postgresql.org; Fri, 27 Sep 2024 03:23:48 +0000 Received: by mail-io1-xd31.google.com with SMTP id ca18e2360f4ac-82aa7c3b3dbso86615939f.2 for ; Thu, 26 Sep 2024 20:23:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727407426; x=1728012226; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=y22gZSzgn5whDjMO5+BnkiPkIxnFS6Qv0LPVONnlXpE=; b=k5VTyZyUu6OJAbs4/IH+KMl/h5PsZ75yeXq+hEW51s8mHLvWSj3l5eyVOvEU27anFE Ft5Ffy0THpilPMVhKWcl3sKTK2X25LS17jMqCNsJirWC8crAOK+M5+/MWQdG80NFAgsk Xbcz7UwKcK7PTIvrnD+Tdgt52055n8jqx21i1hv8Ppi9XopMwrL8+XCmpMyUgH0kWS22 Lo8HPM/LTr6dYAKAlV6j5dq9Ex+qJNUdypfA1clsr4goQ+65Z//6B+N0QpQbSNI67Fkd 2S2CsWnX4NN99jklmhMADKg906vZ1QYpoi5BFamZ5ktgPX7Gkn6aWOhWVFYu1hXYK3s5 hdxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727407426; x=1728012226; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=y22gZSzgn5whDjMO5+BnkiPkIxnFS6Qv0LPVONnlXpE=; b=njuFgBWi24B9eEBkB6xTZBtnL1viRKNkU+qebwhXQ5ox7+VbmrHPi0woZrAj2aMV6P KryB9i9kRjAj93CfnJAeZ0tEerY2uLbbJrBrB8xUGIl7JB95pmQO0JrvsTzS5+6qqK5L lUW//3O4HTDHFdXAuGJu0eAmEN19v800IPIoljl2jI0TRU9lqZvhYDKF999JDVpLcr8j Umoo+0ItbD+KkM8v7X7vQmEWm7Di8/X9Qz/A8XIPPsaWfxGhjiQi/IGdLhqWt5HbVcqV bGX4a70Bdrw5Nr8vItkmH7PWuCcA0yk0GBMaC/oQMPoqTyTwNvhUyiTs1uRmsQYfWUAi gteg== X-Gm-Message-State: AOJu0YyCMM7kldH4WPFBw5CSBfRhUtWP+AQvcZHMG+PKIc3/op7q3ogx 5AadQk9KA/VErCKGspSViG9rnzoZ5z1GJdAB56xDsz5svoqqGRXY X-Google-Smtp-Source: AGHT+IEhKU0n/UXYnC90GWj24xGV8n+oHhMZVvxaEqp/doRo4yURGdVSjn87wJh9jK7Gca2xlXZ6RQ== X-Received: by 2002:a05:6602:13cf:b0:82a:2053:e715 with SMTP id ca18e2360f4ac-83493269970mr232735139f.14.1727407426512; Thu, 26 Sep 2024 20:23:46 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4d888835039sm277665173.28.2024.09.26.20.23.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Sep 2024 20:23:45 -0700 (PDT) Date: Thu, 26 Sep 2024 22:23:43 -0500 From: Nathan Bossart To: David Rowley Cc: pgsql-hackers@postgresql.org Subject: Re: micro-optimize nbtcompare.c routines Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Sep 27, 2024 at 02:50:13PM +1200, David Rowley wrote: > I had been looking at [1] (which I've added your version to now). I > had been surprised to see gcc emitting different code for the first 3 > versions. Clang does a better job at figuring out they all do the same > thing and emitting the same code for each. Interesting. > I played around with the attached (hacked up) qsort.c to see if there > was any difference. Likely function call overhead kills the > performance anyway. There does not seem to be much difference between > them. I've not tested with an inlined comparison function. I'd expect worse performance with the branchless routines for the inlined case. However, I recall that clang was able to optimize med3() as well as it can with the branching routines, so that may not always be true. > Looking at your version, it doesn't look like there's any sort of > improvement in terms of the instructions. Certainly, for clang, it's > worse as it adds a shift left instruction and an additional compare. > No jumps, at least. I think I may have forgotten to add -O2 when I was inspecting this code with godbolt.org earlier. *facepalm* The different versions look pretty comparable with that added. > What's your reasoning for returning INT_MIN and INT_MAX? That's just for the compile option added by commit c87cb5f, which IIUC is intended to test that we correctly handle comparisons that return INT_MIN. -- nathan