-
Notifications
You must be signed in to change notification settings - Fork 16
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
INTERNAL: Refactor the memcached_io_read function #341
Conversation
memcached_safe_read() ํจ์๊ฐ ์๋ memcached_io_read() ํจ์๋ฅผ ์์ ํ ๊ฒ์ธ๊ฐ์? |
๋ค. io_read์์ ์์ ์ ํ๋ฉด, ๋ณ๋์ buffer๊ฐ ํ์ํ์ง ์์ ํด๋น ํจ์๋ฅผ ์์ ํ์ต๋๋ค. ํ์ฌ CI test(ubuntu)๋ฅผ ์คํจํ์ฌ ์ ์ ์์ ํ ๋๊น์ง draft๋ก ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค. |
์์ ๋์์ต๋๋ค. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์๋ฃ
libmemcached/io.cc
Outdated
@@ -538,8 +538,12 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, | |||
return MEMCACHED_CONNECTION_FAILURE; | |||
} | |||
|
|||
ssize_t unused_nread; | |||
if (nread == NULL) nread= &unused_nread; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ธฐ์กด ์ฝ๋์์
nread == NULL
๊ฒ์ฌ ์์ด *nread= xxx;
์ฝ๋๊ฐ ์กด์ฌํ๋ค๋ ๊ฒ์
nread๊ฐ NULL์ธ ๊ฒฝ์ฐ๋ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
๋ฐ๋ผ์, memcached_io_read() ํธ์ถํ๋ ์ชฝ์ ๋ณ๊ฒฝ์ด ์๋ ์ํฉ์์
nread == NULL
์กฐ๊ฑด์ ๊ฒ์ฌํ ํ์๋ ์์ด ๋ณด์
๋๋ค.
6405f01
to
fbe9fc1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์๋ฃ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์๋ฃ
libmemcached/io.cc
Outdated
@@ -539,6 +539,7 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, | |||
} | |||
|
|||
char *buffer_ptr= static_cast<char *>(buffer); | |||
*nread= 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0์ผ๋ก ์ค์ ํ ํ์๊ฐ ์์ต๋๋ค.
libmemcached/io.cc
Outdated
if (buffer_ptr) { | ||
memcpy(buffer_ptr, ptr->read_ptr, difference); | ||
buffer_ptr+= difference; | ||
} | ||
|
||
length-= difference; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
length ๋ณ๊ฒฝ์ ์๋๋ก ๋ด๋ ค์ฃผ์ธ์.
์์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ๋ฐ๋๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ ๋์์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋์ ์์ ๋์๋์?
|
||
offset+= (size_t) nread; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ ๊ฒ ์์ ํด์ผ ํ๋ ์ด์ ๋ ๋ฌด์์ธ๊ฐ์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhpark816
ํ์ฌ ๋ณ๊ฒฝ ๋ฐฉ์์์ buffer(data) ์ธ์๊ฐ NULL์ธ ๊ฒฝ์ฐ, nread ๊ฐ์ 0์ด๊ฒ ๋ฉ๋๋ค.
๊ธฐ์กด safe_read๋ nread ๊ฒฐ๊ณผ ๊ฐ์ผ๋ก๋ถํฐ offset์ ๊ณ์ฐํ์ผ๋ฏ๋ก buffer๊ฐ NULL์ธ ์ํฉ์์
offset ๊ฐ์ด ์ฆ๊ฐํ์ง ์์ ๋ฌดํ๋ฃจํ์ ๋น ์ง๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ memcached_io_read์์ while ๋ฌธ์ ํตํด ์ง์ ๋ ๊ธธ์ด๋งํผ ๋ชจ๋ ์ฝ๋ ๊ฒ์ด ๋ณด์ฅ๋๋ฏ๋ก, memcached_safe_read์์๋ while (offset < size) ๋ฌธ์ ๋ ํ์๊ฐ ์์ด ์ ๊ฑฐํ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ ๋ณ๊ฒฝ ๋ฐฉ์์์ buffer(data) ์ธ์๊ฐ NULL์ธ ๊ฒฝ์ฐ, nread ๊ฐ์ 0์ด๊ฒ ๋ฉ๋๋ค.
์ด๋ค ์ฝ๋ ๋๋ฌธ์ nread ๊ฐ์ด 0์ด ๋๋์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ ๋ด์ฉ์ด ๋ฐ์๋์ง ์์ ๋ค์ ์ฌ๋ ธ์ต๋๋ค.
์ด๋ค ์ฝ๋ ๋๋ฌธ์ nread ๊ฐ์ด 0์ด ๋๋์?
*nread = (ssize_t)(buffer_ptr - (char*)buffer);
์ ์ํ์ฌ 0์ด ๋ฉ๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nread ์ ๋ณด๋ ์ ํํ ์ ๋ฌ๋์ด์ผ ํฉ๋๋ค.
๊ทธ๋์, ์์ PR์์ nread์๋ค ๋งค๋ฒ ์ฝ์ ํฌ๊ธฐ๋ฅผ ๋ํ ๊ฒ์ด๊ตฐ์.
nread์๋ค ๊ณ์ ๋ํ๋ ๊ฒ ๋ณด๋ค,
ํ์ฌ length ๊ฐ์ ๊ฐ์์ํค๊ณ ์์ผ๋,
- request = length; ๊ฐ์ ๊ฐ์ง๋ request ๋ณ์๋ฅผ ๋๊ณ ,
- ๋์ค์
*nread = request - length;
ํํ๋ก ์ค์ ํ๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ณ์ ๋ช
์ด ์กฐ๊ธ ์ด์ํ๋ค์.
length๋ฅผ ๊ทธ๋๋ก ๋๊ณ , ์์ฌํฌ๊ธฐ ์๋ฏธ์ ๋ณ์๋ฅผ ๋์ด readํ ๋๋ง๋ค ๊ฐ์์ํค๋ ๊ฒ์ด ๋์ ๊ฒ ๊ฐ๊ธฐ๋ ํฉ๋๋ค.
์ด์ ๋ํด ์๊ฒฌ ์ฃผ์ธ์.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ณ์ ๋ช ์ด ์กฐ๊ธ ์ด์ํ๋ค์.
length๋ฅผ ๊ทธ๋๋ก ๋๊ณ , ์์ฌํฌ๊ธฐ ์๋ฏธ์ ๋ณ์๋ฅผ ๋์ด readํ ๋๋ง๋ค ๊ฐ์์ํค๋ ๊ฒ์ด ๋์ ๊ฒ ๊ฐ๊ธฐ๋ ํฉ๋๋ค.
์ ๋ remain ๋๋ left๋ผ๋ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์์ฐ์ค๋ฌ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remains ๋ณ์๋ฅผ ์ฌ์ฉํ์์ฃ .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remain์ผ๋ก ๋จผ์ ๋ฐ๊ฟ์ remains๋ก ์ฌ๋ณ๊ฒฝํ์ฌ ๋ค์ push ํ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nread ๊ฐ์ ๋ณ๊ฒฝํ์ผ๋,
memcached_safe_read() ๋ณ๊ฒฝ์ ์์ด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer๊ฐ NULL์ผ ๊ฒฝ์ฐ ์๋ชป๋ ๊ณ์ฐ์ด ๋ฐ์ํ๋ฏ๋ก ์ผ๋ถ๋ ์์ ํ๊ฒ ์ต๋๋ค.
7047c10
to
9dc7b0d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์๋ฃ
libmemcached/io.cc
Outdated
if (buffer_ptr) { | ||
memcpy(buffer_ptr, ptr->read_ptr, difference); | ||
buffer_ptr+= difference; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๊ธฐ empty line์ ์ ๊ฑฐํด ์ฃผ์๊ณ ์.
if (buffer_ptr) { | ||
*buffer_ptr= *ptr->read_ptr; | ||
buffer_ptr++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋์์ remains--; ์ถ๊ฐํด์ผ ํฉ๋๋ค.
libmemcached/io.cc
Outdated
ptr->read_ptr++; | ||
ptr->read_buffer_length--; | ||
buffer_ptr++; | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reamins--; ์ถ๊ฐ๋๋ฉด, break ๋ฌธ์ ์์ด๋ ๋ฉ๋๋ค.
ptr->read_ptr+= difference; | ||
ptr->read_buffer_length-= difference; | ||
buffer_ptr+= difference; | ||
remains-= difference; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remains > 1 ๊ฒฝ์ฐ๊ฐ remains == 1์ธ ๊ฒฝ์ฐ๋ก ๊ตฌ๋ถ๋์ด ์๋ ๋ฐ,
remains > 1 ๊ฒฝ์ฐ์ ์ฝ๋๋ง ์์ด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remains > 1์ธ ๊ฒฝ์ฐ์ remains == 1์ ๊ตฌ๋ถํ๊ณ ์๋ ์ด์ ๊ฐ remains > 1์ธ ๊ฒฝ์ฐ memcpy๋ฅผ ์ฐ๊ณ , remains == 1์ธ ๊ฒฝ์ฐ ๋ฑํธ ์ฐ์ฐ์(=)๋ฅผ ์ฐ๋ ค๋ ์๋๋ก ๋ณด์ ๋๋ค.
๋ชจ๋ memcpy๋ก ํต์ผํ ๊น์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฐ์ ํด๋น ๋ถ๋ถ์ ๊ทธ๋๋ก ๋์์ต๋๋ค.
๋๋จธ์ง ์์ ๋์์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ง๋ฌธ์
๋๋ค.
remains == 1์ธ ๊ฒฝ์ฐ๊ฐ ๋ง์ ์ ์๋์?
๋ง์ง ์๋ค๋ฉด, ์๋ ์ฝ๋๋ง์ผ๋ก ์ถฉ๋ถํ ๊ฒ ๊ฐ์ต๋๋ค.
size_t difference= (remains > ptr->read_buffer_length) ? ptr->read_buffer_length : remains;
if (buffer_ptr) {
memcpy(buffer_ptr, ptr->read_ptr, difference);
buffer_ptr+= difference;
}
ptr->read_ptr+= difference;
ptr->read_buffer_length-= difference;
remains-= difference;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ด๋ถ ์ฝ๋์์ length๋ฅผ 1๋ก ๋๊ณ ํธ์ถํ๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
- memcached_io_readline:
- read_buffer๊ฐ ๋น์ด ์์ ๊ฒฝ์ฐ, memcached_io์ length ์ธ์์ 1์ ๋๊ฒจ read_buffer๋ฅผ ์ฑ์๋๋ค.
- fetch_value_header:
- ์ปฌ๋ ์
์์์ bkey, eflag ๋ฑ์ ์ฝ์ ๋, ํ๋์ฉ ์ฝ์ด๋ค์ด๋ฉฐ,
space
,\r
,\n
์ด ๋์ฌ ๋๊น์ง ๋ฐ๋ณตํฉ๋๋ค.
- ์ปฌ๋ ์
์์์ bkey, eflag ๋ฑ์ ์ฝ์ ๋, ํ๋์ฉ ์ฝ์ด๋ค์ด๋ฉฐ,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฝ์ด์ผ ํ ๊ธธ์ด๋ ๋ชจ๋ฅด๊ณ ,
๋จ์ง ํน์ ๋ฌธ์(\n
)๊ฐ ๋์ฌ ๋๊น์ง ์ฝ๊ธฐ ์ํจ์ด๊ตฐ์.
๊ทธ๋ฌ๋ฉด, remains == 1 ์ธ ๊ฒฝ์ฐ์ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ ์งํ์์ฃ .
local์์ ํ๋ฒ ์์ ํ ๋ ์ ์ฒด๋ฅผ ๋ค ์์ ํด ์ฃผ์๊ณ , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ ์๋ฃ
๐ Related Issue
โจ๏ธ What I did
unused_nread
๋ฅผ ํ์ฉํ๋๋ก ์ฒ๋ฆฌํ์ต๋๋ค.