|
| 1 | + |
| 2 | +import std/strformat |
| 3 | +import ../[ |
| 4 | + pyobjectBase, |
| 5 | + stringobject, |
| 6 | + exceptions, |
| 7 | +] |
| 8 | +import ../abstract/sequence/list |
| 9 | +import ../../Utils/rtarrays |
| 10 | + |
| 11 | +template bytes_join*(S; sep; iterable: PyObject; mutable: bool)#[: PyObject]#{.dirty.} = |
| 12 | + bind RtArray, initRtArray |
| 13 | + bind PySequence_Fast, PySequence_Fast_GET_SIZE, PySequence_Fast_GET_ITEM |
| 14 | + bind newPyStr, newPyAscii, newRuntimeError, newTypeError, newOverflowError |
| 15 | + bind formatValue, fmt |
| 16 | + let |
| 17 | + sepstr = sep.charsView |
| 18 | + seplen = len(sep) |
| 19 | + |
| 20 | + let sequ = PySequence_Fast(iterable, "can only join an iterable") |
| 21 | + retIfExc sequ |
| 22 | + |
| 23 | + let seqlen = PySequence_Fast_GET_SIZE(sequ) |
| 24 | + if seqlen == 0: |
| 25 | + return `newPy S`() |
| 26 | + |
| 27 | + var item: PyObject |
| 28 | + when not mutable: |
| 29 | + if seqlen == 1: |
| 30 | + item = PySequence_Fast_GET_ITEM(sequ, 0) |
| 31 | + if item.`ofExactPy S Object`: |
| 32 | + return item |
| 33 | + |
| 34 | + const GIL_THRESHOLD = 1048576 |
| 35 | + |
| 36 | + #XXX: NIM-BUG: when JS using RtArray: `Error: internal error: ("genAddr: 2", skTemp)` |
| 37 | + # due to `[]=` or `[]` to RtArray |
| 38 | + var buffers = (when defined(js): newSeq else: initRTArray)[Py_buffer](seqlen) |
| 39 | + |
| 40 | + |
| 41 | + #[ Here is the general case. Do a pre-pass to figure out the total |
| 42 | + amount of space we'll need (sz), and see whether all arguments are |
| 43 | + bytes-like. |
| 44 | + ]# |
| 45 | + var sz = 0 |
| 46 | + var nbufs = 0 |
| 47 | + var drop_gil = true |
| 48 | + for i in 0 ..< seqlen: |
| 49 | + item = PySequence_Fast_GET_ITEM(sequ, i) |
| 50 | + proc asgn(b: auto) = |
| 51 | + buffers[i] = init_Py_buffer(to_py_buffer(b), b.len, item) |
| 52 | + if item.ofExactPyBytesObject: |
| 53 | + # Fast path. |
| 54 | + let b = PyBytesObject(item) |
| 55 | + asgn b |
| 56 | + elif item.ofExactPyByteArrayObject: |
| 57 | + let b = PyByteArrayObject(item) |
| 58 | + asgn b |
| 59 | + else: |
| 60 | + template byteslikeExpect = |
| 61 | + return newTypeError newPyStr( |
| 62 | + fmt"sequence item {i}: expected a bytes-like object, {item.typeName:.80s} found" |
| 63 | + ) |
| 64 | + when defined(npython_buffer): |
| 65 | + #TODO:buffer |
| 66 | + let exc: PyBaseErrorObject = PyObject_GetBuffer(item, buffers[i], PyBUF.SIMPLE) |
| 67 | + if not exc.isNil: |
| 68 | + byteslikeExpect |
| 69 | + #[ If the backing objects are mutable, then dropping the GIL |
| 70 | + opens up race conditions where another thread tries to modify |
| 71 | + the object which we hold a buffer on it. Such code has data |
| 72 | + races anyway, but this is a conservative approach that avoids |
| 73 | + changing the behaviour of that data race. |
| 74 | + ]# |
| 75 | + drop_gil = false |
| 76 | + else: |
| 77 | + byteslikeExpect |
| 78 | + |
| 79 | + nbufs = i + 1 # for error cleanup |
| 80 | + let itemlen = buffers[i].len |
| 81 | + template resTooLong = |
| 82 | + return newOverflowError newPyAscii"join() result is too long" |
| 83 | + template `+?=`(s: var int; i: int) = |
| 84 | + if i > int.high - s: resTooLong |
| 85 | + s += i |
| 86 | + sz +?= itemlen |
| 87 | + if i != 0: |
| 88 | + sz +?= seplen |
| 89 | + if seqlen != PySequence_Fast_GET_SIZE(sequ): |
| 90 | + return newRuntimeError newPyAscii"sequence changed size during iteration" |
| 91 | + |
| 92 | + # Allocate result space. |
| 93 | + var res = `newPy S`(sz) |
| 94 | + |
| 95 | + # Catenate everything. |
| 96 | + var p = 0 |
| 97 | + when declared(copyMem): |
| 98 | + template memcpy(_, b; n: int) = copyMem(res.getCharPtr p, b[0].addr, n) |
| 99 | + else: |
| 100 | + template memcpy(_, b; n: int) = |
| 101 | + for i in 0..<n:#p ..< p+n: |
| 102 | + res.items[p+i] = b[i] |
| 103 | + template addbn(b; n: int) = |
| 104 | + memcpy(res[p], b, n) |
| 105 | + p += n |
| 106 | + template addb(bExpr: Py_buffer) = |
| 107 | + let b = bExpr |
| 108 | + addbn(b.buf, b.len) |
| 109 | + if sz < GIL_THRESHOLD: |
| 110 | + drop_gil = false # Benefits are likely outweighed by the overheads |
| 111 | + |
| 112 | + #TODO:threads |
| 113 | + const hasPyThrd = defined(npython_threads) |
| 114 | + when hasPyThrd: |
| 115 | + var save: PyThreadState |
| 116 | + if drop_gil: save = PyEval_SaveThread() |
| 117 | + |
| 118 | + if seplen == 0: |
| 119 | + # fast path |
| 120 | + for i in 0..<nbufs: |
| 121 | + addb buffers[i] |
| 122 | + else: |
| 123 | + if nbufs > 0: |
| 124 | + addb(buffers[0]) |
| 125 | + addbn(sepstr, seplen) |
| 126 | + for i in 1 ..< nbufs: |
| 127 | + addb(buffers[i]) |
| 128 | + |
| 129 | + when hasPyThrd: |
| 130 | + if drop_gil: PyEval_RestoreThread(save) |
| 131 | + |
| 132 | + # RtArray's `=destroy` will call buffer's destroy |
| 133 | + #for b in buffers: PyBuffer_Release(b) |
| 134 | + #if use_non_static: PyMem_Free(buffers) |
| 135 | + return res |
| 136 | + |
| 137 | + |
0 commit comments