-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] How to avoid the need to be aware of zend_object in the internal structure code? #21
Comments
#15 is directly related. |
Another example is I know the overhead of creating and destroying two objects for each comparison is terrible, but the only alternative is to only compare by key (and have the user look up using that key if they want to compare by value?), or to use arrays instead like I really like the two $map->sort(function(Pair $a, Pair $b) {
return $a->value <=> $b->value; // requires two objects per comparison
}); If by key only, that would be: $map->sort(function($a, $b) use ($map) {
return $map[$a] <=> $map[$b]; // requires two lookups per comparison
}); Alternatives:
Even if we go with |
Just a suggestion kinda out of scope, but most probably this is one of the heaviest lines in the callback-sorting methods? It might be that caching the last created pair(s) and reusing them if their refcount dropped to zero would be more efficient (I'm not suggesting a pool - that would involve too much overhead, it could be more basic: if there was |
I don't see a reason why that wouldn't work. I've opted to introduce |
^ Solution. |
I'm trying to separate the "internal" structures from all the zend stuff (zend_object, handlers, ce, etc), which has been smooth sailing so far. I came across a potential situation where this might not work as well as it sounds:
The case is with
$vector->merge($iterable)
. We can take internal shortcuts when the$iterable
is a php-ds structure, eg. anotherphp_ds_vector_t
. In order to access the internal buffer of that vector we have to follow this path: zval => zend_object => structure, ie.((php_ds_vector_t*)(Z_OBJ(z)))->vector
. The problem is that the current scope doesn't have knowledge ofphp_ds_vector_t
, onlyds_vector_t
.The only solution I can think of is to create separate functions, one for each shortcut, and only accept the structures, rather than a generic zval, except for the case where the Z_OBJ_CE is not a php-ds structure (array or other iterable). This would work fine, effectively moving the logic of "is this a vector? is this a deque?" out of the internal structure and into the callee's scope.
So instead of calling
ds_vector_t *ds_vector_merge(ds_vector_t *vector, zval *values)
I could use something likeds_vector_t *ds_vector_merge_vector(ds_vector_t *vector, ds_vector_t *values)
.Is it possible to pass a
void *
along with atypedef
to a function? That way the callee is responsible for extracting the structure, but the structure is responsible for the logic to merge it in. Would remove the need to have multiplex_merge_x
,x_merge_y
,x_merge_z
patterns.The text was updated successfully, but these errors were encountered: