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

INTERNAL: Refactor the memcached_io_read function #341

Merged
merged 1 commit into from
Jan 22, 2025
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
30 changes: 17 additions & 13 deletions libmemcached/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
}

char *buffer_ptr= static_cast<char *>(buffer);
while (length)
size_t remains= length;
while (remains)
{
if (ptr->read_buffer_length == 0)
{
Expand All @@ -551,27 +552,30 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
return io_fill_ret;
}
}
if (length > 1)
if (remains > 1)
{
size_t difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
memcpy(buffer_ptr, ptr->read_ptr, difference);

length-= difference;
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;
buffer_ptr+= difference;
remains-= difference;
Copy link
Contributor

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 ๊ฒฝ์šฐ์˜ ์ฝ”๋“œ๋งŒ ์žˆ์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

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๋กœ ํ†ต์ผํ• ๊นŒ์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์šฐ์„  ํ•ด๋‹น ๋ถ€๋ถ„์€ ๊ทธ๋Œ€๋กœ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋จธ์ง€ ์ˆ˜์ •๋˜์—‡์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

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;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋‚ด๋ถ€ ์ฝ”๋“œ์—์„œ length๋ฅผ 1๋กœ ๋‘๊ณ  ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. memcached_io_readline:
    • read_buffer๊ฐ€ ๋น„์–ด ์žˆ์„ ๊ฒฝ์šฐ, memcached_io์˜ length ์ธ์ž์— 1์„ ๋„˜๊ฒจ read_buffer๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค.
  2. fetch_value_header:
    • ์ปฌ๋ ‰์…˜ ์š”์†Œ์˜ bkey, eflag ๋“ฑ์„ ์ฝ์„ ๋•Œ, ํ•˜๋‚˜์”ฉ ์ฝ์–ด๋“ค์ด๋ฉฐ, space, \r, \n์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

์ฝ์–ด์•ผ ํ•  ๊ธธ์ด๋Š” ๋ชจ๋ฅด๊ณ ,
๋‹จ์ง€ ํŠน์ • ๋ฌธ์ž(\n)๊ฐ€ ๋‚˜์˜ฌ ๋–„๊นŒ์ง€ ์ฝ๊ธฐ ์œ„ํ•จ์ด๊ตฐ์š”.

๊ทธ๋Ÿฌ๋ฉด, remains == 1 ์ธ ๊ฒฝ์šฐ์˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜์‹œ์ฃ .

}
else
{
*buffer_ptr= *ptr->read_ptr;
if (buffer_ptr) {
*buffer_ptr= *ptr->read_ptr;
buffer_ptr++;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

์•„๋ž˜์—์„œ remains--; ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ptr->read_ptr++;
ptr->read_buffer_length--;
buffer_ptr++;
break;
remains--;
}
}

*nread = (ssize_t)(buffer_ptr - (char*)buffer);
*nread = (ssize_t)(length - remains);

return MEMCACHED_SUCCESS;
}
Expand Down Expand Up @@ -855,16 +859,16 @@ memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
ssize_t nread;
memcached_return_t rc;

while (memcached_continue(rc= memcached_io_read(ptr, data + offset, size - offset, &nread))) { };
while (memcached_continue(rc= memcached_io_read(ptr, data, size - offset, &nread))) { };

if (memcached_failed(rc))
{
return rc;
}

offset+= (size_t) nread;
Copy link
Contributor

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?

Copy link
Collaborator Author

@ing-eoking ing-eoking Jan 22, 2025

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) ๋ฌธ์„ ๋‘˜ ํ•„์š”๊ฐ€ ์—†์–ด ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ing-eoking

ํ˜„์žฌ ๋ณ€๊ฒฝ ๋ฐฉ์‹์—์„œ buffer(data) ์ธ์ž๊ฐ€ NULL์ธ ๊ฒฝ์šฐ, nread ๊ฐ’์€ 0์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์–ด๋–ค ์ฝ”๋“œ ๋•Œ๋ฌธ์— nread ๊ฐ’์ด 0์ด ๋˜๋‚˜์š”?

Copy link
Collaborator Author

@ing-eoking ing-eoking Jan 22, 2025

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์ด ๋ฉ๋‹ˆ๋‹ค.

Copy link
Contributor

@jhpark816 jhpark816 Jan 22, 2025

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ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ด์— ๋Œ€ํ•ด ์˜๊ฒฌ ์ฃผ์„ธ์š”.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋ณ€์ˆ˜ ๋ช…์ด ์กฐ๊ธˆ ์–ด์ƒ‰ํ•˜๋„ค์š”.
length๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ์ž”์—ฌํฌ๊ธฐ ์˜๋ฏธ์˜ ๋ณ€์ˆ˜๋ฅผ ๋‘์–ด readํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ €๋Š” remain ๋˜๋Š” left๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ž์—ฐ์Šค๋Ÿฌ์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

remains ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์‹œ์ฃ .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

remain์œผ๋กœ ๋จผ์ € ๋ฐ”๊ฟ”์„œ remains๋กœ ์žฌ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค์‹œ push ํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

nread ๊ฐ’์„ ๋ณ€๊ฒฝํ–ˆ์œผ๋‹ˆ,
memcached_safe_read() ๋ณ€๊ฒฝ์€ ์—†์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

buffer๊ฐ€ NULL์ผ ๊ฒฝ์šฐ ์ž˜๋ชป๋œ ๊ณ„์‚ฐ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ผ๋ถ€๋Š” ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

if (data) data+= nread;
}

return MEMCACHED_SUCCESS;
}

Expand Down
Loading