Skip to content

Commit 0c264a1

Browse files
jakirkhamalimanfoo
authored andcommitted
Use (new) buffer protocol in *JSON's decode (#151)
* Use codecs.decode in JSON.decode to avoid a copy In Python 2/3, `codecs.decode` performs decoding analogous to `bytes.decode` except that it is not limited to `bytes`. In fact, it can take anything that supports the (new) buffer protocol. Thus we can avoid copying the data and instead only take an `ndarray` view onto it before passing the buffer to `codecs.decode`. Should improve performance a bit by avoiding this copy. * Note JSON (new) buffer protocol support
1 parent be0a763 commit 0c264a1

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Release notes
1515
* Use (new) buffer protocol in ``MsgPack`` codec `decode()` method.
1616
By :user:`John Kirkham <jakirkham>`, :issue:`148`.
1717

18+
* Use (new) buffer protocol in ``JSON`` codec `decode()` method.
19+
By :user:`John Kirkham <jakirkham>`, :issue:`151`.
20+
1821
* Avoid copying into data in ``GZip``'s `decode()` method on Python 2.
1922
By :user:`John Kirkham <jakirkham>`, :issue:`152`.
2023

numcodecs/json.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, division
3+
import codecs
34
import json as _json
45
import textwrap
56

@@ -8,7 +9,7 @@
89

910

1011
from .abc import Codec
11-
from .compat import ensure_bytes
12+
from .compat import ensure_contiguous_ndarray
1213

1314

1415
class JSON(Codec):
@@ -63,8 +64,8 @@ def encode(self, buf):
6364
return self._encoder.encode(items).encode(self._text_encoding)
6465

6566
def decode(self, buf, out=None):
66-
buf = ensure_bytes(buf)
67-
items = self._decoder.decode(buf.decode(self._text_encoding))
67+
buf = ensure_contiguous_ndarray(buf)
68+
items = self._decoder.decode(codecs.decode(buf, self._text_encoding))
6869
dec = np.empty(items[-1], dtype=items[-2])
6970
dec[:] = items[:-2]
7071
if out is not None:
@@ -125,8 +126,8 @@ def encode(self, buf):
125126
return self._encoder.encode(items).encode(self._text_encoding)
126127

127128
def decode(self, buf, out=None):
128-
buf = ensure_bytes(buf)
129-
items = self._decoder.decode(buf.decode(self._text_encoding))
129+
buf = ensure_contiguous_ndarray(buf)
130+
items = self._decoder.decode(codecs.decode(buf, self._text_encoding))
130131
dec = np.array(items[:-1], dtype=items[-1])
131132
if out is not None:
132133
np.copyto(out, dec)

0 commit comments

Comments
 (0)