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

【建议】压缩int的时候,如果是负数,现在实现中的压缩结果有点浪费空间 #69

Closed
imzoer opened this issue Oct 6, 2018 · 4 comments

Comments

@imzoer
Copy link

imzoer commented Oct 6, 2018

void CodedOutputData::writeInt32(int32_t value) { if (value >= 0) { this->writeRawVarint32(value); } else { this->writeRawVarint64(value); } }

不明白为什么要采用现在这种压缩方法。
zigzag 压缩负数的效率应该比 writeRawVarint64 的实现要好一些

@imzoer
Copy link
Author

imzoer commented Oct 6, 2018

@lingol
Copy link
Collaborator

lingol commented Oct 6, 2018

And how do you know the value < 0 when decoding? EG, how do you know when to do a un-zigzag when decoding?

To be honest, when MMKV was started, the encoding/decoding code was inspired by the Google official implementation, which will sign-extend negative numbers. If there's a huge call for negative number handling, maybe we should add some SInt32/SInt64 interface.

@imzoer
Copy link
Author

imzoer commented Oct 7, 2018

If we zigzag all positive and negative numbers, we do not have to care if a number is negative. Just un-zigzag all numbers

@lingol
Copy link
Collaborator

lingol commented Oct 8, 2018

And that’s a big if, for breaking existing code/data that relying on not zigzaging positive numbers. You gota think about backward compatibility.

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

No branches or pull requests

2 participants