2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-31 22:35:15 +00:00

Add optional C extension wrapper for Python JSON parsing

The pure Python in-tree JSON parser is *much* slower than the
in-tree C JSON parser. A local test parsing a 100Mb JSON file
showed the Python version taking 270 seconds. With the C wrapper,
it took under 4 seconds.

The C extension will be used automatically if it can be built. If
the extension fails to build, a warning is displayed and the build
is restarted without the extension.

The Serializer class is replaced with Python's built-in
JSON library since the ability to process chunked data is not
needed in that case.

The extension should work with both Python 2.7 and Python 3.3+.

Signed-off-by: Terry Wilson <twilson@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
Terry Wilson
2016-06-08 08:55:14 -05:00
committed by Ben Pfaff
parent 2c362f17d6
commit c63b04d678
5 changed files with 332 additions and 3 deletions

View File

@@ -13,6 +13,10 @@
from __future__ import print_function
import sys
from distutils.command.build_ext import build_ext
from distutils.errors import CCompilerError, DistutilsExecError, \
DistutilsPlatformError
import setuptools
VERSION = "unknown"
@@ -25,8 +29,33 @@ except IOError:
file=sys.stderr)
sys.exit(-1)
ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
if sys.platform == 'win32':
ext_errors += (IOError, ValueError)
setuptools.setup(
class BuildFailed(Exception):
pass
class try_build_ext(build_ext):
# This class allows C extension building to fail
# NOTE: build_ext is not a new-style class
def run(self):
try:
build_ext.run(self)
except DistutilsPlatformError:
raise BuildFailed()
def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except ext_errors:
raise BuildFailed()
setup_args = dict(
name='ovs',
description='Open vSwitch library',
version=VERSION,
@@ -46,5 +75,23 @@ setuptools.setup(
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
]
],
ext_modules=[setuptools.Extension("ovs._json", sources=["ovs/_json.c"],
libraries=['openvswitch'])],
cmdclass={'build_ext': try_build_ext},
)
try:
setuptools.setup(**setup_args)
except BuildFailed:
BUILD_EXT_WARNING = ("WARNING: The C extension could not be compiled, "
"speedups are not enabled.")
print("*" * 75)
print(BUILD_EXT_WARNING)
print("Failure information, if any, is above.")
print("Retrying the build without the C extension.")
print("*" * 75)
del(setup_args['cmdclass'])
del(setup_args['ext_modules'])
setuptools.setup(**setup_args)