php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67147 Support for ArrayObject
Submitted: 2014-04-28 17:36 UTC Modified: 2015-03-13 15:01 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: corrado91 at gmx dot de Assigned: stesie (profile)
Status: Closed Package: v8js (PECL)
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: corrado91 at gmx dot de
New email:
PHP Version: OS:

 

 [2014-04-28 17:36 UTC] corrado91 at gmx dot de
Description:
------------
Would it be possible to support php ArrayObject (or anything which derives from ArrayAccess, Countable) as native arrays in V8Js' javascript? I would like to write my own type of Array, but I cannot easily pass that to V8Js because it is detected as Object not as Array. Unfortunately, I cannot copy it, because it would take too much memory.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-20 22:42 UTC] stesie at brokenpipe dot de
it's been a while since you've added this feature request.  are you still interested though?  I've just been following the issue tracker over at github (https://github.com/preillyme/v8js/issues/), but not here...

What exactly are you trying to achieve?

PHP's arrays behave quite differently from JavaScript arrays.  In PHP arrays are generally sparse, i.e. you can have a element 0 and a element 5, but nothing in between.  In JavaScript (or v8 specially) they are implemented sparsely but they behave like they aren't.

That said I assume you have an ArrayObject with a certain number of objects which are pretty large each and hence don't want to wrap all the objects to javascript objects initially.  And after exporting Javascript may pick elements and they are then exported "on demand" ... and opposed to now, you'll want the exported ArrayObject to behave like a javascript array, i.e. have a length property (and generally be of array type).

I generally think it shouldn't be too hard to export such a object as an array.  However after having a swift look at the v8 api it looks like a access handler needs to be set for every array element initially, i.e. it would be a problem if the array has a largeish number of elements.

Therefore if you could elaborate on what exactly you are trying to do, especially tell us about count($arrayobject) and how large each element is, i.e. what is "too much memory"

Generally I'd be interested to implement this.

cheers
  ~stesie
 [2014-11-21 13:31 UTC] corrado91 at gmx dot de
The source of my question was that I have a huge matrix of numbers (1440*1024). If I create php arrays (array()) for this, they are so memory hungry that I will soon run out of memory and calculations are not very efficient because access can be slow.

So my solution was to use an ArrayObject instead, which is much faster and eats much less memory and is perfectly suited for my case. 

Now if I pass that to V8Js, I will get an object, which is very inefficient in JavaScript (because many special cases like empty slots need to be handled). 

On the other hand using a JS array (new Array(N)) and filling it completely with only one basic type, can be very efficient. So what I am lacking is a way to convert an ArrayObject into a basic JS Array (new Array(N) with only one type of entry). 

So I had the data accessible in php and JavaScript and in both cases very efficiently. 

Now if I could even convert an ArrayAccess object to a JS Array, I could even omit to copy the data after reading it from file with (fread), which would save me have of the memory.
 [2014-11-23 20:19 UTC] stesie at brokenpipe dot de
if at all, I think I'd go and implement based on ArrayAccess & Countable interfaces, just because of the greater benefit.

after having another look at the API of v8 I think it's going to be a bit more complicated than I initially thought.  v8::Array doesn't allow to set indexed property accessors on it; ... but v8::ObjectTemplate does.  However you cannot instanciate native arrays from a v8::ObjectTemplate.

I didn't try it yet, but it should be feasible to instanciate custom objects from a v8::ObjectTemplate (with property accessors attached + length function implementation) and then set the prototype of these objects to Array.prototype so it inherits forEach, filter & co

cheers
  ~stesie
 [2014-11-23 23:01 UTC] stesie at brokenpipe dot de
okay, I've now stepped up and implemented this ... at least to some initial degree :)

Current code is here: https://github.com/stesie/v8js/tree/array-access
The new feature is for now locked behind "v8js.use_array_access" php.ini feature flag.

For the moment the code is more of proof of concept style, I need to refactor some stuff and for the moment it's just implementing the reading part.  Especially set & unset are still to be done.

Have a look at the function test in https://github.com/stesie/v8js/blob/array-access/tests/array_access.phpt to see what it looks like :)

cheers
  ~stesie
 [2015-03-13 15:01 UTC] stesie@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: stesie
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 00:01:30 2024 UTC