Ability to pick PHP value by reference in FFI.
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C
- Star
- 40.4k
- Fork
- 8.2k
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 96
Mô tả
Description
HI!
Currently the only way convert php value to FFI value is memcpy (or other ways with memory copying involved).
It could be wasting if we for example received large chunk from socket and need to deal with it through FFI.
For example,
/* Initial */
echo memory_get_usage(); // 348504
/* Got chunk of data */
$data = str_repeat('1', 512 << 10);
echo memory_get_usage(); // 876992
/* Defining the FFI type */
$type = FFI::arrayType(FFI::type('char'), [512 << 10]);
echo memory_get_usage(); // 877184
/* Allocating memory for type */
$ffiValue = FFI::new($type);
echo memory_get_usage(); // 1401984
/* And memcpy */
FFI::memcpy($ffiValue, $data, 512 << 10);
echo memory_get_usage(); 1402048
It would be great to have ability to reflect value from PHP to FFI using it by reference like so:
$data = str_repeat('1', 512 << 10);
/* @var FFI\CData $ffiValue */
$ffiValueCharArray = FFI::represent('char[]', &$data, size: 512 << 10); // No additional memory allocations for $data content
$ffiValueCharArray [1] = 5;
var_dump($data[1]); // $data changed by reference
$ffiValueInt32 = FFI::represent('int32_t', &$data, size: 4, offset: 12);
var_dump($ffiValueInt32->cdata); // substr($data, 12, 4) === "1111", we will get here 825307441
$ffiValueInt32->cdata = 10;
var_dump(bin2hex(substr($data, 12, 4))); // "0a000000"
Also could work not only with strings:
$int = 10;
$ffiValueInt64 = FFI::represent('int64_t', &$int); // length argument could be omitted if cast performed to fixed size type
var_dump($ffiValueInt64->cdata); // 10;
$ffiValueInt64->cdata = 20;
var_dump($int); // 20;
$readOverBoundary = FFI::represent('char[]', &$int, 10); // Exception: attempt to read over boundary
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với các entry point FFI::type, FFI::arrayType, FFI::new và FFI::memcpy được minh họa trong các ví dụ, sau đó xác định xem ngữ nghĩa tham chiếu được đề xuất có phù hợp với mô hình giá trị và bộ nhớ của PHP hay không. Công việc được xem là hoàn tất khi đạt được thiết kế cho hành vi FFI::represent được maintainer phê duyệt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, php
- Lĩnh vực
- backend
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100