-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Buffer protocol proposal #3261
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
Buffer protocol proposal #3261
Conversation
c1dc40a
to
7d5e5e6
Compare
include/ruby/buffer.h
Outdated
/* The original object that have the memory exported via this buffer. | ||
* The consumer of this buffer has the responsibility to call rb_gc_mark | ||
* for preventing this obj collected by GC. */ | ||
VALUE obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
The consumer calls rb_obj_get_buffer(obj, ...)
. So it must know the obj
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some committers suggest to let RubyVM manage the reference of obj
until its buffer will be released.
This obj
may be useful for such a purpose.
88d54af
to
8b8ecfb
Compare
829c6f2
to
07fd1a9
Compare
In python the memoryview constructor is used to build a memoryvieew object that references the original object. The original object implements the buffer protocol.
I think changing the name to memory view for the underlying buffer is confusing. |
@dsisnero Our goal isn’t completely following Python. |
325f8d5
to
604949f
Compare
bool alignment = false; | ||
|
||
const char *p = format; | ||
if (*p == '|') { // alginment specifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I employed |
for specifying element aligning mode.
@akr Could you please review this? |
*members = buf; | ||
*n_members = len; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems no padding after the last component.
I guess that the size of "lc" would be 5.
But it should be 8 to align next item.
memory_view.c
Outdated
uint8_t *ptr = view->data; | ||
|
||
if (view->ndim == 1) { | ||
return ptr + indices[0] * view->item_size; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
view->strides
is ignored.
Is it intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, view->strides
shouldn't be ignored. I'll fix it.
memory_view.c
Outdated
if (view->strides == NULL) { | ||
// row-major contiguous array | ||
for (i = 0; i < view->ndim; ++i) { | ||
ptr += indices[i] * view->shape[i] * view->item_size; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess view->shape[i]
should be view->shape[i+1] * ... * view->shape[ndim-1]
.
Not tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. I should write a test of this function.
|
||
#undef DEF_ALIGNMENT_CONST | ||
|
||
exported_objects = rb_hash_new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that string content-based comparison is not appropriate here.
It may needs compare_by_identity
.
|
||
if (view.shape) { | ||
VALUE shape = rb_ary_new_capa(view.ndim); | ||
rb_hash_aset(hash, sym_shape, shape); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this is incomplete.
shape
is always an empty array.
strides
and sub_offsets
has same issue.
/* 1 for readonly memory, 0 for writable memory. */ | ||
int readonly; | ||
|
||
/* A string to describe the format of an element, or NULL for unsigned byte. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems "item" and "element" are same meaning.
How about using one word for one meaning?
e8a67fe
to
657b021
Compare
2f0d906
to
d264358
Compare
The CI failure isn’t due to this pull request. |
Oh, I rewrote the merge commit message but I don't know why it was rolled back..
|
* Add buffer protocol * Modify for some review comments * Per-object buffer availability * Rename to MemoryView from Buffer and make compilable * Support integral repeat count in memory view format * Support 'x' for padding bytes * Add rb_memory_view_parse_item_format * Check type in rb_memory_view_register * Update dependencies in common.mk * Add test of MemoryView * Add test of rb_memory_view_init_as_byte_array * Add native size format test * Add MemoryView test utilities * Add test of rb_memory_view_fill_contiguous_strides * Skip spaces in format string * Support endianness specifiers * Update documentation * Support alignment * Use RUBY_ALIGNOF * Fix format parser to follow the pack format * Support the _ modifier * Parse count specifiers in get_format_size function. * Use STRUCT_ALIGNOF * Fix test * Fix test * Fix total size for the case with tail padding * Fix rb_memory_view_get_item_pointer * Fix rb_memory_view_parse_item_format again
[Feature #14722]