Repository navigation
Conversation
To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
| static inline bool IsAllASCII(std::string* input) { | ||
| for (size_t n = 0; n < input->size(); n++) { | ||
| const char ch = (*input)[n]; | ||
| if (ch & 0x80) { |
There was a problem hiding this comment.
Please declare a new CHAR_TEST in the dedicated section above (grep CHAR_TEST to find where it is):
// https://infra.spec.whatwg.org/#ascii-code-point
CHAR_TEST(8, IsASCIICodePoint, (ch >= '\0' && ch <= '\x7f'))And use IsASCIICodePoint(ch) here.
There was a problem hiding this comment.
@TimothyGu : Done all changes suggested.
| return type; | ||
| } | ||
|
|
||
| static inline bool IsAllASCII(std::string* input) { |
There was a problem hiding this comment.
You can use a const std::string& here instead of std::string* too.
There was a problem hiding this comment.
Perhaps we can use contains_non_ascii() from string_bytes.cc instead as an optimization?
There was a problem hiding this comment.
@mscdex : That function is inside anon namespace and is static. Do you off hand know any place where we can refactor it in some utility?.
There was a problem hiding this comment.
@mscdex : I checked the function, it optimizes for length >=16 . Most domain names will be smaller, so will it help?. We can try and benchmark it.
There was a problem hiding this comment.
Well if nothing else it's good to reuse existing functionality where possible. That way if further optimizations are ever made to that function, everyone benefits automatically.
There was a problem hiding this comment.
Actually, it may be even better to just fold the checking and lowercasing into the same function and loop (reading and lowercasing 4 bytes at a time as long as possible). That way you're only iterating over the string once.
There was a problem hiding this comment.
@mscdex : I made changes to use existing contains_non_ascii.
Will check about folding checking and lowercasing into one.
There was a problem hiding this comment.
@mscdex : folding both checking and lowercasing will result
either
- Code duplication as ContainsNonAscii is used at one more place. or
- Or have a function pointer as argument, its a problem with separation of concern. IIRC since function pointers are of statically know function it will get inlined.
Whats your opinion on this?.
I made changes to re-use ContainsNonAscii. Have a look at that when you get time.
| // Match browser behavior for ASCII only domains | ||
| // and do not run them through ToASCII algorithm. | ||
| if (IsAllASCII(&decoded)) { | ||
| // Lowercase aschii domains |
| decoded[n] = std::tolower(decoded[n]); | ||
| } | ||
| } else { | ||
| // Then we have to punycode toASCII |
| if (IsAllASCII(&decoded)) { | ||
| // Lowercase aschii domains | ||
| for (size_t n = 0; n < decoded.size(); n++) { | ||
| decoded[n] = std::tolower(decoded[n]); |
| // and do not run them through ToASCII algorithm. | ||
| if (IsAllASCII(&decoded)) { | ||
| // Lowercase aschii domains | ||
| for (size_t n = 0; n < decoded.size(); n++) { |
There was a problem hiding this comment.
For converting to lowercase, consider converting multiple bytes at a time instead of 1 by 1. Here is one good example of how to do that.
There was a problem hiding this comment.
I'm not sure if this is that performance-sensitive, and even if it is C-level SIMD optimization still sounds like overkill to me.
There was a problem hiding this comment.
Technically it's not "proper" SIMD (e.g. SSE) unless the compiler is somehow smart enough to automatically convert it to that. Anyway, I still think it would be worth benchmarking...
To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
| } | ||
|
|
||
| { | ||
| for (const [i, { ascii, unicode }] of testsHyphenDomains.valid.entries()) { |
There was a problem hiding this comment.
If it's only for testing that those domains won't get converted, maybe just make the test cases an array of string and assert.strictEqual(domain, domainToASCII(domain))?
There was a problem hiding this comment.
Oh..never mind, it is supposed to check uppercase characters will be converted to lowercase ones...(is there any in the test cases?)
There was a problem hiding this comment.
Made the change, reverting it. Thanks for catching.
| if (IsAllASCII(decoded)) { | ||
| // Lowercase ASCII domains | ||
| for (size_t n = 0; n < decoded.size(); n++) { | ||
| decoded[n] = ASCIILowercase(decoded[n]); |
There was a problem hiding this comment.
Another optimization would be testing if the decoded domain contains only lowercase characters first. If it does, just use the original string and avoid assignments.
| } | ||
|
|
||
| { | ||
| for (const [i, { ascii, unicode }] of testsHyphenDomains.valid.entries()) { |
There was a problem hiding this comment.
Oh..never mind, it is supposed to check uppercase characters will be converted to lowercase ones...(is there any in the test cases?)
To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
| return type; | ||
| } | ||
|
|
||
| static inline bool IsAllASCII(const std::string& input) { |
| if (IsAllASCII(decoded)) { | ||
| // Lowercase ASCII domains | ||
| for (size_t n = 0; n < decoded.size(); n++) { | ||
| if (!IsLowerCaseASCII(decoded[n])) { |
There was a problem hiding this comment.
No need for this check; ASCIILowercase will do nothing if the code unit is already lower case or not a letter.
There was a problem hiding this comment.
Won't this prevent idempotent writes to lowercase chars?. @joyeecheung that was the intent right?.
There was a problem hiding this comment.
I was acutually talking about something like
bool needs_lowercase = false;
for (size_t n = 0; n < decoded.size(); n++) {
if (!IsLowerCaseASCII(decoded[n])) {
needs_lowercase = true;
break;
}
}
if (needs_lowercase) {
// convert decode
}
// otherwise no need to mutate decode, which should be the common caseTo match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
| return false; | ||
| } | ||
|
|
||
| bool ContainsNonAscii(const char* src, size_t len) { |
There was a problem hiding this comment.
Done, along with other changes.
| if (!stringutils::ContainsNonAscii(buf, strlen(buf))) { | ||
| // Lowercase ASCII domains | ||
| for (size_t n = 0; n < decoded.size(); n++) { | ||
| if (!IsLowerCaseASCII(decoded[n])) { |
There was a problem hiding this comment.
Again, the IsLowerCaseASCII is not needed.
|
|
||
| #include "env.h" | ||
| #include "env-inl.h" | ||
| #include "util.h" |
There was a problem hiding this comment.
My guess is that some of these includes may not be needed.
0cdbd66 to
e0a9a33
Compare
To match browser behavior fast path ascii only domains and do not run ToASCII on them. Fixes: nodejs#12965 Refs: nodejs#12966 Refs: whatwg/url#309
e0a9a33 to
861604f
Compare
|
ping @TimothyGu, have your comments been addressed? |
|
@addaleax : I have another PR some problem which is as per very latest changes in spec #12966 . I'll close current one. //cc @TimothyGu |
To match browser behavior fast path ascii only domains and
do not run ToASCII on them.
Fixes: #12965
Refs: #12966
Refs: whatwg/url#309
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)