Coder Social home page Coder Social logo

Comments (21)

Tigrov avatar Tigrov commented on June 19, 2024 2

The parser can potentially be used for in-project optimization.

For example:

$intArray = $command->setSql('SELECT int_array FROM some_table')->queryScalar();
$parsedArray = (new ArrayParser())->parse($intArray);
$castArray = array_map('intval', $parsedArray);

instead of $castArray = $column->phpTypecast($intArray).

from db-pgsql.

vjik avatar vjik commented on June 19, 2024 2

Difference is so small and in different environments results may vary slightly. By logic property and static should be most performance, but in optimization most logic not always is most performance.

@Tigrov Little improve benchmark: Tigrov/yii3-benchmarks#1

My results:

image

Looks like directly use new ((new ArrayHelper())->parse()) is most performance variant.

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024 1

yes, that's not a problem, it just resolves the value, it's not saved anywhere.

Why create instance every time? Many times - for each type casting.

But that's not a problem, as i tell you the object is not saved, it only resolves the value.

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024 1

yes, that's not a problem, it just resolves the value, it's not saved anywhere.

Why create instance every time? Many times - for each type casting.

But that's not a problem, as i tell you the object is not saved, it only resolves the value.

It's like widgets, they always create an object, and resolve its value to a string.

from db-pgsql.

vjik avatar vjik commented on June 19, 2024 1

Simple, if I create my own db-pgsql implementation, or just create the PGSQL NON-PDO implementation, for example.

Everything cannot be within the framework, there will be extensions.

It's will be extension of yiisoft/db, not yiisoft/db-pgsql. Array parser is not goal of this package, I don't think that yiisoft/db-pgsql should provide it.

from db-pgsql.

smirnov-e avatar smirnov-e commented on June 19, 2024 1

can potentially be used for in-project optimization.

It's not optimisation, but the only way to get array from raw responses :(

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024
  1. Suggest make ArrayParser as internal. Users don't must use this class directly, it'sright?
  2. For each type casting of string values created new instance of ArrayParser.

public function phpTypecast(mixed $value): mixed
{
if ($this->dimension > 0) {
if (is_string($value)) {
$value = $this->getArrayParser()->parse($value);
}

It would be good prevent this behavior and create instance of ArrayParser once or make ArrayParser methods static.

getArrayParser() return new ArrayParser().

Always returns a new instance.

protected function getArrayParser(): ArrayParser

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

getArrayParser() return new ArrayParser().

And creates new instance every time.

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024

getArrayParser() return new ArrayParser().

And creates new instance every time.

yes, that's not a problem, it just resolves the value, it's not saved anywhere.

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

yes, that's not a problem, it just resolves the value, it's not saved anywhere.

Why create instance every time?
Many times - for each type casting.

from db-pgsql.

samdark avatar samdark commented on June 19, 2024

Should be a good optimization. Worth benchmarking.

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024

Btw array parser cannot be internal, Db is an abstraction and you may need it for an extension.

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

Btw array parser cannot be internal, Db is an abstraction and you may need it for an extension.

ArrayParser used in specific implementation for PostgreSQL. When might it be needed somewhere else?

from db-pgsql.

terabytesoftw avatar terabytesoftw commented on June 19, 2024

Simple, if I create my own db-pgsql implementation, or just create the PGSQL NON-PDO implementation, for example.

Everything cannot be within the framework, there will be extensions.

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

The parser can potentially be used for in-project optimization.

Agree. This is good case.

from db-pgsql.

Tigrov avatar Tigrov commented on June 19, 2024

It's not optimisation, but the only way to get array from raw responses :(

There is the second way where the parser is not used directly:

$castArray = $column->phpTypecast($rawIntArray);

But the first way will work almost 2 times faster.

from db-pgsql.

Tigrov avatar Tigrov commented on June 19, 2024

Benchmarks to compare current implementation with using property or static class:

https://github.com/Tigrov/yii3-benchmarks/blob/master/src/Benchmark/PgsqlBench.php

Results:

    benchParseIntArrayCurrent...............I4 - Mo367.103μs (±0.61%)
    benchParseIntArrayProperty..............I4 - Mo367.761μs (±0.90%)
    benchParseIntArrayVar...................I4 - Mo369.223μs (±0.29%)
    benchParseIntArrayNew...................I4 - Mo385.663μs (±0.31%)
    benchParseIntArrayStatic................I4 - Mo372.307μs (±0.36%)

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

Benchmarks to compare current implementation with using property or static class:

Optimization in PHP good work.

Seems, for cases when object use clear methods without side effects we can create object many times and it don't degradate performance.

from db-pgsql.

smirnov-e avatar smirnov-e commented on June 19, 2024

@vjik benchParseIntArrayNew is 4.8% slower than benchParseIntArrayProperty. Difference is not big, but noticeable.

from db-pgsql.

vjik avatar vjik commented on June 19, 2024

@vjik benchParseIntArrayNew is 4.8% slower than benchParseIntArrayProperty. Difference is not big, but noticeable.

On my PC result little other:

image

Difference is so small.

from db-pgsql.

Tigrov avatar Tigrov commented on June 19, 2024

Changed ITEMS_COUNT to 1, results a slightly different:

    benchParseIntArrayCurrent...............I4 - Mo0.825μs (±1.67%)
    benchParseIntArrayProperty..............I4 - Mo0.765μs (±1.31%)
    benchParseIntArrayVar...................R5 I4 - Mo0.793μs (±1.83%)
    benchParseIntArrayNew...................R1 I3 - Mo0.790μs (±1.25%)
    benchParseIntArrayStatic................R1 I4 - Mo0.982μs (±1.52%)

Looks like with property it works faster but the benchmark doesn't cover initialization of the property (from constructor, method or cache)

from db-pgsql.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.