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

Commit

Permalink
Merge pull request #62 from plone/optimization
Browse files Browse the repository at this point in the history
Optimization
  • Loading branch information
vangheem committed Feb 2, 2017
2 parents 2ce29d2 + f6dd141 commit d98b300
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 11 deletions.
5 changes: 2 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"databases": [
{
"zodb1": {
"storage": "ZEO",
"address": "127.0.0.1",
"port": 8090,
"storage": "ZODB",
"path": "./data",
"configuration": {
"pool_size": 100,
"cache_size": 100
Expand Down
5 changes: 4 additions & 1 deletion src/plone.server/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
1.0a11 (unreleased)
-------------------

- Nothing changed yet.
New features:

- Adding C optimization for get_current_request
[ramonnb]


1.0a10 (2017-02-01)
Expand Down
2 changes: 1 addition & 1 deletion src/plone.server/plone/server/catalog/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from plone.server.interfaces import IResource
from plone.server.interfaces import ISite
from plone.server.transactions import get_current_request
from plone.server.transactions import RequestNotFound
from plone.server.exceptions import RequestNotFound
from plone.server.transactions import tm
from zope.component import queryUtility
from zope.lifecycleevent.interfaces import IObjectAddedEvent
Expand Down
5 changes: 5 additions & 0 deletions src/plone.server/plone/server/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ def __repr__(self):
return "Precondition Failed {precondition} on {path}".format(
precondition=self.precondition,
path=self.container)


class RequestNotFound(Exception):
"""Lookup for the current request for request aware transactions failed
"""
98 changes: 98 additions & 0 deletions src/plone.server/plone/server/optimizations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <Python.h>
#include <frameobject.h>

static PyObject *RequestNotFound, *RequestHandler;

/* current_request frame obtainer */


static PyObject*
current_request()
{
PyFrameObject *f = PyThreadState_GET()->frame;
int found = 0;
PyObject *request = NULL;
PyObject *self;
while (found == 0 && f != NULL) {
if (PyFrame_FastToLocalsWithError(f) < 0)
return NULL;

if (PyDict_CheckExact(f->f_locals)) {
self = PyDict_GetItem(f->f_locals, PyUnicode_FromString("self"));

if (self != NULL) {

if (PyObject_HasAttr(self, PyUnicode_FromString("request"))) {
found = 1;
request = PyObject_GetAttr(self, PyUnicode_FromString("request"));
}
if (PyObject_IsInstance(self, RequestHandler)) {
found = 1;
request = PyDict_GetItem(f->f_locals, PyUnicode_FromString("request"));
}
}

}
f = f->f_back;
}


if (f == NULL) {
PyErr_SetString(RequestNotFound,
"Could not found the request");
return NULL;
}

Py_INCREF(request);
return (PyObject*)request;
}


static PyMethodDef OptimizationsMethods[] =
{
{"get_current_request", current_request, METH_VARARGS, "Get the request"},
{NULL, NULL, 0, NULL}
};


PyMODINIT_FUNC
PyInit_optimizations(void)
{

PyObject* m;

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"optimizations",
"Optimizations plone server", -1, OptimizationsMethods, };
PyObject* ob = PyModule_Create(&moduledef);

if (ob == NULL)
{
return NULL;
}

if ((m = PyImport_ImportModule("plone.server.exceptions")) == NULL)
{
return NULL;
}

RequestNotFound = PyObject_GetAttrString(m, "RequestNotFound");
if (RequestNotFound == NULL)
{
return NULL;
}
Py_DECREF(m);

if ((m = PyImport_ImportModule("aiohttp.web_server")) == NULL)
{
return NULL;
}
if ((RequestHandler = PyObject_GetAttrString(m, "RequestHandler")) == NULL)
{
return NULL;
}
Py_DECREF(m);

return ob;
}
14 changes: 9 additions & 5 deletions src/plone.server/plone/server/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from concurrent.futures import ThreadPoolExecutor
from plone.server.interfaces import SHARED_CONNECTION
from plone.server.utils import get_authenticated_user_id
from plone.server.exceptions import RequestNotFound
from transaction._manager import _new_transaction
from transaction.interfaces import ISavepoint
from transaction.interfaces import ISavepointDataManager
Expand Down Expand Up @@ -375,11 +376,6 @@ def sync(request):
request.conn.executor, *args, **kwargs)


class RequestNotFound(Exception):
"""Lookup for the current request for request aware transactions failed
"""


def get_current_request():
"""Return the current request by heuristically looking it up from stack
"""
Expand All @@ -398,6 +394,14 @@ def get_current_request():
raise RequestNotFound(RequestNotFound.__doc__)


try:
import plone.server.optimizations # noqa
except (ImportError, AttributeError): # pragma NO COVER PyPy / PURE_PYTHON
pass
else:
from plone.server.optimizations import get_current_request # noqa


def synccontext(context):
"""Return connections asyncio executor instance (from context) to be used
together with "await" syntax to queue or commit to be executed in
Expand Down
21 changes: 20 additions & 1 deletion src/plone.server/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# -*- coding: utf-8 -*-
from setuptools import find_packages
from setuptools import setup

from distutils.core import Extension
import os
import sys
import platform


py_impl = getattr(platform, 'python_implementation', lambda: None)
pure_python = os.environ.get('PURE_PYTHON', False)
is_pypy = py_impl() == 'PyPy'
is_jython = 'java' in sys.platform

if pure_python or is_pypy or is_jython:
ext_modules = []
else:
ext_modules = [
Extension(
'plone.server.optimizations',
sources=[os.path.join(
'plone', 'server',
'optimizations.c')])
]

setup(
name='plone.server',
Expand Down Expand Up @@ -35,6 +53,7 @@
package_dir=None if os.path.isdir('plone') else {'': os.path.join('src', 'plone.server')}, # noqa
packages=find_packages('./' if os.path.isdir('plone') else os.path.join('src', 'plone.server'), exclude=['ez_setup']), # noqa
namespace_packages=['plone'],
ext_modules=ext_modules,
install_requires=[
'aiohttp',
'jsonschema',
Expand Down

0 comments on commit d98b300

Please sign in to comment.