diff --git a/bin/gendoc b/bin/docgen similarity index 52% rename from bin/gendoc rename to bin/docgen index 3468ccd3..9c34a33a 100644 --- a/bin/gendoc +++ b/bin/docgen @@ -2,4 +2,4 @@ cd `dirname $0`/.. -php ./docs/gendoc/gendoc.php +php ./tools/docgen/docgen.php diff --git a/res/phpcb-benchmarks/string_numbers_engines.php b/res/phpcb-benchmarks/string_numbers_engines.php deleted file mode 100644 index ee7b122a..00000000 --- a/res/phpcb-benchmarks/string_numbers_engines.php +++ /dev/null @@ -1,50 +0,0 @@ -addBench(function() { - - $x = 123 + 456; - $x = $x * 789; - $x = $x / 1024; - $x = $x - 1024; - $x = $x * 123456789; - $x = $x * 987654321; - -}); - -$bench->addBench(function() { - - $x = "123" + "456"; - $x = $x * "789"; - $x = $x / "1024"; - $x = $x - "1024"; - $x = $x * "123456789"; - $x = $x * "987654321"; - -}); - -$bench->addBench(function() { - - $x = \bcadd("123", "456"); - $x = \bcmul($x, "789"); - $x = \bcdiv($x, "1024"); - $x = \bcsub($x, "1024"); - $x = \bcmul($x, "123456789"); - $x = \bcmul($x, "987654321"); - -}); - -$bench->addBench(function() { - - $x = \gmp_add("123", "456"); - $x = \gmp_mul($x, "789"); - $x = \gmp_div($x, "1024"); - $x = \gmp_sub($x, "1024"); - $x = \gmp_mul($x, "123456789"); - $x = \gmp_mul($x, "987654321"); - -}); - -$bench->run(1000000); diff --git a/docs/gendoc/gendoc.php b/tools/docgen/docgen.php similarity index 90% rename from docs/gendoc/gendoc.php rename to tools/docgen/docgen.php index bf62dd4c..911dbdf8 100644 --- a/docs/gendoc/gendoc.php +++ b/tools/docgen/docgen.php @@ -3,6 +3,7 @@ declare(strict_types=1); +use \Smuuf\Primi\Context; use \Smuuf\Primi\Structures\Value; define('ROOT_DIR', realpath(__DIR__ . '/../..')); @@ -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'; @@ -72,6 +73,8 @@ 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); @@ -79,7 +82,7 @@ function clean_doc_whitespace(string $doc): string { $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; } @@ -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; } @@ -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] = [ diff --git a/res/phpcb-benchmarks/array_list_dict.php b/tools/phpcb-benchmarks/array_list_dict.php similarity index 76% rename from res/phpcb-benchmarks/array_list_dict.php rename to tools/phpcb-benchmarks/array_list_dict.php index 81de7bfa..7a0abfe2 100644 --- a/res/phpcb-benchmarks/array_list_dict.php +++ b/tools/phpcb-benchmarks/array_list_dict.php @@ -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'], @@ -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. @@ -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); diff --git a/res/phpcb-benchmarks/arrayiter_vs_yield.php b/tools/phpcb-benchmarks/arrayiter_vs_yield.php similarity index 100% rename from res/phpcb-benchmarks/arrayiter_vs_yield.php rename to tools/phpcb-benchmarks/arrayiter_vs_yield.php diff --git a/res/phpcb-benchmarks/fn_vs_static_method.php b/tools/phpcb-benchmarks/fn_vs_static_method.php similarity index 100% rename from res/phpcb-benchmarks/fn_vs_static_method.php rename to tools/phpcb-benchmarks/fn_vs_static_method.php diff --git a/res/phpcb-benchmarks/if_equality.php b/tools/phpcb-benchmarks/if_equality.php similarity index 100% rename from res/phpcb-benchmarks/if_equality.php rename to tools/phpcb-benchmarks/if_equality.php diff --git a/res/phpcb-benchmarks/if_true_equality.php b/tools/phpcb-benchmarks/if_true_equality.php similarity index 100% rename from res/phpcb-benchmarks/if_true_equality.php rename to tools/phpcb-benchmarks/if_true_equality.php diff --git a/res/phpcb-benchmarks/is_round_int.php b/tools/phpcb-benchmarks/is_round_int.php similarity index 100% rename from res/phpcb-benchmarks/is_round_int.php rename to tools/phpcb-benchmarks/is_round_int.php diff --git a/res/phpcb-benchmarks/key_exists_vs_isset.php b/tools/phpcb-benchmarks/key_exists_vs_isset.php similarity index 100% rename from res/phpcb-benchmarks/key_exists_vs_isset.php rename to tools/phpcb-benchmarks/key_exists_vs_isset.php diff --git a/res/phpcb-benchmarks/numeric_int.php b/tools/phpcb-benchmarks/numeric_int.php similarity index 100% rename from res/phpcb-benchmarks/numeric_int.php rename to tools/phpcb-benchmarks/numeric_int.php diff --git a/res/phpcb-benchmarks/string_concat_extrapolate.php b/tools/phpcb-benchmarks/string_concat_extrapolate.php similarity index 100% rename from res/phpcb-benchmarks/string_concat_extrapolate.php rename to tools/phpcb-benchmarks/string_concat_extrapolate.php diff --git a/tools/phpcb-benchmarks/string_numbers_engines.php b/tools/phpcb-benchmarks/string_numbers_engines.php new file mode 100644 index 00000000..36fce616 --- /dev/null +++ b/tools/phpcb-benchmarks/string_numbers_engines.php @@ -0,0 +1,62 @@ +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); diff --git a/res/phpcb-benchmarks/subclass_vs_cache.php b/tools/phpcb-benchmarks/subclass_vs_cache.php similarity index 100% rename from res/phpcb-benchmarks/subclass_vs_cache.php rename to tools/phpcb-benchmarks/subclass_vs_cache.php