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

Remove buffer to decrease copy when encode NSString #50

Merged
merged 2 commits into from
Sep 29, 2018

Conversation

zhongwuzw
Copy link
Contributor

m_formatBuffer is not needed, leads to additional malloc and times of copy.

@lingol
Copy link
Collaborator

lingol commented Sep 28, 2018

Have you run the baseline test before & after this modification? Is there any difference for NSString encoding?

Logically speaking, it will be faster after this optimization. It's better to know.

NSData *buffer = [NSData dataWithBytes:m_formatBuffer length:realSize];
encodeItem->value.tmpObjectValue = (void *) CFBridgingRetain(buffer);
NSData *buffer = [str dataUsingEncoding:NSUTF8StringEncoding];
encodeItem->value.tmpObjectValue = (__bridge_retained void *) buffer;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Keep indentation aligned please.

@zhongwuzw
Copy link
Contributor Author

@lingol Yes, it's more faster, but TBO, using baseline test result to compare is unfair, because this optimize only have benefits about 10ms under 10000 times, every time I run test, it's hard to figure out wether it works, because the total time of baseline test ranges from 300ms to 400ms.

So I used instrument to profile, but you know Time Profiler just do samples, so just for reference. From the image below, we can see the consume of old way. It capture mallocgetBytes:maxLength:usedLength:encoding:options:range:remainingRange:+ dataWithBytes:length:- maximumLengthOfBytesUsingEncoding: etc.

image

The new way, Time Profiler capture the consume of dataUsingEncoding, about 9ms,
image

@lingol lingol merged commit 0df419f into Tencent:dev Sep 29, 2018
@zhongwuzw zhongwuzw deleted the optimize_nsstring_copy branch September 29, 2018 02:23
@Knight-ZXW Knight-ZXW mentioned this pull request Sep 12, 2019
@Leiluojun Leiluojun mentioned this pull request Aug 8, 2020
This was referenced Oct 25, 2023
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