Skip to content

Commit 2dfe1f2

Browse files
committed
Perf: Do not recreate Cell entity just for formatting value
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent 7bbc382 commit 2dfe1f2

File tree

6 files changed

+24
-44
lines changed

6 files changed

+24
-44
lines changed

lib/Db/Row2Mapper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,10 @@ private function parseEntities(IResult $result, array $sleeves): array {
653653

654654
$column = $this->columnMapper->find($rowData['column_id']);
655655
$columnType = $column->getType();
656-
$cellClassName = 'OCA\Tables\Db\RowCell' . ucfirst($columnType);
657-
$entity = call_user_func($cellClassName . '::fromRowData', $rowData); // >5.2.3
658656
if (!isset($cellMapperCache[$columnType])) {
659657
$cellMapperCache[$columnType] = $this->getCellMapperFromType($columnType);
660658
}
661-
$value = $cellMapperCache[$columnType]->formatEntity($column, $entity);
659+
$value = $cellMapperCache[$columnType]->formatRowData($column, $rowData);
662660
$compositeKey = (string)$rowData['row_id'] . ',' . (string)$rowData['column_id'];
663661
if ($cellMapperCache[$columnType]->hasMultipleValues()) {
664662
if (array_key_exists($compositeKey, $rowValues)) {

lib/Db/RowCellMapperSuper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public function __construct(IDBConnection $db, string $table, string $class) {
2727
}
2828

2929
/**
30-
* Format a row cell entity to API response array
30+
* Format a row cell raw value from DB to API response array
3131
*
32-
* @param T $cell
32+
* @param array<string, mixed> $row
3333
* @return TOutgoing
3434
*/
35-
public function formatEntity(Column $column, RowCellSuper $cell) {
35+
public function formatRowData(Column $column, array $row) {
3636
/** @var TOutgoing $value */
37-
$value = $cell->getValue();
37+
$value = $row['value'];
3838
return $value;
3939
}
4040
/*

lib/Db/RowCellNumberMapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public function __construct(IDBConnection $db) {
2020
parent::__construct($db, $this->table, RowCellNumber::class);
2121
}
2222

23-
public function formatEntity(Column $column, RowCellSuper $cell) {
24-
$value = $cell->getValue();
23+
public function formatRowData(Column $column, array $row) {
24+
$value = $row['value'];
2525
if ($value === '') {
2626
return null;
2727
}
2828
$decimals = $column->getNumberDecimals() ?? 0;
2929
if ($decimals === 0) {
3030
return (int)$value;
31-
} else {
32-
return round(floatval($value), $decimals);
3331
}
32+
33+
return round(floatval($value), $decimals);
3434
}
3535

3636
public function applyDataToEntity(Column $column, RowCellSuper $cell, $data): void {

lib/Db/RowCellSelectionMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function applyDataToEntity(Column $column, RowCellSuper $cell, $data): vo
2929
$cell->setValue($this->valueToJsonDbValue($column, $data));
3030
}
3131

32-
public function formatEntity(Column $column, RowCellSuper $cell) {
33-
return json_decode($cell->getValue());
32+
public function formatRowData(Column $column, array $row) {
33+
return json_decode($row['value']);
3434
}
3535

3636
private function valueToJsonDbValue(Column $column, $value): string {

lib/Db/RowCellSuper.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,9 @@ public function __construct() {
3737
$this->addType('rowId', 'integer');
3838
}
3939

40-
/**
41-
* Same as Entity::fromRow but ignoring unknown properties
42-
*/
43-
public static function fromRowData(array $row): RowCellSuper {
44-
$instance = new static();
45-
46-
foreach ($row as $key => $value) {
47-
$property = $instance->columnToProperty($key);
48-
$setter = 'set' . ucfirst($property);
49-
;
50-
if (property_exists($instance, $property)) {
51-
$instance->$setter($value);
52-
}
53-
}
54-
55-
$instance->resetUpdatedFields();
56-
57-
return $instance;
58-
}
59-
6040
/**
6141
* @param float|null|string $value
62-
* @param int $value_type
42+
* @param int $valueType
6343
*/
6444
public function jsonSerializePreparation(string|float|null $value, int $valueType = 0): array {
6545
return [

lib/Db/RowCellUsergroupMapper.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ public function applyDataToEntity(Column $column, RowCellSuper $cell, $data): vo
4242
$cell->setValueWrapper($data);
4343
}
4444

45-
public function formatEntity(Column $column, RowCellSuper $cell) {
46-
$displayName = $cell->getValue();
47-
if ($cell->getValueType() === UsergroupType::USER) {
48-
$displayName = $this->userManager->getDisplayName($cell->getValue()) ?? $cell->getValue();
49-
} elseif ($cell->getValueType() === UsergroupType::CIRCLE) {
50-
$displayName = $this->circleHelper->getCircleDisplayName($cell->getValue(), ($this->userSession->getUser()?->getUID() ?: '')) ?: $cell->getValue();
51-
} elseif ($cell->getValueType() === UsergroupType::GROUP) {
52-
$displayName = $this->groupHelper->getGroupDisplayName($cell->getValue()) ?: $cell->getValue();
45+
public function formatRowData(Column $column, array $row) {
46+
$value = $row['value'];
47+
$valueType = (int)$row['value_type'];
48+
$displayName = $value;
49+
if ($valueType === UsergroupType::USER) {
50+
$displayName = $this->userManager->getDisplayName($value) ?? $value;
51+
} elseif ($valueType === UsergroupType::CIRCLE) {
52+
$displayName = $this->circleHelper->getCircleDisplayName($value, ($this->userSession->getUser()?->getUID() ?: '')) ?: $value;
53+
} elseif ($valueType === UsergroupType::GROUP) {
54+
$displayName = $this->groupHelper->getGroupDisplayName($value) ?: $value;
5355
}
5456
return [
55-
'id' => $cell->getValue(),
56-
'type' => $cell->getValueType(),
57+
'id' => $value,
58+
'type' => $valueType,
5759
'displayName' => $displayName,
5860
];
5961
}

0 commit comments

Comments
 (0)