Skip to content

Commit

Permalink
Optimize table (#3959)
Browse files Browse the repository at this point in the history
* optimize code

* optimize code[2]

* fix typo
  • Loading branch information
matyhtf authored Dec 27, 2020
1 parent db56827 commit 1e10454
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
45 changes: 19 additions & 26 deletions ext-src/swoole_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,20 @@ using namespace swoole;
static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval *return_value) {
array_init(return_value);

TableStringLength vlen = 0;
double dval = 0;
long lval = 0;

for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) {
TableColumn *col = *i;
if (col->type == TableColumn::TYPE_STRING) {
memcpy(&vlen, row->data + col->index, sizeof(TableStringLength));
add_assoc_stringl_ex(return_value,
col->name.c_str(),
col->name.length(),
row->data + col->index + sizeof(TableStringLength),
vlen);
TableStringLength len = 0;
char *str = nullptr;
row->get_value(col, &str, &len);
add_assoc_stringl_ex(return_value, col->name.c_str(), col->name.length(), str, len);
} else if (col->type == TableColumn::TYPE_FLOAT) {
memcpy(&dval, row->data + col->index, sizeof(dval));
double dval = 0;
row->get_value(col, &dval);
add_assoc_double_ex(return_value, col->name.c_str(), col->name.length(), dval);
} else if (col->type == TableColumn::TYPE_INT) {
memcpy(&lval, row->data + col->index, sizeof(lval));
long lval = 0;
row->get_value(col, &lval);
add_assoc_long_ex(return_value, col->name.c_str(), col->name.length(), lval);
} else {
abort();
Expand All @@ -50,23 +46,24 @@ static inline void php_swoole_table_row2array(Table *table, TableRow *row, zval

static inline void php_swoole_table_get_field_value(
Table *table, TableRow *row, zval *return_value, char *field, uint16_t field_len) {
TableStringLength vlen = 0;
double dval = 0;
long lval = 0;

TableColumn *col = table->get_column(std::string(field, field_len));
if (!col) {
ZVAL_FALSE(return_value);
return;
}
if (col->type == TableColumn::TYPE_STRING) {
memcpy(&vlen, row->data + col->index, sizeof(TableStringLength));
ZVAL_STRINGL(return_value, row->data + col->index + sizeof(TableStringLength), vlen);
TableStringLength len = 0;
char *str = nullptr;
row->get_value(col, &str, &len);
ZVAL_STRINGL(return_value, str, len);
} else if (col->type == TableColumn::TYPE_FLOAT) {
memcpy(&dval, row->data + col->index, sizeof(dval));
double dval = 0;
row->get_value(col, &dval);
add_assoc_double_ex(return_value, col->name.c_str(), col->name.length(), dval);
ZVAL_DOUBLE(return_value, dval);
} else if (col->type == TableColumn::TYPE_INT) {
memcpy(&lval, row->data + col->index, sizeof(lval));
long lval = 0;
row->get_value(col, &lval);
ZVAL_LONG(return_value, lval);
} else {
abort();
Expand Down Expand Up @@ -515,9 +512,7 @@ static PHP_METHOD(swoole_table, incr) {
}

if (out_flags & SW_TABLE_FLAG_NEW_ROW) {
for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) {
(*i)->clear(row);
}
table->clear_row(row);
}

if (column->type == TableColumn::TYPE_STRING) {
Expand Down Expand Up @@ -577,9 +572,7 @@ static PHP_METHOD(swoole_table, decr) {
}

if (out_flags & SW_TABLE_FLAG_NEW_ROW) {
for (auto i = table->column_list->begin(); i != table->column_list->end(); i++) {
(*i)->clear(row);
}
table->clear_row(row);
}

if (column->type == TableColumn::TYPE_STRING) {
Expand Down
9 changes: 9 additions & 0 deletions include/swoole_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct TableRow {
}

void set_value(TableColumn *col, void *value, size_t vlen);
void get_value(TableColumn *col, double *dval);
void get_value(TableColumn *col, long *lval);
void get_value(TableColumn *col, char **strval, TableStringLength *strlen);
};

struct TableIterator {
Expand Down Expand Up @@ -216,6 +219,12 @@ class Table {
}
}

void clear_row(TableRow *row) {
for (auto i = column_list->begin(); i != column_list->end(); i++) {
(*i)->clear(row);
}
}

void init_row(TableRow *new_row, const char *key, int keylen) {
sw_memset_zero(new_row, sizeof(TableRow));
memcpy(new_row->key, key, keylen);
Expand Down
13 changes: 13 additions & 0 deletions src/memory/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,17 @@ void TableRow::set_value(TableColumn *col, void *value, size_t vlen) {
}
}

void TableRow::get_value(TableColumn *col, double *dval) {
memcpy(dval, data + col->index, sizeof(*dval));
}

void TableRow::get_value(TableColumn *col, long *lval) {
memcpy(lval, data + col->index, sizeof(*lval));
}

void TableRow::get_value(TableColumn *col, char **value, TableStringLength *len) {
memcpy(len, data + col->index, sizeof(*len));
*value = data + col->index + sizeof(*len);
}

} // namespace swoole

0 comments on commit 1e10454

Please sign in to comment.