chiark
/
gitweb
/
~mdw
/
catacomb-python
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
438b163
)
bytestring.c: Cache empty and singleton strings.
author
Mark Wooding
<mdw@distorted.org.uk>
Fri, 9 Nov 2018 12:27:42 +0000
(12:27 +0000)
committer
Mark Wooding
<mdw@distorted.org.uk>
Sat, 10 Nov 2018 01:30:58 +0000
(
01:30
+0000)
bytestring.c
patch
|
blob
|
blame
|
history
diff --git
a/bytestring.c
b/bytestring.c
index 8557e3bc8a05af1ef7fdae92f923e92641909eab..124ae9ae7b25dd9846ffe70c3880dec1ea224599 100644
(file)
--- a/
bytestring.c
+++ b/
bytestring.c
@@
-32,6
+32,8
@@
PyTypeObject *bytestring_pytype;
PyTypeObject *bytestring_pytype;
+static PyObject *empty, *bytev[256];
+
static PyObject *allocate(PyTypeObject *ty, size_t n)
{
PyStringObject *x;
static PyObject *allocate(PyTypeObject *ty, size_t n)
{
PyStringObject *x;
@@
-47,6
+49,18
@@
static PyObject *allocate(PyTypeObject *ty, size_t n)
static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
{
PyObject *x;
static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
{
PyObject *x;
+ int ch;
+
+ if (p && ty == bytestring_pytype) {
+ if (!n) {
+ if (!empty) empty = allocate(ty, 0);
+ Py_INCREF(empty); return (empty);
+ } else if (n == 1 && (ch = *(unsigned char *)p) < sizeof(bytev)) {
+ if (!bytev[ch])
+ { bytev[ch] = allocate(ty, 1); *PyString_AS_STRING(bytev[ch]) = ch; }
+ Py_INCREF(bytev[ch]); return (bytev[ch]);
+ }
+ }
x = allocate(ty, n);
if (p) memcpy(PyString_AS_STRING(x), p, n);
x = allocate(ty, n);
if (p) memcpy(PyString_AS_STRING(x), p, n);