Skip to content

Commit

Permalink
More performance improvements.
Browse files Browse the repository at this point in the history
Removed use of unique_ptr for stored stmt, rs and rs metadata objects
Slightly opotimized MADB_DescGetInternalRecord
  • Loading branch information
lawrinn committed Nov 28, 2024
1 parent d517333 commit d9b0c1d
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 136 deletions.
2 changes: 1 addition & 1 deletion driver/class/ClientSidePreparedStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace mariadb
if (!metadata) {
loadParametersData();
}
return metadata.get();
return metadata.release();
}


Expand Down
2 changes: 1 addition & 1 deletion driver/ma_bulk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ SQLRETURN MADB_ExecuteBulk(MADB_Stmt *Stmt, unsigned int ParamOffset)

if (Stmt->stmt->isServerSide() && !MADB_ServerSupports(Stmt->Connection, MADB_CAPABLE_PARAM_ARRAYS))
{
Stmt->stmt.reset(new ClientSidePreparedStatement(Stmt->Connection->guard.get(), STMT_STRING(Stmt), Stmt->Options.CursorType
MADB_CXX_RESET(Stmt->stmt, new ClientSidePreparedStatement(Stmt->Connection->guard.get(), STMT_STRING(Stmt), Stmt->Options.CursorType
, Stmt->Query.NoBackslashEscape));
// So far
useCallbacks= false;
Expand Down
37 changes: 20 additions & 17 deletions driver/ma_desc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ MADB_DescSetIrdMetadata(MADB_Stmt *Stmt, const MYSQL_FIELD *Fields, unsigned int
{
return 1;
}
Stmt->Ird->Header.Count= i + 1;
}
return 0;
}
Expand Down Expand Up @@ -638,32 +639,34 @@ void MADB_DescSetRecordDefaults(MADB_Desc *Desc, MADB_DescRecord *Record)
}
/* }}} */

/* {{{ MADB_DescGetInternalRecord */
MADB_DescRecord *MADB_DescGetInternalRecord(MADB_Desc *Desc, SQLSMALLINT RecordNumber, SQLSMALLINT Type)
/* {{{ MADB_DescGetInternalRecord
[in] ReorordNumber - 0 based record index */
MADB_DescRecord* MADB_DescGetInternalRecord(MADB_Desc *Desc, SQLSMALLINT RecordNumber, SQLSMALLINT Type)
{
MADB_DescRecord *DescRecord;

if (RecordNumber > (SQLINTEGER)Desc->Records.elements &&
Type == MADB_DESC_READ)
if (RecordNumber >= (SQLINTEGER)Desc->Records.elements)
{
MADB_SetError(&Desc->Error, MADB_ERR_07009, nullptr, 0);
return nullptr;
}

while (RecordNumber >= (SQLINTEGER)Desc->Records.elements)
{
if (!(DescRecord= (MADB_DescRecord *)MADB_AllocDynamic(&Desc->Records)))
if (Type == MADB_DESC_READ)
{
MADB_SetError(&Desc->Error, MADB_ERR_HY001, nullptr, 0);
MADB_SetError(&Desc->Error, MADB_ERR_07009, nullptr, 0);
return nullptr;
}

MADB_DescSetRecordDefaults(Desc, DescRecord);

do {
if (!(DescRecord= (MADB_DescRecord *)MADB_AllocDynamic(&Desc->Records)))
{
MADB_SetError(&Desc->Error, MADB_ERR_HY001, nullptr, 0);
return nullptr;
}

MADB_DescSetRecordDefaults(Desc, DescRecord);
} while (RecordNumber >= (SQLINTEGER)Desc->Records.elements);

if (RecordNumber + 1 > Desc->Header.Count)
Desc->Header.Count= (SQLSMALLINT)(RecordNumber + 1);
}

if (RecordNumber + 1 > Desc->Header.Count)
Desc->Header.Count= (SQLSMALLINT)(RecordNumber + 1);

DescRecord= ((MADB_DescRecord *)Desc->Records.buffer) + RecordNumber;

return DescRecord;
Expand Down
11 changes: 6 additions & 5 deletions driver/ma_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@ MYSQL_RES *MADB_GetDefaultColumnValues(MADB_Stmt *Stmt, const MYSQL_FIELD *field

for (i= 0; i < Stmt->metadata->getColumnCount(); i++)
{
// TODO: Shouldn't it be really IRD here?
MADB_DescRecord* Rec= MADB_DescGetInternalRecord(Stmt->Ard, i, MADB_DESC_READ);

if (!Rec->inUse || MADB_ColumnIgnoredInAllRows(Stmt->Ard, Rec) == TRUE)
if (!Rec || !Rec->inUse || MADB_ColumnIgnoredInAllRows(Stmt->Ard, Rec) == TRUE)
{
continue;
}
Expand Down Expand Up @@ -864,7 +865,7 @@ void* GetBindOffset(MADB_Header& Header, void* Ptr, SQLULEN RowNumber, std::size
}

/* Checking if column ignored in all bound rows. Should hel*/
BOOL MADB_ColumnIgnoredInAllRows(MADB_Desc *Desc, MADB_DescRecord *Rec)
bool MADB_ColumnIgnoredInAllRows(MADB_Desc *Desc, MADB_DescRecord *Rec)
{
SQLULEN row;
SQLLEN *IndicatorPtr;
Expand All @@ -873,13 +874,13 @@ BOOL MADB_ColumnIgnoredInAllRows(MADB_Desc *Desc, MADB_DescRecord *Rec)
{
IndicatorPtr= (SQLLEN *)GetBindOffset(Desc->Header, Rec->IndicatorPtr, row, sizeof(SQLLEN));

if (IndicatorPtr == NULL || *IndicatorPtr != SQL_COLUMN_IGNORE)
if (IndicatorPtr == nullptr || *IndicatorPtr != SQL_COLUMN_IGNORE)
{
return FALSE;
return false;
}
}

return TRUE;
return true;
}


Expand Down
5 changes: 4 additions & 1 deletion driver/ma_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const char * MADB_GetTypeName(const MYSQL_FIELD *Field);
my_bool MADB_CheckPtrLength(SQLINTEGER MaxLength, char *Ptr, SQLINTEGER NameLen);
std::size_t getArrayStep(MADB_Header& header, std::size_t pointedSize);
void * GetBindOffset(MADB_Header& DescHeader, void *Ptr, SQLULEN RowNumber, size_t PtrSize);
BOOL MADB_ColumnIgnoredInAllRows(MADB_Desc *Desc, MADB_DescRecord *Rec);
bool MADB_ColumnIgnoredInAllRows(MADB_Desc *Desc, MADB_DescRecord *Rec);

SQLRETURN MADB_DaeStmt(MADB_Stmt *Stmt, SQLUSMALLINT Operation);
MYSQL_RES * MADB_GetDefaultColumnValues(MADB_Stmt *Stmt, const MYSQL_FIELD *fields);
Expand Down Expand Up @@ -109,4 +109,7 @@ extern my_bool DummyError;

#define MADB_FRACTIONAL_PART(_decimals) ((_decimals) > 0 ? (_decimals) + 1/*Decimal point*/ : 0)

#define MADB_DELETE(PTR) do {delete (PTR); (PTR)= nullptr;} while(false)
#define MADB_CXX_RESET(PTR,NEWPTR) do {delete (PTR); (PTR)= (NEWPTR);} while(false)

#endif
6 changes: 3 additions & 3 deletions driver/ma_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ SQLRETURN MADB_GetTypeInfo(SQLHSTMT StatementHandle,

std::vector<std::vector<mariadb::bytes_view>> row;

Stmt->stmt.reset();
MADB_DELETE(Stmt->stmt);
if (DataType == SQL_ALL_TYPES)
{
Stmt->rs.reset(ResultSet::createResultSet(TypeInfoColumnName, TypeInfoColumnType, *TypeInfo));
MADB_CXX_RESET(Stmt->rs, ResultSet::createResultSet(TypeInfoColumnName, TypeInfoColumnType, *TypeInfo));
}
else
{
Expand All @@ -262,7 +262,7 @@ SQLRETURN MADB_GetTypeInfo(SQLHSTMT StatementHandle,
row.push_back(cit);
}
}
Stmt->rs.reset(ResultSet::createResultSet(TypeInfoColumnName, TypeInfoColumnType, row));
MADB_CXX_RESET(Stmt->rs, ResultSet::createResultSet(TypeInfoColumnName, TypeInfoColumnType, row));
}

Stmt->State= MADB_SS_EXECUTED;
Expand Down
6 changes: 3 additions & 3 deletions driver/ma_odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ struct MADB_Stmt
long long AffectedRows= 0;
MADB_Dbc *Connection;
struct st_ma_stmt_methods *Methods;
Unique::ResultSet rs;
Unique::PreparedStatement stmt;
Unique::ResultSetMetaData metadata;
ResultSet* rs= nullptr;
PreparedStatement* stmt= nullptr;
mariadb::ResultSetMetaData* metadata= nullptr;
Unique::MYSQL_RES DefaultsResult;
MADB_DescRecord *PutDataRec= nullptr;
MADB_Stmt *DaeStmt= nullptr;
Expand Down
4 changes: 1 addition & 3 deletions driver/ma_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void MADB_QUERY::reset()
Original.assign("");
RefinedText.assign("");
Tokens.clear();
PoorManParsing= ReturnsResult= false;
PoorManParsing= false;
}

int MADB_ParseQuery(MADB_QUERY * Query)
Expand Down Expand Up @@ -397,8 +397,6 @@ int ParseQuery(MADB_QUERY *Query)
/* We are currently at 2nd token of statement, and getting previous token position from Tokens array*/
StmtType= MADB_GetQueryType(MADB_Token(Query, Query->Tokens.size() - 2), p);

Query->ReturnsResult= Query->ReturnsResult || !QUERY_DOESNT_RETURN_RESULT(StmtType);

/* If we on first statement, setting QueryType*/
if (Query->Tokens.size() == 2)
{
Expand Down
2 changes: 0 additions & 2 deletions driver/ma_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ typedef struct {
SQLString RefinedText;
enum enum_madb_query_type QueryType= MADB_QUERY_NO_RESULT;
bool MultiStatement= false;
/* Keeping it so far */
bool ReturnsResult= false;
bool PoorManParsing= false;

bool BatchAllowed= false;
Expand Down
8 changes: 4 additions & 4 deletions driver/ma_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ SQLRETURN MADB_StmtMoreResults(SQLHSTMT StatementHandle)
/* We can't have it in MADB_StmtResetResultStructures, as it breaks dyn_cursor functionality.
Thus we free-ing bind structs on move to new result only */
MADB_FREE(Stmt->result);
Stmt->metadata.reset();
Stmt->rs.reset();
MADB_DELETE(Stmt->metadata);
MADB_DELETE(Stmt->rs);

try {
// TODO: that's not right to mess here with Protocol's lock. Protocol should take care of that
Expand All @@ -163,7 +163,7 @@ SQLRETURN MADB_StmtMoreResults(SQLHSTMT StatementHandle)
{
unsigned int ServerStatus;
mariadb_get_infov(Stmt->Connection->mariadb, MARIADB_CONNECTION_SERVER_STATUS, (void*)&ServerStatus);
Stmt->rs.reset(Stmt->stmt->getResultSet());
MADB_CXX_RESET(Stmt->rs, Stmt->stmt->getResultSet());
bool itsOutParams= ServerStatus & SERVER_PS_OUT_PARAMS;
bool haveOutParams= HasOutParams(Stmt);

Expand Down Expand Up @@ -198,7 +198,7 @@ SQLRETURN MADB_StmtMoreResults(SQLHSTMT StatementHandle)
ret= MADB_FromException(Stmt->Error, e);
}
catch (int32_t /*rc*/) {
ret= MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt.get());
ret= MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
}

MADB_StmtResetResultStructures(Stmt);
Expand Down
Loading

0 comments on commit d9b0c1d

Please sign in to comment.