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 1tdqq8-00A1pj-0k for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Jan 2025 13:13:44 +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 1tdqq6-000Qfg-AF for pgsql-hackers@arkaria.postgresql.org; Fri, 31 Jan 2025 13:13:42 +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 1tdqq6-000QfY-19 for pgsql-hackers@lists.postgresql.org; Fri, 31 Jan 2025 13:13:42 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tdqq3-002SsE-1j for pgsql-hackers@postgresql.org; Fri, 31 Jan 2025 13:13:41 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-540218726d5so1894036e87.2 for ; Fri, 31 Jan 2025 05:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738329218; x=1738934018; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=mSYcJHmSYLznGVsBNP6QOZZaZRqqrATsXwPihQOT02Y=; b=i+tfpduLk4h8x808mohbXcAfTlW2aG3kSWqUObvCmiOQ5WFqaa4H5SHX9jXEHflVWG gWMdgSuRmUt6n1/BsTl+SFRJ6da1+vMe6bHTy5yjxj5bvbIcGMYYcbY5gQX7vrTzRIIM bVylv5x8pE1yJQg/00cIfBCq7jvWxFX8j6Mx0n1VhyT4zypPsyDt6jRt8CnX/dT3vXQj uRB/0/3jm03TYgvx/xPWk8jqm2DINgrKhAcqeMeDFJ06MQr4rCH731BpU9l3wxypAixA ADKJavfWIx5+Ynqhtzm8E71/vC49DzQQVZttoy5mMpZDjoSa9TS6BHWnmGMMa/9pvo2F 1u8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738329218; x=1738934018; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mSYcJHmSYLznGVsBNP6QOZZaZRqqrATsXwPihQOT02Y=; b=jFrwfMX4AxggB4AsK4fugEoP+4mCRLj4ydduDKtti/5riwzyn2BkwT7bHhDObgiD9Q UxXDdsj/lNZSFySRPN6oVqbajqmH6/SeKob4PxYyj6nsOTRh1i4iQl7WvLteMRmsUanR 9SdramQnah2+wlM+lZ5my2xgkFzTo6mOYZkA2MYpnZDkfdGVPMHMJFPPspzUV6n6rPy+ JOv+l6MzfduZbispsZRQlB/UCUzU1l03q9QS3AoGv+AzGMpneSChEYNymQOPzQ0pE1KP 6avwUvuXpHnbkilRHbTS2Nu6CDST6amFGXv4OvQ5yWfpIYY4wcbzVOt/ezNFHgn6/z+L SI9w== X-Forwarded-Encrypted: i=1; AJvYcCU/0tmVe9FFo7zv64bLwVU+Z/I6E6se082bMwbokTW/uIEcdJDqUX5tZSY16BufZIEMqf65tHuYSa7CzXc9@postgresql.org X-Gm-Message-State: AOJu0YwAhUKWJoiS82ScuEcjuznWS0jYyW/CYMVXL5Swfh0yhzDpQPaY O9HX0UYN18gjLvBZE/eOn93JU1HalFzGephq/M8oZA8isQpd+ZsD X-Gm-Gg: ASbGncumatpO+jne5lLye3tJM2Kj+MOwpb7qNNFIakWFRYNb0a6qjgbOECGf766nkwO lL89fZm60c9PC/EmAPuU1pGk/qT4X1W4jyyoDjeY4dM/M2084+1FGFlmc4BhyT69mN/5AE/9X5N eLIp1L5MM9DT9qAycTdB5M+5V8kDYCbN/fi/ZC4ZIseXCZtN+h/IdTxTqvIfacjn9pTzr2zyU3J ijP2rFIezDbgUDgnzF7or4zaAVB6RBvdJz0bUt7WkzrQtBVHq6kv7kQF6w17J6kw1ey9ukbmXDC T9AMh2HZe+kbZVpxkvdjIKbOKlMwgBJTDaLkMdtsiJ/av7Tjzi5BbOwJ+dcuZ3m3cG+xjPSY2A= = X-Google-Smtp-Source: AGHT+IHZABrwqjeJC1Dtyuzlo8wJu5cXVZkj+lHicc/hRyp6JM4RNH9/xHhn9rWK76myHrze14bXjQ== X-Received: by 2002:ac2:4895:0:b0:542:91ac:3919 with SMTP id 2adb3069b0e04-543e4c02d92mr2873069e87.23.1738329217305; Fri, 31 Jan 2025 05:13:37 -0800 (PST) Received: from [192.168.50.127] (broadband-188-255-2-52.ip.moscow.rt.ru. [188.255.2.52]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-543ebeb7d19sm475370e87.194.2025.01.31.05.13.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 31 Jan 2025 05:13:37 -0800 (PST) Message-ID: <12956299-1496-4455-b3ab-299cc0a87d7c@gmail.com> Date: Fri, 31 Jan 2025 16:13:36 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Optimization for lower(), upper(), casefold() functions. To: Heikki Linnakangas , pgsql-hackers@postgresql.org References: <7cac7e66-9a3b-4e3f-a997-42aa0c401f80@gmail.com> <2a1b632e-4531-43cf-a6d9-51cc1a271dc1@gmail.com> <7ab9d255-d7c0-4841-96c3-ed3ce9aa4abd@iki.fi> Content-Language: ru From: Alexander Borisov In-Reply-To: <7ab9d255-d7c0-4841-96c3-ed3ce9aa4abd@iki.fi> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 31.01.2025 01:43, Heikki Linnakangas пишет: Hi Heikki, > Did you consider using a radix tree? We use that method in src/backend/ > utils/mb/Unicode/convutils.pm. I'm not sure if that's better or worse > than what's proposed here, but it would seem like a more standard > technique at least. Or if this is clearly better, then maybe we should > switch to this technique in convutils.pm too. A perfect hash would be > another alternative, we use that in src/common/unicode_norm.c. I looked at the radix tree implementation, and according to number of branches and mathematical operations I think radix tree will not be faster than the proposed approach. About the perfect hash. The problem with the perfect hash is that it requires a Unicode codepoint to be stored for matching. Originally I started to optimize Unicode Normalization Form in Postgres. But I decided to “practice” on a case so as not to scare anyone with a big patch at once. Actually, I want to do Unicode in Postgres, optimizations and improvements. > Did you check that these optimizations still win with Unicode version 16 > (https://www.postgresql.org/message-id/146349e4-4687-4321-91af- > f235572490a8@eisentraut.org)? We haven't updated to that yet, but sooner > or later we will. Yes, everything works just as well with Unicode version 16 data. > The way you're defining 'pg_unicode_case_index' as a function in the > header file won't work. It needs to be a static inline function if it's > in the header. Or put it in a .c file. I agree, it needs to be moved to a .c file. > Some ideas on how to squeeze this further: > > - Instead of having one table that contains Lower/Title/Upper/Fold for > every character, it might be better to have four separate tables. I > think that would be more cache-friendly: you typically run one of the > functions for many different characters in a loop, rather than all of > the functions for the same character. You could deduplicate between the > tables too: for many ranges of characters, Title=Upper and Lower=Fold. I'll try to experiment with that. Theoretically the performance should increase. > - The characters are stored as 4-byte integers, but the high byte is > always 0. Could squeeze those out. Not sure if that would be a win if it > makes the accesses unaligned, but you could benchmark that. > Alternatively, use that empty byte to store the 'special_case' index, > instead of having a separate field for it. I thought about it, but it seems that it will make the code much more complicated, and we won't gain much. > > - Many characters that have a special case only need the special case > for some of the functions, not all. If you stored the special_case > separately for each function (as the high byte in the 'simplemap' field > perhaps, like I suggested on previous point), you could avoid having > those "dummy" special cases. That's a good idea. Then the main table will be reduced by uint8*n. Thanks, after the weekend I'll send an updated patch that takes into account the comments/advice. -- SberTech Alexander Borisov