Re: [PATCH 3/4] MDEV-31273: Refactor MYSQL_BIN_LOG::write_cache()
![](https://secure.gravatar.com/avatar/68cce0b6e372c2c66f4d66c6356102af.jpg?s=120&d=mm&r=g)
Hi!
On Fri, Aug 25, 2023 at 10:16 AM Kristian Nielsen
Preparatory patch for pre-computing binlog checksums outside of holding LOCK_log.
The existing code for MYSQL_BIN_LOG::write_cache() was needlessly complex and very hard to understand and modify for handling the new case where pre-computed checksums are already present in the IO_CACHE.
MDEV-31273: Refactor MYSQL_BIN_LOG::write_cache()
if (likely(length > LOG_EVENT_HEADER_LEN)) { header= cache->read_pos; cache->read_pos+= LOG_EVENT_HEADER_LEN; length-= LOG_EVENT_HEADER_LEN; } else { size_t sofar= length; size_t remain= LOG_EVENT_HEADER_LEN - sofar; header= &header_buf[0]; memcpy(header, cache->read_pos, sofar); cache->read_pos+= sofar;
while (hdr_offs < length) length= my_b_fill(cache); if (!length) goto error_in_read; size_t chunk= std::min(length, remain); memcpy(header + sofar, cache->read_pos, chunk); sofar+= chunk; remain-= chunk; cache->read_pos+= chunk; length-= chunk; } while (unlikely(remain > 0)); }
The above can be replaced with: if (my_b_read(cache, header, LOG_EVENT_HEAD_LENGTH)) goto error_in_read; It is almost exactly as efficent as the above (one extra if) and avoids using cache internals. Note that you do not have to do call my_fill() if you use my_b_read(). my_b_read() will return 0 if it was able to read all data. In case of end of file, my_b_read() will return 1 and info->error will be 0. If needed, you can als find out how much data left to read from IO_CACHE: left_data_to_read= (cache->end_of_file - my_b_tell(cache)) (I can make an inline function of that if needed). /* Write the rest of the event. */
while (ev_len > 0) { if (length == 0) length= my_b_fill(cache); if (!length) goto error_in_read;
-> while (ev_len > 0) { if (length == 0) { if (!(length= my_b_fill(cache))); goto error_in_read; } <cut> uint chunk= std::min(ev_len, (uint)length); I would have prefer to have MY_MIN() used (like the rest of the code). (not critical) Regards, Monty
![](https://secure.gravatar.com/avatar/99fde0c1dfd216326aae0aff30d493f1.jpg?s=120&d=mm&r=g)
Michael Widenius via developers
MDEV-31273: Refactor MYSQL_BIN_LOG::write_cache()
if (likely(length > LOG_EVENT_HEADER_LEN))
<snip>
The above can be replaced with:
if (my_b_read(cache, header, LOG_EVENT_HEAD_LENGTH)) goto error_in_read;
Agree, that's better, done.
If needed, you can als find out how much data left to read from IO_CACHE:
left_data_to_read= (cache->end_of_file - my_b_tell(cache))
Yes, used that instead.
/* Write the rest of the event. */
while (ev_len > 0) { if (length == 0) length= my_b_fill(cache); if (!length) goto error_in_read;
-> while (ev_len > 0) { if (length == 0) { if (!(length= my_b_fill(cache))); goto error_in_read; }
Done.
uint chunk= std::min(ev_len, (uint)length);
I would have prefer to have MY_MIN() used (like the rest of the code).
Done. Thanks for review, good that you spottet the simplification of simply using my_b_read(). - Kristian.
participants (2)
-
Kristian Nielsen
-
Michael Widenius