google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

Remove unnecessary abstraction of bytes type or give an option to skip that abstraction

Open asamats opened this issue 9 months ago • 1 comments

What does the class Google\Cloud\Spanner\Bytes make? It's an abstraction for bytes, but there's no reason to do it on a library side. Even more, it's not necessary in 99% of cases when we work with Spanner, because usually bytes in Spanner are UUID or some small size of bytes, not BLOBs. For BLOBs exist other tools and services.

So, what is happening behind that abstraction? Steps:

  • get base64 encoded bytes from Spanner
  • decode it to bytes in a class Google\Cloud\Spanner\ValueMapper::decodeValue
  • make from bytes a resource php://temp and after making a Stream class in a construction of Google\Cloud\Spanner\Bytes class by calling GuzzleHttp\Psr7\Utils::streamFor
  • use in 99% Google\Cloud\Spanner\Bytes for transforming bytes to base64

base64 -> bytes -> resource -> stream -> base64 (if you don't store BLOBs in Spanner) Saṃsāra...

So, what about memory consumption? UUID is 16 bytes but by all this abstraction, for every UUID, we pay about 100 times more memory(I've double-checked). Without that abstraction, there wouldn't be any extra memory allocations. And also CPU cycles are important.

What I propose: make that abstraction optional in the next major release, and make a mapper configurable in the current version. Maybe you have a better option or idea of how to remove unnecessary abstraction.

asamats avatar May 12 '24 11:05 asamats

    public function __construct($value)
    {
        $this->value = Utils::streamFor($value);
    }

remove this $this->value = Utils::streamFor($value);

and it will be significantly much better

ser-sergeev avatar May 14 '24 07:05 ser-sergeev

Thanks for the input!

Will take a look with the team and discuss any possible implementations that helps ease this concern for the next major release.

Hectorhammett avatar Jun 14 '24 23:06 Hectorhammett