RESOLVED FIXED 75821
Using strncmp() for comparing scheme and port numbers is inefficient
https://bugs.webkit.org/show_bug.cgi?id=75821
Summary Using strncmp() for comparing scheme and port numbers is inefficient
Benjamin Poulain
Reported 2012-01-08 20:01:02 PST
In KURL, we frequently compare very short strings, like the scheme and port. Currently, we use strncmp(), we could just compare char by char and let the compiler inline everything.
Attachments
Patch (6.17 KB, patch)
2012-01-08 20:09 PST, Benjamin Poulain
no flags
Patch (6.23 KB, patch)
2012-01-14 20:40 PST, Benjamin Poulain
no flags
Patch (6.05 KB, patch)
2012-01-25 17:37 PST, Benjamin Poulain
darin: review+
Benjamin Poulain
Comment 1 2012-01-08 20:09:18 PST
Darin Adler
Comment 2 2012-01-08 20:44:17 PST
Comment on attachment 121613 [details] Patch You should be able to do all of this without hard-coding the lengths of the arrays. template<size_t bLength> static inline bool equal(const char* a, const char b[bLength]) Try it.
Benjamin Poulain
Comment 3 2012-01-09 22:27:22 PST
Comment on attachment 121613 [details] Patch I'll update later this week, I am busy with another bug atm :(
Benjamin Poulain
Comment 4 2012-01-14 20:40:50 PST
Benjamin Poulain
Comment 5 2012-01-14 20:42:00 PST
The template "template<size_t bLength> static inline bool equal(const char* a, const char b[bLength])" never matched. I read online to use a reference to the array, and this time the compiler match the template.
Benjamin Poulain
Comment 6 2012-01-25 17:37:03 PST
Darin Adler
Comment 7 2012-01-26 11:00:36 PST
Comment on attachment 124046 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=124046&action=review > Source/WebCore/platform/KURL.cpp:1079 > +template<size_t referenceLength> > +static inline bool equal(const char* str, size_t length, const char (&reference)[referenceLength]) > +{ > + return length == referenceLength && equal(str, reference); > } This compares two strings. It seems a little strange that the first string is named “str” with length “length” and the second is named “reference” with name “referenceLength”. I would just name them “a” and “b”, or “stringA” and “stringB”, and “lengthA” and “lengthB”, or something like that.
Benjamin Poulain
Comment 8 2012-01-26 11:03:38 PST
> This compares two strings. It seems a little strange that the first string is named “str” with length “length” and the second is named “reference” with name “referenceLength”. I would just name them “a” and “b”, or “stringA” and “stringB”, and “lengthA” and “lengthB”, or something like that. Stricto sensu, it is comparing the string to an (reference) const array, that is why I had chosen the name "reference". I do not mind updating that :) Thanks for the review!
Anders Carlsson
Comment 9 2012-01-26 11:15:44 PST
For reference, I still don't think it's necessary to declare the strings as character arrays, just make equals handle the trailing null character.
Benjamin Poulain
Comment 10 2012-01-26 12:10:09 PST
Darin Adler
Comment 11 2012-01-31 10:31:37 PST
(In reply to comment #9) > For reference, I still don't think it's necessary to declare the strings as character arrays, just make equals handle the trailing null character. Right, we could easily do it that way which would look a little nicer.
Benjamin Poulain
Comment 12 2012-02-18 14:51:47 PST
Comment on attachment 124046 [details] Patch Clearing useless flag.
Note You need to log in before you can comment on or make changes to this bug.