Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ext-ds to replace array to vector for performance optimization #122

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
with:
php-version: 8.3
coverage: none
extensions: ds
ini-values: opcache.enable_cli=1, opcache.jit=1255

- name: Build local Phar file
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
with:
php-version: 8.3
coverage: xdebug
extensions: ast
extensions: ast, ds

- name: Build project
run: make build --no-print-directory
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
with:
php-version: 8.1
coverage: none
extensions: ast
extensions: ast, ds

- name: Install project
run: make build --no-print-directory
Expand Down Expand Up @@ -142,7 +142,7 @@ jobs:
with:
php-version: highest
coverage: none
extensions: ast
extensions: ast, ds

- name: Install project
run: make build --no-print-directory
Expand Down Expand Up @@ -182,6 +182,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: ds

- name: Build project in production mode
run: make build-prod --no-print-directory
Expand Down Expand Up @@ -214,6 +215,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: ds

- name: Build project in production mode
run: make build-prod build-phar-file --no-print-directory
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
with:
php-version: 8.3
tools: composer
extensions: ds

- name: Build project in production mode
run: make build-prod build-phar-file --no-print-directory
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ FROM php:8.3-cli-alpine

# Install PHP extensions
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN install-php-extensions opcache @composer
RUN install-php-extensions opcache ds @composer

