Bug #71621 MysqlXAConnection#xidToString(Xid xid) produces too much garbage
Submitted: 7 Feb 2014 11:59 Modified: 10 Mar 2014 23:14
Reporter: Andrej Golovnin (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S5 (Performance)
Version:5.1.29 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any
Tags: jdbc, xa, xid

[7 Feb 2014 11:59] Andrej Golovnin
Description:
The method MysqlXAConnection#xidToString(Xid xid) uses Integer#toHexString(int) for every single byte of Xid#getGlobalTransactionId(), Xid#getBranchQualifier() and Xid#getFormatId() to convert them to a hex string. Integer#toHexString(int) creates first a temp array with the length of 32 characters and then at the end it creates a String object which references a char array withe length of 2. Additionally MysqlXAConnection#xidToString(Xid xid) creates its own instance of StringBuffer instead of reusing the one created in the methods MysqlXAConnection#prepare(Xid xid), #rollback(Xid xid), #end(Xid xid, int flags), #start(Xid xid, int flags), #commit(Xid xid, boolean onePhase).

How to repeat:
Run a lot of XA transactions and use a profiler to see how many temp character arrays are created during the transactions.

Suggested fix:
Use a custom code, which avoids creation of temp character array, to convert a byte to a hex string.
[7 Feb 2014 12:00] Andrej Golovnin
Patch which fixes this issue.

Attachment: MysqlXAConnection.patch (application/octet-stream, text), 5.02 KiB.

[11 Feb 2014 10:31] Filipe Silva
Hi Andrej,

Thank you for this bug report. Verified by code review.
[12 Feb 2014 6:49] Alexander Soklakov
Hi Andrej,

Thank you for contribution, just please confirm that it is submitted under the terms of the OCA.
[12 Feb 2014 7:01] Andrej Golovnin
Hi Alexander,

I have signed OCA for OpenJDK some time ago. Let me know, if I should sign a new OCA for MySQL Connector/J contributions.  And I confirm that all my contributions are submitted under the terms of the OCA.

Best regards,
Andrej Golovnin
[18 Feb 2014 13:50] Andrej Golovnin
Patch for this issue.

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: MysqlXAConnection.patch (application/octet-stream, text), 5.02 KiB.

[21 Feb 2014 11:03] Alexander Soklakov
Thanks Andrej! Accepted.
[10 Mar 2014 23:14] Daniel So
Added the following entry to the Connector/J 5.1.30 changelog:

"Improved on the code for integer-to-hex conversion when building XA commands by avoiding the creation of temporary character arrays, thus enhancing performance."