Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Attempt to be more performant on PyPy (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkowl authored Jun 28, 2018
1 parent 72d2143 commit 6350bf9
Show file tree
Hide file tree
Showing 40 changed files with 79 additions and 62 deletions.
1 change: 1 addition & 0 deletions changelog.d/3462.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Synapse now uses the best performing JSON encoder/decoder according to your runtime (simplejson on CPython, stdlib json on PyPy).
3 changes: 2 additions & 1 deletion synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import logging

import simplejson as json
from canonicaljson import json

from six import iteritems
from six.moves import http_client

Expand Down
3 changes: 2 additions & 1 deletion synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from synapse.types import UserID, RoomID
from twisted.internet import defer

import simplejson as json
from canonicaljson import json

import jsonschema
from jsonschema import FormatChecker

Expand Down
2 changes: 1 addition & 1 deletion synapse/crypto/keyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from twisted.internet.protocol import Factory
from twisted.internet import defer, reactor
from synapse.http.endpoint import matrix_federation_endpoint
import simplejson as json
from canonicaljson import json
import logging


Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
import logging

import simplejson as json
from canonicaljson import json
from twisted.internet import defer

from synapse.api.errors import AuthError, FederationError, SynapseError, NotFoundError
Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from twisted.internet import defer, threads

from canonicaljson import json

from ._base import BaseHandler
from synapse.api.constants import LoginType
from synapse.api.errors import (
Expand All @@ -32,7 +34,6 @@
import logging
import bcrypt
import pymacaroons
import simplejson
import attr

import synapse.util.stringutils as stringutils
Expand Down Expand Up @@ -403,7 +404,7 @@ def _check_recaptcha(self, authdict, clientip):
except PartialDownloadError as pde:
# Twisted is silly
data = pde.response
resp_body = simplejson.loads(data)
resp_body = json.loads(data)

if 'success' in resp_body:
# Note that we do NOT check the hostname here: we explicitly
Expand Down
5 changes: 2 additions & 3 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import simplejson as json
import logging

from canonicaljson import encode_canonical_json
from canonicaljson import encode_canonical_json, json
from twisted.internet import defer
from six import iteritems

Expand Down Expand Up @@ -357,7 +356,7 @@ def _exception_to_failure(e):
# include ConnectionRefused and other errors
#
# Note that some Exceptions (notably twisted's ResponseFailed etc) don't
# give a string for e.message, which simplejson then fails to serialize.
# give a string for e.message, which json then fails to serialize.
return {
"status": 503, "message": str(e.message),
}
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import logging

import simplejson as json
from canonicaljson import json

from twisted.internet import defer

Expand Down
5 changes: 2 additions & 3 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import simplejson
import sys

from canonicaljson import encode_canonical_json
from canonicaljson import encode_canonical_json, json
import six
from six import string_types, itervalues, iteritems
from twisted.internet import defer
Expand Down Expand Up @@ -797,7 +796,7 @@ def handle_new_client_event(
# Ensure that we can round trip before trying to persist in db
try:
dump = frozendict_json_encoder.encode(event.content)
simplejson.loads(dump)
json.loads(dump)
except Exception:
logger.exception("Failed to encode content: %r", event.content)
raise
Expand Down
2 changes: 1 addition & 1 deletion synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from six import StringIO

from prometheus_client import Counter
import simplejson as json
from canonicaljson import json
import logging
import urllib

Expand Down
3 changes: 1 addition & 2 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from synapse.util.logcontext import make_deferred_yieldable
import synapse.util.retryutils

from canonicaljson import encode_canonical_json
from canonicaljson import encode_canonical_json, json

from synapse.api.errors import (
SynapseError, Codes, HttpResponseException, FederationDeniedError,
Expand All @@ -36,7 +36,6 @@
from signedjson.sign import sign_json

import cgi
import simplejson as json
import logging
import random
import sys
Expand Down
5 changes: 2 additions & 3 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import synapse.events

from canonicaljson import (
encode_canonical_json, encode_pretty_printed_json
encode_canonical_json, encode_pretty_printed_json, json
)

from twisted.internet import defer
Expand All @@ -41,7 +41,6 @@
import collections
import logging
import urllib
import simplejson

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -410,7 +409,7 @@ def respond_with_json(request, code, json_object, send_cors=False,
if canonical_json or synapse.events.USE_FROZEN_DICTS:
json_bytes = encode_canonical_json(json_object)
else:
json_bytes = simplejson.dumps(json_object)
json_bytes = json.dumps(json_object)

return respond_with_json_bytes(
request, code, json_bytes,
Expand Down
6 changes: 4 additions & 2 deletions synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from synapse.api.errors import SynapseError, Codes

import logging
import simplejson

from canonicaljson import json


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -171,7 +173,7 @@ def parse_json_value_from_request(request, allow_empty_body=False):
return None

try:
content = simplejson.loads(content_bytes)
content = json.loads(content_bytes)
except Exception as e:
logger.warn("Unable to parse JSON: %s", e)
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
Expand Down
3 changes: 2 additions & 1 deletion synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def collect(self):
yield cm


REGISTRY.register(GCCounts())
if not running_on_pypy:
REGISTRY.register(GCCounts())

#
# Twisted reactor metrics
Expand Down
16 changes: 10 additions & 6 deletions synapse/replication/tcp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
"""

import logging
import simplejson
import platform

if platform.python_implementation() == "PyPy":
import json
_json_encoder = json.JSONEncoder()
else:
import simplejson as json
_json_encoder = json.JSONEncoder(namedtuple_as_object=False)

logger = logging.getLogger(__name__)

_json_encoder = simplejson.JSONEncoder(namedtuple_as_object=False)


class Command(object):
"""The base command class.
Expand Down Expand Up @@ -102,7 +106,7 @@ def from_line(cls, line):
return cls(
stream_name,
None if token == "batch" else int(token),
simplejson.loads(row_json)
json.loads(row_json)
)

def to_line(self):
Expand Down Expand Up @@ -300,7 +304,7 @@ def __init__(self, cache_func, keys):
def from_line(cls, line):
cache_func, keys_json = line.split(" ", 1)

return cls(cache_func, simplejson.loads(keys_json))
return cls(cache_func, json.loads(keys_json))

def to_line(self):
return " ".join((
Expand Down Expand Up @@ -329,7 +333,7 @@ def __init__(self, user_id, access_token, ip, user_agent, device_id, last_seen):
def from_line(cls, line):
user_id, jsn = line.split(" ", 1)

access_token, ip, user_agent, device_id, last_seen = simplejson.loads(jsn)
access_token, ip, user_agent, device_id, last_seen = json.loads(jsn)

return cls(
user_id, access_token, ip, user_agent, device_id, last_seen
Expand Down
3 changes: 2 additions & 1 deletion synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from .base import ClientV1RestServlet, client_path_patterns

import simplejson as json
from canonicaljson import json

import urllib
from six.moves.urllib import parse as urlparse

Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from six.moves.urllib import parse as urlparse

import logging
import simplejson as json
from canonicaljson import json

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/client/v2_alpha/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import itertools
import logging

import simplejson as json
from canonicaljson import json

logger = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion synapse/rest/media/v0/content_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
from twisted.protocols.basic import FileSender
from twisted.web import server, resource

from canonicaljson import json

import base64
import simplejson as json
import logging
import os
import re
Expand Down
3 changes: 2 additions & 1 deletion synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import shutil
import sys
import traceback
import simplejson as json

from canonicaljson import json

from six.moves import urllib_parse as urlparse
from six import string_types
Expand Down
3 changes: 2 additions & 1 deletion synapse/storage/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
from synapse.util.caches.stream_change_cache import StreamChangeCache
from synapse.util.caches.descriptors import cached, cachedInlineCallbacks

from canonicaljson import json

import abc
import simplejson as json
import logging

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# limitations under the License.
import logging
import re
import simplejson as json
from twisted.internet import defer
from canonicaljson import json

from synapse.appservice import AppServiceTransaction
from synapse.config.appservice import load_appservices
Expand Down
3 changes: 2 additions & 1 deletion synapse/storage/background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from twisted.internet import defer

import simplejson as json
from canonicaljson import json

import logging

logger = logging.getLogger(__name__)
Expand Down
13 changes: 7 additions & 6 deletions synapse/storage/deviceinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# limitations under the License.

import logging
import simplejson

from canonicaljson import json

from twisted.internet import defer

Expand Down Expand Up @@ -85,7 +86,7 @@ def add_messages_txn(txn, now_ms, stream_id):
)
rows = []
for destination, edu in remote_messages_by_destination.items():
edu_json = simplejson.dumps(edu)
edu_json = json.dumps(edu)
rows.append((destination, stream_id, now_ms, edu_json))
txn.executemany(sql, rows)

Expand Down Expand Up @@ -177,7 +178,7 @@ def _add_messages_to_local_device_inbox_txn(self, txn, stream_id,
" WHERE user_id = ?"
)
txn.execute(sql, (user_id,))
message_json = simplejson.dumps(messages_by_device["*"])
message_json = json.dumps(messages_by_device["*"])
for row in txn:
# Add the message for all devices for this user on this
# server.
Expand All @@ -199,7 +200,7 @@ def _add_messages_to_local_device_inbox_txn(self, txn, stream_id,
# Only insert into the local inbox if the device exists on
# this server
device = row[0]
message_json = simplejson.dumps(messages_by_device[device])
message_json = json.dumps(messages_by_device[device])
messages_json_for_user[device] = message_json

if messages_json_for_user:
Expand Down Expand Up @@ -253,7 +254,7 @@ def get_new_messages_for_device_txn(txn):
messages = []
for row in txn:
stream_pos = row[0]
messages.append(simplejson.loads(row[1]))
messages.append(json.loads(row[1]))
if len(messages) < limit:
stream_pos = current_stream_id
return (messages, stream_pos)
Expand Down Expand Up @@ -389,7 +390,7 @@ def get_new_messages_for_remote_destination_txn(txn):
messages = []
for row in txn:
stream_pos = row[0]
messages.append(simplejson.loads(row[1]))
messages.append(json.loads(row[1]))
if len(messages) < limit:
stream_pos = current_stream_id
return (messages, stream_pos)
Expand Down
3 changes: 2 additions & 1 deletion synapse/storage/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import simplejson as json

from twisted.internet import defer

from synapse.api.errors import StoreError
from ._base import SQLBaseStore, Cache
from synapse.util.caches.descriptors import cached, cachedList, cachedInlineCallbacks

from canonicaljson import json

from six import itervalues, iteritems

logger = logging.getLogger(__name__)
Expand Down
Loading

0 comments on commit 6350bf9

Please sign in to comment.