Skip to content

Commit d98b9c5

Browse files
maartenbreddelspitrou
authored andcommitted
ARROW-9131: [C++] Faster ascii_lower and ascii_upper.
Following up on #7418 I tried and benchmarked a different way for * ascii_lower * ascii_upper Before (lower is similar): ``` -------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------- AsciiUpper_median 4922843 ns 4918961 ns 10 bytes_per_second=3.1457G/s items_per_second=213.17M/s ``` After: ``` -------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------- AsciiUpper_median 1391272 ns 1390014 ns 10 bytes_per_second=11.132G/s items_per_second=754.363M/s ``` This is a 3.7x speedup (on a AMD machine). Using http://quick-bench.com/JaDErmVCY23Z1tu6YZns_KBt0qU I found 4.6x speedup for clang 9, 6.4x for GCC 9.2. Also, the test is expanded a bit to include a non-ascii codepoint, to make explicit it is fine to upper or lower case a utf8 string. The non-overlap encoding of utf8 make this ok (see section 2.5 of Unicode Standard Core Specification v13.0). Closes #7434 from maartenbreddels/ARROW-9131 Authored-by: Maarten A. Breddels <maartenbreddels@gmail.com> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 96279ee commit d98b9c5

2 files changed

Lines changed: 17 additions & 64 deletions

File tree

cpp/src/arrow/compute/kernels/scalar_string.cc

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -64,77 +64,30 @@ void StringDataTransform(KernelContext* ctx, const ExecBatch& batch,
6464
}
6565
}
6666

67-
// Generated with
68-
//
69-
// print("static constexpr uint8_t kAsciiUpperTable[] = {")
70-
// for i in range(256):
71-
// if i > 0: print(', ', end='')
72-
// if i >= ord('a') and i <= ord('z'):
73-
// print(i - 32, end='')
74-
// else:
75-
// print(i, end='')
76-
// print("};")
77-
78-
static constexpr uint8_t kAsciiUpperTable[] = {
79-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
80-
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
81-
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
82-
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
83-
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
84-
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
85-
96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
86-
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 123, 124, 125, 126, 127,
87-
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
88-
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
89-
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
90-
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
91-
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
92-
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
93-
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
94-
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
95-
9667
void TransformAsciiUpper(const uint8_t* input, int64_t length, uint8_t* output) {
9768
for (int64_t i = 0; i < length; ++i) {
98-
*output++ = kAsciiUpperTable[*input++];
69+
const uint8_t utf8_code_unit = *input++;
70+
// Code units in the range [a-z] can only be an encoding of an ascii
71+
// character/codepoint, not the 2nd, 3rd or 4th code unit (byte) of an different
72+
// codepoint. This guaranteed by non-overlap design of the unicode standard. (see
73+
// section 2.5 of Unicode Standard Core Specification v13.0)
74+
*output++ = ((utf8_code_unit >= 'a') && (utf8_code_unit <= 'z'))
75+
? (utf8_code_unit - 32)
76+
: utf8_code_unit;
9977
}
10078
}
10179

10280
void AsciiUpperExec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
10381
StringDataTransform(ctx, batch, TransformAsciiUpper, out);
10482
}
10583

106-
// Generated with
107-
//
108-
// print("static constexpr uint8_t kAsciiLowerTable[] = {")
109-
// for i in range(256):
110-
// if i > 0: print(', ', end='')
111-
// if i >= ord('A') and i <= ord('Z'):
112-
// print(i + 32, end='')
113-
// else:
114-
// print(i, end='')
115-
// print("};")
116-
117-
static constexpr uint8_t kAsciiLowerTable[] = {
118-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
119-
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
120-
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
121-
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
122-
64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
123-
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95,
124-
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
125-
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
126-
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
127-
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
128-
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
129-
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
130-
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
131-
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
132-
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
133-
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
134-
13584
void TransformAsciiLower(const uint8_t* input, int64_t length, uint8_t* output) {
13685
for (int64_t i = 0; i < length; ++i) {
137-
*output++ = kAsciiLowerTable[*input++];
86+
// As with TransformAsciiUpper, the same guarantee holds for the range [A-Z]
87+
const uint8_t utf8_code_unit = *input++;
88+
*output++ = ((utf8_code_unit >= 'A') && (utf8_code_unit <= 'Z'))
89+
? (utf8_code_unit + 32)
90+
: utf8_code_unit;
13891
}
13992
}
14093

cpp/src/arrow/compute/kernels/scalar_string_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ TYPED_TEST(TestStringKernels, AsciiLength) {
5757
}
5858

5959
TYPED_TEST(TestStringKernels, AsciiUpper) {
60-
this->CheckUnary("ascii_upper", "[\"aAa&\", null, \"\", \"b\"]", this->string_type(),
61-
"[\"AAA&\", null, \"\", \"B\"]");
60+
this->CheckUnary("ascii_upper", "[\"aAazZæÆ&\", null, \"\", \"b\"]",
61+
this->string_type(), "[\"AAAZZæÆ&\", null, \"\", \"B\"]");
6262
}
6363

6464
TYPED_TEST(TestStringKernels, AsciiLower) {
65-
this->CheckUnary("ascii_lower", "[\"aAa&\", null, \"\", \"b\"]", this->string_type(),
66-
"[\"aaa&\", null, \"\", \"b\"]");
65+
this->CheckUnary("ascii_lower", "[\"aAazZæÆ&\", null, \"\", \"b\"]",
66+
this->string_type(), "[\"aaazzæÆ&\", null, \"\", \"b\"]");
6767
}
6868

6969
TYPED_TEST(TestStringKernels, Strptime) {

0 commit comments

Comments
 (0)