Skip to content

Commit

Permalink
Moved docgen (previously gendoc) and php-cb into tools/ dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
smuuf committed Mar 27, 2022
1 parent f14b8db commit d6420b6
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bin/gendoc → bin/docgen
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cd `dirname $0`/..

php ./docs/gendoc/gendoc.php
php ./tools/docgen/docgen.php
50 changes: 0 additions & 50 deletions res/phpcb-benchmarks/string_numbers_engines.php

This file was deleted.

21 changes: 16 additions & 5 deletions docs/gendoc/gendoc.php → tools/docgen/docgen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

declare(strict_types=1);

use \Smuuf\Primi\Context;
use \Smuuf\Primi\Structures\Value;

define('ROOT_DIR', realpath(__DIR__ . '/../..'));
Expand Down Expand Up @@ -47,7 +48,7 @@ function str_ends_with(string $haystack, string $needle): bool {
}
}

out('█ Primi Standard Library docs generator');
out('DocGen: Primi Standard Library Docs Generator');

out('Parsing extension files...');
const EXTENSIONS_GLOB = ROOT_DIR . '/src/extensions/psl/*.php';
Expand All @@ -72,14 +73,16 @@ function ($ref) {

function clean_doc_whitespace(string $doc): string {
// Unify newlines.
// Remove @annotations
$doc = preg_replace('#^\h*\*+\h*\h@\w+\h*$#m', '', $doc);
$doc = preg_replace('#\r?\n#', "\n", $doc);
// Remove whitespace and '*' and '/' from the start
$doc = preg_replace('#^[\/\s\*]*+#', '', $doc);
// Remove whitespace and '*' and '/' at the end.
$doc = preg_replace('#[\/\s\*]*+$#', '', $doc);
// Remove startofline-horizontalwhitespace-asterisk-whitespace
// Handles also empty lines after the asterisk (removes those empty lines).
$doc = preg_replace('#^\h*(\*+\s*+)*#m', '', $doc);
$doc = preg_replace('#^\h*\**(\h+|\n)#m', '', $doc);
$doc = trim($doc);
return $doc;
}
Expand All @@ -102,8 +105,16 @@ function extract_params(\ReflectionMethod $methodRef): array {
$paramType = $paramTypeRef->getName();
}

if (!is_a($paramType, Value::class, true)) {
warn("Class '$className, method '$methodName', parameter '$paramName' does not hint Primi's Value class.");
$isValue = is_a($paramType, Value::class, true);
$isCtx = is_a($paramType, Context::class, true);
if (!$isValue && !$isCtx) {
warn("Class '$className, method '$methodName', parameter '$paramName' does not hint Value|Context");
continue;
}

// Context is automatically injected to functions that need it.
// This has no place in function's signature. Skip this param type.
if ($isCtx) {
continue;
}

Expand Down Expand Up @@ -142,7 +153,7 @@ function extract_params(\ReflectionMethod $methodRef): array {
warn("Class '$className, method '$methodName', referencing non-existent type having class " . $returnTypeRef->getName());
}
} else {
warn("Class '$className, method '$methodName', return type does not hint Primi's Value class.");
warn("Class '$className, method '$methodName', return type does not hint Value|Context");
}

$data[$className][$methodName] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
require __DIR__ . "/../../vendor/autoload.php";
$bench = new \Smuuf\Phpcb\PhpBenchmark(new \Smuuf\Phpcb\SerialEngine);

function shuffle_assoc(array $array): array {

$keys = array_keys($array);
shuffle($keys);

foreach($keys as $key) {
$new[$key] = $array[$key];
}

return $new;

}

$arr = [
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 'c'],
Expand All @@ -16,6 +29,13 @@
[1, 2, 3, 4, 'c' => true, 6, 7, 8, 9],
];

$megaarr = [];
foreach ($arr as $a) {
foreach (range(1, 100) as $_) {
$megaarr[] = shuffle_assoc($a);
}
}

function is_array_dict_A(array $input): bool {

// Let's say that empty PHP array is not a dictionary.
Expand Down Expand Up @@ -63,28 +83,28 @@ function is_array_dict_C(array $input): bool {

}

$bench->addBench(function() use ($arr) {
$bench->addBench(function() use ($megaarr) {
$results = [];
foreach ($arr as $a) {
foreach ($megaarr as $a) {
$results[] = is_array_dict_A($a);
}
return $results;
});

$bench->addBench(function() use ($arr) {
$bench->addBench(function() use ($megaarr) {
$results = [];
foreach ($arr as $a) {
foreach ($megaarr as $a) {
$results[] = is_array_dict_B($a);
}
return $results;
});

$bench->addBench(function() use ($arr) {
$bench->addBench(function() use ($megaarr) {
$results = [];
foreach ($arr as $a) {
foreach ($megaarr as $a) {
$results[] = is_array_dict_C($a);
}
return $results;
});

$bench->run(1e3);
$bench->run(50);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions tools/phpcb-benchmarks/string_numbers_engines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

require __DIR__ . "/../../vendor/autoload.php";
$bench = new \Smuuf\Phpcb\PhpBenchmark;

$bench->addBench(function() {

$res = [];
$res[] = $x = 123 + 456;
$res[] = $x * 789;
$res[] = $x / 1024;
$res[] = $x - 1024;
$res[] = $x * 123456789;
$res[] = $x * 987654321;

return array_map('intval', $res);

});

$bench->addBench(function() {

$res = [];
$res[] = $x = "123" + "456";
$res[] = $x * "789";
$res[] = $x / "1024";
$res[] = $x - "1024";
$res[] = $x * "123456789";
$res[] = $x * "987654321";

return array_map('intval', $res);

});

$bench->addBench(function() {

$res = [];
$res[] = $x = \bcadd("123", "456");
$res[] = \bcmul($x, "789");
$res[] = \bcdiv($x, "1024");
$res[] = \bcsub($x, "1024");
$res[] = \bcmul($x, "123456789");
$res[] = \bcmul($x, "987654321");

return array_map('intval', $res);

});

$bench->addBench(function() {

$res = [];
$res[] = $x = \gmp_add("123", "456");
$res[] = \gmp_mul($x, "789");
$res[] = \gmp_div($x, "1024");
$res[] = \gmp_sub($x, "1024");
$res[] = \gmp_mul($x, "123456789");
$res[] = \gmp_mul($x, "987654321");

return array_map('intval', $res);

});

$bench->run(1000000);
File renamed without changes.

0 comments on commit d6420b6

Please sign in to comment.