go-sql-driver/mysql — pull request #55 by JulienSchmidt (2013-04-21 21:03:45+00:00)
Title: zero-copy buffer

Continued from #52

This approach removes the 2nd buffer (date / packet buffer). Instead we work with slices directly from the buffer. This results in more or less *zero copy*.

**Performance:**
Before
```

*************************************************************
BENCHMARKING Go-MySQL-Driver [run 1]
*************************************************************

-------------------------------------------------------------
[10000 * Query 100 Rows]
-------------------------------------------------------------
SimpleQuery: 2.1241215s [ 4708 queries/second ]
PreparedQuery: 1.854106s [ 5393 queries/second ]

-------------------------------------------------------------
[100 * QueryRow] * 1000
-------------------------------------------------------------
AutoQueryRow: 13.3857656s [ 7471 queries/second ]
SimpleQueryRow: 7.2264133s [ 13838 queries/second ]
PreparedQueryRow: 6.8483917s [ 14602 queries/second ]

-------------------------------------------------------------
[100000 * Exec]
-------------------------------------------------------------
SimpleExec: 3.6992116s [ 27033 queries/second ]
PreparedExec: 3.4701985s [ 28817 queries/second ]

*************************************************************
BENCHMARKING Go-MySQL-Driver [run 2]
*************************************************************

-------------------------------------------------------------
[10000 * Query 100 Rows]
-------------------------------------------------------------
SimpleQuery: 2.3471342s [ 4261 queries/second ]
PreparedQuery: 1.871107s [ 5344 queries/second ]

-------------------------------------------------------------
[100 * QueryRow] * 1000
-------------------------------------------------------------
AutoQueryRow: 13.5567754s [ 7376 queries/second ]
SimpleQueryRow: 7.3384198s [ 13627 queries/second ]
PreparedQueryRow: 6.7943886s [ 14718 queries/second ]

-------------------------------------------------------------
[100000 * Exec]
-------------------------------------------------------------
SimpleExec: 3.6942113s [ 27069 queries/second ]
PreparedExec: 3.5642039s [ 28057 queries/second ]
```

After
```
*************************************************************
BENCHMARKING Go-MySQL-Driver [run 1]
*************************************************************

-------------------------------------------------------------
[10000 * Query 100 Rows]
-------------------------------------------------------------
SimpleQuery: 1.9131094s [ 5227 queries/second ]
PreparedQuery: 1.555089s [ 6431 queries/second ]

-------------------------------------------------------------
[100 * QueryRow] * 1000
-------------------------------------------------------------
AutoQueryRow: 13.1377515s [ 7612 queries/second ]
SimpleQueryRow: 6.8833938s [ 14528 queries/second ]
PreparedQueryRow: 6.4733703s [ 15448 queries/second ]

-------------------------------------------------------------
[100000 * Exec]
-------------------------------------------------------------
SimpleExec: 3.6292076s [ 27554 queries/second ]
PreparedExec: 3.5092007s [ 28497 queries/second ]

*************************************************************
BENCHMARKING Go-MySQL-Driver [run 2]
*************************************************************

-------------------------------------------------------------
[10000 * Query 100 Rows]
-------------------------------------------------------------
SimpleQuery: 1.8621065s [ 5370 queries/second ]
PreparedQuery: 1.5790903s [ 6333 queries/second ]

-------------------------------------------------------------
[100 * QueryRow] * 1000
-------------------------------------------------------------
AutoQueryRow: 13.2367571s [ 7555 queries/second ]
SimpleQueryRow: 6.9683986s [ 14350 queries/second ]
PreparedQueryRow: 6.4303678s [ 15551 queries/second ]

-------------------------------------------------------------
[100000 * Exec]
-------------------------------------------------------------
SimpleExec: 3.6432084s [ 27448 queries/second ]
PreparedExec: 3.4141953s [ 29289 queries/second ]
```

I worked on such a "zero copy buffer" before but stopped when I found a reason why this concept couldn't work - at least I assumed it the be a reason.
Until now I found no case where the new buffer causes problems, but **please test it as much as you can**.
Especially cases where `sql.RawBytes` is used excessively could be interesting.