Skip to content

Commit 9803378

Browse files
committed
Initial commit
0 parents  commit 9803378

File tree

11 files changed

+1011
-0
lines changed

11 files changed

+1011
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/rust/libonebrc.so
2+
/rust/onebrc/target
3+
.idea
4+
measurements.txt

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Enable FFI in PHP 8.3 (Arch Linux)
2+
```sh
3+
sudo sed -i 's/;extension=ffi/extension=ffi/' /etc/php/php.ini
4+
sudo sed -i 's/;zend_extension=opcache/zend_extension=opcache/' /etc/php/php.ini
5+
6+
echo "ffi.enable=preload" | sudo tee -a /etc/php/conf.d/ffi.ini
7+
8+
echo "opcache.enable=1" | sudo tee -a /etc/php/conf.d/opcache.ini
9+
echo "opcache.enable_cli=1" | sudo tee -a /etc/php/conf.d/opcache.ini
10+
echo "opcache.jit=on" | sudo tee -a /etc/php/conf.d/opcache.ini
11+
```
12+
13+
## Compiling Rust library
14+
From the root directory, run:
15+
```sh
16+
cd rust/onebrc && cargo build --release && rm -f ../libonebrc.so && mv target/release/libonebrc.so ../ && strip --strip-unneeded ../libonebrc.so && cd ../../
17+
```
18+
19+
## Running
20+
Generate `rust/measurements.txt` file from the original 1brc tool.
21+
Then from the root directory, run:
22+
23+
```sh
24+
php app/index.php
25+
```

app/index.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
require_once "libonebrc.php";
4+
$libOneBrc = new LibOneBrc();
5+
6+
$filename = "./rust/measurements.txt";
7+
8+
$result = json_decode($libOneBrc->run($filename));
9+
10+
echo "{" . PHP_EOL;
11+
$isFirstRow = true;
12+
foreach ($result as $key => $value) {
13+
if (!$isFirstRow) {
14+
echo "," . PHP_EOL;
15+
} else {
16+
$isFirstRow = false;
17+
}
18+
19+
echo "\t" . $value->city . '=' . $value->max / 10 . '/' . $value->min / 10 . '/' . round($value->sum / $value->count / 10, 1);
20+
}
21+
echo PHP_EOL . "}" . PHP_EOL;
22+

app/libonebrc.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
final class LibOneBrc {
3+
private static $ffi = null;
4+
5+
function __construct() {
6+
if (is_null(self::$ffi)) {
7+
self::$ffi = FFI::cdef("char* run(const char* str);", "rust/libonebrc.so");
8+
}
9+
}
10+
11+
function run($filename) {
12+
$resultPtr = self::$ffi->run($filename);
13+
return FFI::string($resultPtr);
14+
}
15+
}

rust/onebrc/.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
rustflags = ["-C", "target-cpu=native"]

0 commit comments

Comments
 (0)