Skip to content

Performance optimization for case_fold - #326

Open
andreasbuhr wants to merge 1 commit into
boostorg:developfrom
andreasbuhr:case_fold_fast_path
Open

andreasbuhr wants to merge 1 commit into
boostorg:developfrom
andreasbuhr:case_fold_fast_path

Conversation

@andreasbuhr

Copy link
Copy Markdown
Contributor

There was one call to std::lower_bound for each space, for each parenthesis, for each newline, and for each bracket. This is not required.
See https://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt and https://www.fileformat.info/info/charset/UTF-32/list.htm

// One-byte fast path.
if (cp < 0x100) {
// ASCII letter fast path.
if (0x61 <= cp && cp <= 0x7a) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the if (cp < 0xb5) case below seems good to me, but removing the fast-path for the already-folded region [0x61, 0x7a] seems wrong. Is there a reason for it, or was it a mistake?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I thought about it and it was a deliberate choice to remove it. But it's not important. Important is that there is no more std::lower_bound call for each space, brace, etc, as it was in the past. I am fine both with the fast path and without it.

If you are interested, here is my reasoning. Only read if you really want to know:

I considered six cases and counted the number of comparisons for each case.

Three different classes of characters:

  1. uppercase A-Z [0x41, 0x5a]
  2. lowercase a-z [0x61, 0x7a]
  3. punctuation/whitespace ()*+.. [0x21,0x40]

And two code options:
A: with fast-path for [0x61,0x7a]
B: without fast-path for [0x61,0x7a]

Now let's count the comparisons:

First uppercase A-Z:
Case 1-A:
0x61(false),0x41(true),0x5a(true)
Case 1-B:
0x41(true),0x5a(true)
-> The fast path for lowercase a-z costs us one comparison for each uppercase A-Z

Then lowercase a-z:
Case 2-A:
0x61(true),0x7a(true)
Case 2-B:
0x41(true),0x5a(false),0xb5(true)
-> The fast path for lowercase a-z saves us one comparison for each lowercase a-z

Then punctuation/whitespace:
Case 3-A:
0x61(false),0x41(false),0xb5(true)
Case 3-B:
0x41(false),0xb5(true)
-> The fast path for lowercase a-z costs us one comparison for each punctuation or whitespace

Conclusion:
The fast path for a-z saves us one comparison for each lowercase a-z character, but costs us one comparison for every uppercase character, every punctuation, brace, space etc. Looking at my vba code, I had the impression that there are not more a-z characters than other characters. Combined with my fundamental believe "less code is better" I decided to remove the fast-path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading my own comment I realized we can do even better:
#335

There was one call to std::lower_bound for each space,
for each parenthesis, for each newline, and for each bracket.
This is not required.
See https://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt
and https://www.fileformat.info/info/charset/UTF-32/list.htm
@andreasbuhr

Copy link
Copy Markdown
Contributor Author

I rebased on develop

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants