Skip to content

Commit 404b6e3

Browse files
thiemowmdeKrinkle
authored andcommitted
Inline 1-use methods for performance/readability
I suggest this for a series of reasons: 1. Performance. Every function scope is expensive in PHP. This code is executed hundreds of thousands of times. This adds up fast. 2. This allows us to merge two write() calls together. 3. Readabilty becomes better, I would argue. Names like "addbegin" and "addend" were not really meaningful to begin with. Inlining them allows to change the execution order slightly and group stuff together that belongs together. Both $this->hplist and $this->numentries are only needed much later in finish(). This patch alone makes an actual difference. For example, the MessageIndexTest in the Translate extension improves from 1.7s to 1.4s on my machine. Change-Id: I94ce310384a807b8ff177a5c80826d5e77f184b0
1 parent 54cad84 commit 404b6e3

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/Writer/PHP.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,27 @@ public function set( $key, $value ): void {
7272
// DBA cross-check hack
7373
return;
7474
}
75-
$this->addbegin( strlen( $key ), strlen( $value ) );
76-
$this->write( $key . $value );
77-
$this->addend( strlen( $key ), strlen( $value ), Util::hash( $key ) );
75+
76+
// Based on cdb_make_addbegin
77+
$keylen = strlen( $key );
78+
$datalen = strlen( $value );
79+
if ( $keylen > 0x7fffffff ) {
80+
$this->throwException( 'Key length too long in file "' . $this->tmpFileName . '".' );
81+
}
82+
if ( $datalen > 0x7fffffff ) {
83+
$this->throwException( 'Data length too long in file "' . $this->tmpFileName . '".' );
84+
}
85+
$begin = pack( 'VV', $keylen, $datalen );
86+
87+
$this->write( $begin . $key . $value );
88+
89+
// Based on cdb_make_addend
90+
$this->hplist[] = [
91+
'h' => Util::hash( $key ),
92+
'p' => $this->pos
93+
];
94+
$this->numentries++;
95+
$this->posplus( 8 + $keylen + $datalen );
7896
}
7997

8098
public function close(): void {
@@ -114,36 +132,6 @@ protected function posplus( $len ) {
114132
$this->pos = $newpos;
115133
}
116134

117-
/**
118-
* @param int $keylen
119-
* @param int $datalen
120-
* @param int $h
121-
*/
122-
protected function addend( $keylen, $datalen, $h ) {
123-
$this->hplist[] = [
124-
'h' => $h,
125-
'p' => $this->pos
126-
];
127-
128-
$this->numentries++;
129-
$this->posplus( 8 + $keylen + $datalen );
130-
}
131-
132-
/**
133-
* @param int $keylen
134-
* @param int $datalen
135-
*/
136-
protected function addbegin( $keylen, $datalen ) {
137-
if ( $keylen > 0x7fffffff ) {
138-
$this->throwException( 'Key length too long in file "' . $this->tmpFileName . '".' );
139-
}
140-
if ( $datalen > 0x7fffffff ) {
141-
$this->throwException( 'Data length too long in file "' . $this->tmpFileName . '".' );
142-
}
143-
$buf = pack( 'VV', $keylen, $datalen );
144-
$this->write( $buf );
145-
}
146-
147135
protected function finish(): void {
148136
// Hack for DBA cross-check
149137
$this->hplist = array_reverse( $this->hplist );

0 commit comments

Comments
 (0)