-
Notifications
You must be signed in to change notification settings - Fork 503
Add Cython BinaryEncoder for Avro block encoding #3303
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
Open
rynewang
wants to merge
1
commit into
apache:main
Choose a base branch
from
rynewang:perf/cython-avro-encoder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from typing import Any | ||
| from uuid import UUID | ||
|
|
||
| class CythonBinaryEncoder: | ||
| def __init__(self) -> None: ... | ||
| def getvalue(self) -> bytes: ... | ||
| def write(self, b: bytes) -> None: ... | ||
| def write_boolean(self, v: bool) -> None: ... | ||
| def write_int(self, v: int) -> None: ... | ||
| def write_float(self, v: float) -> None: ... | ||
| def write_double(self, v: float) -> None: ... | ||
| def write_bytes(self, b: bytes) -> None: ... | ||
| def write_utf8(self, s: str) -> None: ... | ||
| def write_uuid(self, uuid: UUID) -> None: ... | ||
| def write_unknown(self, _: Any) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import cython | ||
| from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING, PyBytes_GET_SIZE | ||
| from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free | ||
| from libc.string cimport memcpy | ||
| from libc.stdint cimport uint8_t, uint64_t, int64_t | ||
|
|
||
| from pyiceberg.avro import STRUCT_DOUBLE, STRUCT_FLOAT | ||
|
|
||
| cdef uint64_t _INITIAL_CAPACITY = 1024 | ||
|
|
||
|
|
||
| @cython.final | ||
| cdef class CythonBinaryEncoder: | ||
| """In-memory BinaryEncoder that writes to a growable C buffer. | ||
|
|
||
| Drop-in replacement for BinaryEncoder for the block-encoding path: | ||
| exposes the same write_* methods the Writer tree calls, plus | ||
| getvalue() to materialise the encoded bytes once at the end. | ||
| """ | ||
|
|
||
| cdef unsigned char *_data | ||
| cdef uint64_t _size | ||
| cdef uint64_t _capacity | ||
|
|
||
| def __cinit__(self): | ||
| self._data = <unsigned char *> PyMem_Malloc(_INITIAL_CAPACITY) | ||
| if not self._data: | ||
| raise MemoryError() | ||
| self._size = 0 | ||
| self._capacity = _INITIAL_CAPACITY | ||
|
|
||
| def __dealloc__(self): | ||
| PyMem_Free(self._data) | ||
|
|
||
| cdef inline int _ensure(self, uint64_t n) except -1: | ||
| cdef uint64_t need = self._size + n | ||
| if need <= self._capacity: | ||
| return 0 | ||
| cdef uint64_t cap = self._capacity | ||
| while cap < need: | ||
| cap <<= 1 | ||
| cdef unsigned char *grown = <unsigned char *> PyMem_Realloc(self._data, cap) | ||
| if not grown: | ||
| raise MemoryError() | ||
| self._data = grown | ||
| self._capacity = cap | ||
| return 0 | ||
|
|
||
| cpdef bytes getvalue(self): | ||
| return PyBytes_FromStringAndSize(<char *> self._data, self._size) | ||
|
|
||
| cpdef void write(self, bytes b): | ||
| cdef Py_ssize_t n = PyBytes_GET_SIZE(b) | ||
| self._ensure(n) | ||
| memcpy(self._data + self._size, PyBytes_AS_STRING(b), n) | ||
| self._size += n | ||
|
|
||
| cpdef void write_boolean(self, bint v): | ||
| self._ensure(1) | ||
| self._data[self._size] = 1 if v else 0 | ||
| self._size += 1 | ||
|
|
||
| cpdef void write_int(self, int64_t v): | ||
| # zigzag then base-128 varint; a 64-bit value needs at most 10 bytes | ||
| self._ensure(10) | ||
| cdef uint64_t uv = <uint64_t> v | ||
| cdef uint64_t datum = (uv << 1) ^ (0 - (uv >> 63)) | ||
| cdef unsigned char *p = self._data + self._size | ||
| while datum & <uint64_t> ~0x7F: | ||
| p[0] = <uint8_t> ((datum & 0x7F) | 0x80) | ||
| p += 1 | ||
| datum >>= 7 | ||
| p[0] = <uint8_t> datum | ||
| p += 1 | ||
| self._size = p - self._data | ||
|
|
||
| def write_float(self, v: float) -> None: | ||
| self.write(STRUCT_FLOAT.pack(v)) | ||
|
|
||
| def write_double(self, v: float) -> None: | ||
| self.write(STRUCT_DOUBLE.pack(v)) | ||
|
|
||
| cpdef void write_bytes(self, b): | ||
| cdef bytes bb = bytes(b) if type(b) is not bytes else b | ||
| cdef Py_ssize_t n = PyBytes_GET_SIZE(bb) | ||
| self.write_int(n) | ||
| self._ensure(n) | ||
| memcpy(self._data + self._size, PyBytes_AS_STRING(bb), n) | ||
| self._size += n | ||
|
|
||
| def write_utf8(self, s) -> None: | ||
| self.write_bytes(s.encode("utf-8")) | ||
|
|
||
| def write_uuid(self, uuid) -> None: | ||
| cdef bytes b = uuid.bytes | ||
| if PyBytes_GET_SIZE(b) != 16: | ||
| raise ValueError(f"Expected UUID to have 16 bytes, got: len({b!r})") | ||
| self._ensure(16) | ||
| memcpy(self._data + self._size, PyBytes_AS_STRING(b), 16) | ||
| self._size += 16 | ||
|
|
||
| def write_unknown(self, _) -> None: | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
minor: should this use UTF8 from pyiceberg.typedef?