Skip to content

Commit 5b191b7

Browse files
committed
Reduce accumulation-phase overhead in join, columns, and order setup
Inline buildColumnRefs into Table->join, skip addJoinRefs for empty columns, use array_push spread, lazy column normalization, and remove trim from OrderSpec direction check.
1 parent 283d9d8 commit 5b191b7

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

src/Sql/Part/Columns.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpDb\Sql\Platform\AbstractSqlRenderer;
1111
use PhpDb\Sql\Select;
1212

13+
use function array_push;
1314
use function count;
1415
use function current;
1516
use function implode;
@@ -23,14 +24,14 @@ class Columns extends AbstractPart
2324
private array $columnRefs = [];
2425
private array $rawColumns = [Select::SQL_STAR];
2526
private bool $prefixColumnsWithTable = true;
27+
private bool $isNormalized = false;
2628
private ?From $from = null;
2729
/** @var ColumnRef[] */
2830
private array $joinRefs = [];
2931

3032
public function __construct(?From $from = null)
3133
{
3234
$this->from = $from;
33-
$this->normalizeColumns();
3435
}
3536

3637
public function setFrom(?From $from): void
@@ -40,6 +41,11 @@ public function setFrom(?From $from): void
4041

4142
public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', int &$paramIndex = 0): ?string
4243
{
44+
if (! $this->isNormalized) {
45+
$this->normalizeColumns();
46+
$this->isNormalized = true;
47+
}
48+
4349
$refs = $this->columnRefs;
4450
$fromPrefix = $this->prefixColumnsWithTable && $this->from !== null && $this->from->table !== null
4551
? $renderer->renderResolvedTable($this->from->table, $this->from->alias)
@@ -100,13 +106,18 @@ public function isEmpty(): bool
100106

101107
public function set(array $columns): static
102108
{
103-
$this->rawColumns = $columns;
104-
$this->normalizeColumns();
109+
$this->rawColumns = $columns;
110+
$this->isNormalized = false;
105111
return $this;
106112
}
107113

108114
public function add(array|ArgumentInterface|ExpressionInterface|string $column, ?string $alias = null): static
109115
{
116+
if (! $this->isNormalized) {
117+
$this->normalizeColumns();
118+
$this->isNormalized = true;
119+
}
120+
110121
if (is_array($column)) {
111122
$key = key($column);
112123
$alias = ! is_numeric($key) ? $key : null;
@@ -142,9 +153,7 @@ public function getPrefixColumnsWithTable(): bool
142153
/** @param ColumnRef[] $refs */
143154
public function addJoinRefs(array $refs): static
144155
{
145-
foreach ($refs as $ref) {
146-
$this->joinRefs[] = $ref;
147-
}
156+
array_push($this->joinRefs, ...$refs);
148157
return $this;
149158
}
150159

src/Sql/Part/OrderBy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function implode;
1414
use function is_array;
1515
use function is_string;
16+
use function ltrim;
1617
use function preg_split;
1718
use function str_contains;
1819

@@ -77,7 +78,7 @@ public function add(ArgumentInterface|ExpressionInterface|array|string $order):
7778
$this->order[] = new OrderSpec($k, $v);
7879
} elseif (str_contains($v, ' ')) {
7980
[$col, $dir] = explode(' ', $v, 2);
80-
$this->order[] = new OrderSpec($col, $dir);
81+
$this->order[] = new OrderSpec($col, ltrim($dir));
8182
} else {
8283
$this->order[] = new OrderSpec($v);
8384
}

src/Sql/Part/OrderSpec.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use function is_string;
1212
use function strcasecmp;
13-
use function trim;
1413

1514
final readonly class OrderSpec
1615
{
@@ -25,7 +24,7 @@ public function __construct(
2524
string $direction = self::ORDER_ASCENDING,
2625
) {
2726
$this->column = is_string($column) ? new Identifier($column) : $column;
28-
$this->direction = strcasecmp(trim($direction), self::ORDER_DESCENDING) === 0
27+
$this->direction = strcasecmp($direction, self::ORDER_DESCENDING) === 0
2928
? self::ORDER_DESCENDING
3029
: self::ORDER_ASCENDING;
3130
}

src/Sql/Part/Table.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ public function join(
9090
): static {
9191
$spec = self::createJoinSpec($name, $on, $columns, $type);
9292
$this->join->add($spec);
93-
$this->columns->addJoinRefs(self::buildColumnRefs($columns, $spec->table, $spec->alias));
93+
94+
if (! is_array($columns)) {
95+
$columns = [$columns];
96+
}
97+
98+
if ($columns !== []) {
99+
$refs = [];
100+
foreach ($columns as $key => $column) {
101+
$refs[] = new ColumnRef($key, $column, $spec->table, $spec->alias);
102+
}
103+
$this->columns->addJoinRefs($refs);
104+
}
105+
94106
return $this;
95107
}
96108

@@ -121,23 +133,6 @@ public static function createJoinSpec(
121133
return new JoinSpec($table, $alias, $on, $type);
122134
}
123135

124-
/** @return ColumnRef[] */
125-
private static function buildColumnRefs(
126-
array|string $columns,
127-
TableIdentifier|Select|ExpressionInterface $table,
128-
?string $alias,
129-
): array {
130-
if (! is_array($columns)) {
131-
$columns = [$columns];
132-
}
133-
134-
$refs = [];
135-
foreach ($columns as $key => $column) {
136-
$refs[] = new ColumnRef($key, $column, $table, $alias);
137-
}
138-
return $refs;
139-
}
140-
141136
public function hasJoins(): bool
142137
{
143138
return $this->join->isEmpty();

0 commit comments

Comments
 (0)