Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite parseIntegerList with better performance #19619

Merged
merged 1 commit into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions cocos/2d/CCSpriteFrameCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,20 @@ SpriteFrameCache::~SpriteFrameCache()

void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
{
std::string delim(" ");

size_t n = std::count(string.begin(), string.end(), ' ');
res.resize(n+1);

size_t start = 0U;
size_t end = string.find(delim);

int i=0;
while (end != std::string::npos)
{
res[i++] = atoi(string.substr(start, end - start).c_str());
start = end + delim.length();
end = string.find(delim, start);
}

res[i] = atoi(string.substr(start, end).c_str());
res.resize(n + 1);

const char *cStr = string.c_str();
char *endptr;

int i = 0;
do {
long int val = strtol(cStr, &endptr, 10);
if (endptr == cStr)
return;
res[i++] = static_cast<int>(val);
cStr = endptr;
} while (*endptr != '\0');
Copy link
Contributor

Choose a reason for hiding this comment

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

There are two conditions to terminate the while loop:

  • endptr == cStr
  • *endptr == '\0'

Does it need tow conditions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure about the precondition of the string containing list of integers. I added it because I am not sure about the string format.

Does the plist file guarantee that the string is a list of integers separated by a single space? If so, the endptr == cStr statement is redundant. It can be deleted safely.

However, if the string could be like " " (a string with a single space), this loop will run forever without the above condition. A plist file with wrong format may cause the program hang on.

Or the file have been sanitized somewhere else?

Copy link
Contributor

Choose a reason for hiding this comment

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

K, then i think keep it is more safe.

}

void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
Expand Down