# Install application
# run `make build-version` before!
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "jbzoo/csv-blueprint",
"type" : "project",
"type" : "project",
"description" : "CLI Utility for Validating and Generating CSV Files Based on Custom Rules. It ensures your data meets specified criteria, streamlining data management and integrity checks.",
"license" : "MIT",
"keywords" : [
Expand Down Expand Up @@ -29,6 +29,7 @@
"require" : {
"php" : "^8.1",
"ext-mbstring" : "*",
"ext-ds" : "*",

"league/csv" : "^9.15.0",
"jbzoo/data" : "^7.1.1",
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/AbstarctRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules;

use Ds\Vector;
use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Error;
use JBZoo\CsvBlueprint\Validators\ValidatorColumn;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function __construct(
// TODO: Move resolving and validating expected value on this stage to make it only once (before validation).
}

public function validate(array|string $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error
public function validate(array|string|Vector $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error
{
// TODO: Extract to abstract boolean cell/agregate rule
if ($this->isEnabled($cellValue) === false) {
Expand Down Expand Up @@ -185,7 +186,7 @@ protected function getOptionAsArray(): array
* Optimize performance
* There is no need to validate empty values for predicates or if it's disabled.
*/
protected function isEnabled(array|string $cellValue): bool
protected function isEnabled(string|Vector $cellValue): bool
{
// List of rules that should be checked for empty values
$exclusions = [
Expand Down
9 changes: 5 additions & 4 deletions src/Rules/AbstarctRuleCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\Aggregate\AbstractAggregateRuleCombo;
use JBZoo\CsvBlueprint\Rules\Cell\AbstractCellRuleCombo;
use JBZoo\CsvBlueprint\Validators\Error;
Expand All @@ -38,7 +39,7 @@ abstract protected function getExpected(): float;

abstract protected function getActual(array|string $value): float;

public function validate(array|string $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error
public function validate(string|Vector $cellValue, int $line = ValidatorColumn::FALLBACK_LINE): ?Error
{
$error = $this->validateCombo($cellValue);

Expand All @@ -51,7 +52,7 @@ public function validate(array|string $cellValue, int $line = ValidatorColumn::F

public function test(array|string $cellValue, bool $isHtml = false): string
{
$errorMessage = (string)$this->validateCombo($cellValue);
$errorMessage = (string)$this->validateCombo(new Vector($cellValue));

return $isHtml ? $errorMessage : \strip_tags($errorMessage);
}
Expand Down Expand Up @@ -96,7 +97,7 @@ protected static function compare(float $expected, float $actual, string $mode):
};
}

private function validateCombo(array|string $cellValue): ?string
private function validateCombo(string|Vector $cellValue): ?string
{
if ($this instanceof AbstractCellRuleCombo) {
if (!\is_string($cellValue)) {
Expand All @@ -107,7 +108,7 @@ private function validateCombo(array|string $cellValue): ?string
}

if ($this instanceof AbstractAggregateRuleCombo) {
if (!\is_array($cellValue)) {
if (!$cellValue instanceof Vector) {
throw new \InvalidArgumentException('The value should be an array of numbers/strings');
}

Expand Down
5 changes: 2 additions & 3 deletions src/Rules/Aggregate/AbstractAggregateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

abstract class AbstractAggregateRule extends AbstarctRule
Expand All @@ -28,10 +29,8 @@ abstract class AbstractAggregateRule extends AbstarctRule
* TODO: This method takes an array reference &$columnValues as input and returns a nullable string.
* We use a reference to the array to avoid copying the array. Important memory optimization!
* Please DO NOT change the array in this method!
*
* @param string[] $columnValues
*/
abstract public function validateRule(array $columnValues): ?string;
abstract public function validateRule(Vector $columnValues): ?string;

public function test(array $cellValue, bool $isHtml = false): string
{
Expand Down
19 changes: 10 additions & 9 deletions src/Rules/Aggregate/AbstractAggregateRuleCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;
use JBZoo\CsvBlueprint\Rules\AbstarctRuleCombo;

abstract class AbstractAggregateRuleCombo extends AbstarctRuleCombo
{
public const INPUT_TYPE = AbstarctRule::INPUT_TYPE_STRINGS;

abstract protected function getActualAggregate(array $colValues): ?float;
abstract protected function getActualAggregate(Vector $colValues): ?float;

public function getRuleCode(?string $mode = null): string
{
Expand All @@ -46,21 +47,21 @@ protected function getExpected(): float
return $this->getOptionAsFloat();
}

protected function validateComboAggregate(array $colValues, string $mode): ?string
protected function validateComboAggregate(Vector $colValues, string $mode): ?string
{
$prefix = $mode === self::NOT ? 'not ' : '';
$verb = static::VERBS[$mode];
$name = static::NAME;

try {
// TODO: Think about the performance optimization here
if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_FLOATS) {
$colValues = \array_map('floatval', $colValues);
}

if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_INTS) {
$colValues = \array_map('intval', $colValues);
}
// if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_FLOATS) {
// $colValues = \array_map('floatval', $colValues);
// }
//
// if (static::INPUT_TYPE === AbstarctRule::INPUT_TYPE_INTS) {
// $colValues = \array_map('intval', $colValues);
// }

$actual = $this->getActualAggregate($colValues); // Important to use the original method here!
} catch (\Throwable $exception) {
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Aggregate/ComboAverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;
use MathPHP\Statistics\Average;

Expand All @@ -30,12 +31,12 @@ public function getHelpMeta(): array
return [['Regular the arithmetic mean. The sum of the numbers divided by the count.'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}

return Average::mean($colValues);
return Average::mean($colValues->toArray());
}
}
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCoefOfVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;
use MathPHP\Statistics\Descriptive;

Expand All @@ -38,7 +39,7 @@ public function getHelpMeta(): array
];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboContraharmonicMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;
use MathPHP\Statistics\Average;

Expand All @@ -37,7 +38,7 @@ public function getHelpMeta(): array
];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCount extends AbstractAggregateRuleCombo
Expand All @@ -36,7 +37,7 @@ public function getHelpMeta(): array
];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
return \count($colValues);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountDistinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountDistinct extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Number of unique values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountEmpty extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Counts only empty values (string length is 0).'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountEven.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountEven extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Number of even values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountNegative.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountNegative extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Number of negative values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountNotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountNotEmpty extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Counts only not empty values (string length is not 0).'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Aggregate/ComboCountOdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace JBZoo\CsvBlueprint\Rules\Aggregate;

use Ds\Vector;
use JBZoo\CsvBlueprint\Rules\AbstarctRule;

final class ComboCountOdd extends AbstractAggregateRuleCombo
Expand All @@ -29,7 +30,7 @@ public function getHelpMeta(): array
return [['Number of odd values.'], []];
}

protected function getActualAggregate(array $colValues): ?float
protected function getActualAggregate(Vector $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
Expand Down
Loading
Loading