Skip to content

Commit ea80d37

Browse files
committed
Optimized binary reader
1 parent 0569bd8 commit ea80d37

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/Core/JVM/AttributePool.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public function __construct(
2020
$this->reader = $reader;
2121
for ($i = 0; $i < $entries; $i++) {
2222
// not implemented, read only
23-
$this->entries[$i] = new AttributeInfo($reader);
24-
$this->entries[$i]->setConstantPool($constantPool);
25-
$this->entries[$i]->setDebugTool($debugTool);
26-
$this->entries[$i]->execute();
23+
$entry = (new AttributeInfo($reader))
24+
->setConstantPool($constantPool)
25+
->setDebugTool($debugTool);
26+
$entry->execute();
27+
28+
$this->entries[] = $entry;
2729
}
2830
}
2931

src/Core/JVM/Stream/BinaryReader.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public function readByte()
3535

3636
public function readUnsignedByte()
3737
{
38-
return (int) sprintf('%u', ord($this->read(1)));
38+
return current(unpack('C', $this->read(1)));
3939
}
4040

4141
public function readUnsignedInt()
4242
{
43-
return base_convert(bin2hex($this->read(4)), 16, 10);
43+
return current(unpack('N', $this->read(4)));
4444
}
4545

4646
public function readUnsignedShort()
4747
{
48-
return (int) sprintf('%u', hexdec(bin2hex($this->read(2))));
48+
return current(unpack('n', $this->read(2)));
4949
}
5050

5151
public function readInt()
@@ -57,16 +57,12 @@ public function readInt()
5757
public function readShort()
5858
{
5959
$short = $this->readUnsignedShort();
60-
return (($short & 0x8000) > 0) ? ($short - 0xFFFF - 1) : $short ;
60+
return (($short & 0x8000) > 0) ? ($short - 0xFFFF - 1) : $short;
6161
}
6262

6363
public function readUnsignedLong()
6464
{
65-
if (PHP_INT_MAX === 2147483647) {
66-
return base_convert(bin2hex($this->read(8)), 16, 10);
67-
}
68-
69-
return (int) sprintf('%u', hexdec(bin2hex($this->read(8))));
65+
return current(unpack('J', $this->read(8)));
7066
}
7167

7268
public function readLong()

src/Core/JavaClass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public function __construct(ReaderInterface $reader, array $options = [])
224224
$options
225225
);
226226

227+
var_dump(microtime(true) - $this->startTime);
228+
227229
if ($this->invoker->getStatic()->getMethods()->has('<clinit>')) {
228230
$this->invoker
229231
->getStatic()

0 commit comments

Comments
 (0)