Bug #114410 Code performance issue
Submitted: 19 Mar 2024 14:44 Modified: 4 Oct 2024 22:27
Reporter: jaesung woo Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.3.0 OS:Any
Assigned to: CPU Architecture:Any

[19 Mar 2024 14:44] jaesung woo
Description:
https://github.com/mysql/mysql-connector-j/blob/8.3.0/src/main/protocol-impl/java/com/mysq...

isTimestamp function creates Pattern instance which leads to cpu/memory waste while isTime doesnt

How to repeat:
don't need to repeat
[19 Mar 2024 15:10] MySQL Verification Team
Hi Mr. woo,

Thank you for your bug report.

Can you provide us with a snippet of the code that would enable us to repeat the problem ???

Also, please let us know that you have checked that performance drop is not due to Java's garbage collection.

Thanks in advance.
[19 Mar 2024 15:35] jaesung woo
Hi MySQL Verification Team, 

i think it's quite obvious that the code has performance issue because pattern compile occurs all the time when the method is called. 

you can fix this and also leave this as it is. it's your choice 

thank you
[19 Mar 2024 15:37] jaesung woo
```kt
@Test
fun test() {
    val string = "1234"
    val repeatTime = 1_000_000
    // warmup
    measureTimeMillis {
        repeat(1000) {
            MysqlTextValueDecoder.isTimestamp(string)
            MysqlTextValueDecoder.isTime(string)
        }
    }

    val isTimeElapsed = measureTimeMillis {
        repeat(repeatTime) {
            MysqlTextValueDecoder.isTime(string)
        }
    }
    val isTimestampElapsed = measureTimeMillis {
        repeat(repeatTime) {
            MysqlTextValueDecoder.isTimestamp(string)
        }
    }

    println("isTimeElapsed = $isTimeElapsed ms")
    println("isTimestampElapsed = $isTimestampElapsed ms")
}
```

i just did this and the result as follows: 
isTimeElapsed = 102 ms
isTimestampElapsed = 792 ms
[22 Mar 2024 13:21] MySQL Verification Team
Hello Jaesung Woo,

Thank you for the details.
Verified as described.

Regards,
Ashwini Patil
[4 Oct 2024 22:27] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 9.1.0 changelog: 

"isTimestamp() no longer creates a Pattern when it is called, which leads to a better code performance."
[12 Oct 2024 0:36] Daniel So
Posted by developer:
 
Updated the changelog entry to the following: 

"isTimestamp() no longer creates a regular expression Pattern instance when it is called, which leads to better code performance."