Skip to content
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

Slower Than C #310

Open
arshidkv12 opened this issue Jan 24, 2024 · 8 comments
Open

Slower Than C #310

arshidkv12 opened this issue Jan 24, 2024 · 8 comments

Comments

@arshidkv12
Copy link

Hi
I tested with xdebug profile option with grind cache.
Functions are slower than c. How to speed up?

@ryangjchandler
Copy link

The difference in speed is likely due to converting between PHP types (Zval) and Rust types such as String, Vec, etc.

@joehoyle
Copy link
Collaborator

I think to test this assumption it could be a good idea to benchmark:

#[php_function]
fn should_be_fast(&Zval zval ) -> Zval {
    zval.shallow_clone()
}

I suspect we might actually have several inefficiencies in the calling bridging, unnecessarily allocating etc.

@ryangjchandler
Copy link

That would just be benchmarking a direct clone of a Zval though right? Not the Vec<T> allocations, etc?

@joehoyle
Copy link
Collaborator

I'm not sure, I think the #[php_function] may generate code that has additional allocations etc in.

@EdmondDantes
Copy link
Contributor

The current approach is expected to introduce some performance overhead:

Calling a function from a wrapper function.
Copying variables to the stack.
In other words, if I create a function whose body does very little useful work and plan to call this function frequently, I will inevitably lose about 50% of performance.

I'm not sure how common this case is, but if necessary, it's possible to create a function without a wrapper and work directly with zend_execute_data* execute_data and zval* return_val.

@Xenira
Copy link
Collaborator

Xenira commented Jan 8, 2025

For calling functions more efficiently i use something like this: https://github.com/Xenira/php-iter-tools/blob/rust-impl/src/lib.rs#L1739

@arshidkv12
Copy link
Author

I think zig is best for it https://github.com/arshidkv12/zig-php

@EdmondDantes
Copy link
Contributor

@arshidkv12

export fn hello_world(execute_data: ?*php.zend_execute_data, return_value: ?*php.zval) void {
    _ = execute_data;
    _ = return_value;
    _ = php.php_printf("Hello from ZIG!\n");
}

@Xenira

That's exactly what I'm talking about – the same code can be written in Rust without any performance loss. We need to consider this implementation option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants