Skip to content

Commit dc288a5

Browse files
committed
optimize [] 对比 array_push 性能提升两倍
1 parent 77cde57 commit dc288a5

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎src/QueryBuilder.php‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function select(string ...$fields): ConnectionInterface
8787
*/
8888
public function join(string $table, string $on, ...$values): ConnectionInterface
8989
{
90-
array_push($this->join, ['INNER JOIN', $table, $on, $values]);
90+
$this->join[] = ['INNER JOIN', $table, $on, $values];
9191
return $this;
9292
}
9393

@@ -99,7 +99,7 @@ public function join(string $table, string $on, ...$values): ConnectionInterface
9999
*/
100100
public function leftJoin(string $table, string $on, ...$values): ConnectionInterface
101101
{
102-
array_push($this->join, ['LEFT JOIN', $table, $on, $values]);
102+
$this->join[] = ['LEFT JOIN', $table, $on, $values];
103103
return $this;
104104
}
105105

@@ -111,7 +111,7 @@ public function leftJoin(string $table, string $on, ...$values): ConnectionInter
111111
*/
112112
public function rightJoin(string $table, string $on, ...$values): ConnectionInterface
113113
{
114-
array_push($this->join, ['RIGHT JOIN', $table, $on, $values]);
114+
$this->join[] = ['RIGHT JOIN', $table, $on, $values];
115115
return $this;
116116
}
117117

@@ -123,7 +123,7 @@ public function rightJoin(string $table, string $on, ...$values): ConnectionInte
123123
*/
124124
public function fullJoin(string $table, string $on, ...$values): ConnectionInterface
125125
{
126-
array_push($this->join, ['FULL JOIN', $table, $on, $values]);
126+
$this->join[] = ['FULL JOIN', $table, $on, $values];
127127
return $this;
128128
}
129129

@@ -134,7 +134,7 @@ public function fullJoin(string $table, string $on, ...$values): ConnectionInter
134134
*/
135135
public function where(string $expr, ...$values): ConnectionInterface
136136
{
137-
array_push($this->where, ['AND', $expr, $values]);
137+
$this->where[] = ['AND', $expr, $values];
138138
return $this;
139139
}
140140

@@ -145,7 +145,7 @@ public function where(string $expr, ...$values): ConnectionInterface
145145
*/
146146
public function or(string $expr, ...$values): ConnectionInterface
147147
{
148-
array_push($this->where, ['OR', $expr, $values]);
148+
$this->where[] = ['OR', $expr, $values];
149149
return $this;
150150
}
151151

@@ -159,7 +159,7 @@ public function order(string $field, string $order): ConnectionInterface
159159
if (!in_array($order, ['asc', 'desc'])) {
160160
throw new \RuntimeException('Sort can only be asc or desc.');
161161
}
162-
array_push($this->order, [$field, strtoupper($order)]);
162+
$this->order[] = [$field, strtoupper($order)];
163163
return $this;
164164
}
165165

@@ -180,7 +180,7 @@ public function group(string ...$fields): ConnectionInterface
180180
*/
181181
public function having(string $expr, ...$values): ConnectionInterface
182182
{
183-
array_push($this->having, [$expr, $values]);
183+
$this->having[] = [$expr, $values];
184184
return $this;
185185
}
186186

@@ -257,10 +257,10 @@ protected function build(string $index, array $data = []): array
257257
$set = [];
258258
foreach ($data as $k => $v) {
259259
if ($v instanceof Expr) {
260-
array_push($set, "$k = {$v->__toString()}");
260+
$set[] = "$k = {$v->__toString()}";
261261
} else {
262262
$set[] = "$k = ?";
263-
array_push($values, $v);
263+
$values[] = $v;
264264
}
265265
}
266266
$sqls[] = "UPDATE {$this->table} SET " . implode(', ', $set);
@@ -292,6 +292,7 @@ protected function build(string $index, array $data = []): array
292292
$value = "'$value'";
293293
}
294294
}
295+
unset($value);
295296
$expr = preg_replace('/\(\?\)/', sprintf('(%s)', implode(',', $val)), $expr, 1);
296297
unset($vals[$k]);
297298
}

0 commit comments

Comments
 (0)