Skip to content

Commit 2010833

Browse files
grigiabondar
authored andcommitted
Performance tuning (#51)
* __init__ restructure give 25-30% speedup in select benchmark. I also cleared up a few lint warnings. * Fix check * v0.10.8 * Fix bad refactor * Allow NPE to pass through
1 parent 7eaf937 commit 2010833

File tree

13 files changed

+60
-57
lines changed

13 files changed

+60
-57
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Changelog
22
=========
3+
4+
0.10.8
5+
------
6+
- Performance fixes from ``pypika`` and object retrieval
7+
38
0.10.7
49
------
510
- Fixed SQLite relative db path and :memory: now also works

requirements-dev.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ certifi==2018.8.24 # via requests
1919
cffi==1.11.5 # via cryptography
2020
chardet==3.0.4 # via requests
2121
ciso8601==2.0.1
22-
click==6.7 # via pip-tools
22+
click==7.0 # via pip-tools
2323
cloud-sptheme==1.9.4
2424
colorama==0.3.9 # via green
2525
coverage==4.5.1 # via coveralls, green
@@ -41,7 +41,7 @@ markupsafe==1.0 # via jinja2
4141
mccabe==0.6.1 # via flake8, pylint
4242
mypy-extensions==0.4.1 # via mypy
4343
mypy==0.630
44-
packaging==17.1 # via sphinx
44+
packaging==18.0 # via sphinx
4545
pbr==4.2.0 # via stevedore
4646
pip-tools==3.0.0
4747
pluggy==0.7.1 # via tox
@@ -53,7 +53,7 @@ pygments==2.2.0
5353
pylint==2.1.1
5454
pymysql==0.9.2 # via aiomysql
5555
pyparsing==2.2.1 # via packaging
56-
pypika==0.15.5
56+
pypika==0.15.6
5757
pytz==2018.5 # via babel
5858
pyyaml==3.13 # via bandit
5959
requests==2.19.1 # via coveralls, sphinx

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pypika>=0.15.4,<1.0
1+
pypika>=0.15.6,<1.0
22
ciso8601>=2.0
33
aiocontextvars==0.1.2;python_version<"3.7"
44
aiosqlite>=0.6.0

tortoise/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,4 @@ async def do_stuff():
374374
loop.run_until_complete(Tortoise.close_connections())
375375

376376

377-
__version__ = "0.10.7"
377+
__version__ = "0.10.8"

tortoise/backends/asyncpg/client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class AsyncpgDBClient(BaseDBAsyncClient):
1818
executor_class = AsyncpgExecutor
1919
schema_generator = AsyncpgSchemaGenerator
2020

21-
def __init__(self, user, password, database, host, port, *args, **kwargs):
22-
super().__init__(*args, **kwargs)
21+
def __init__(self, user, password, database, host, port, **kwargs):
22+
super().__init__(**kwargs)
2323

2424
self.user = user
2525
self.password = password
@@ -107,7 +107,7 @@ def _in_transaction(self):
107107
else:
108108
return self._transaction_class(self.connection_name, pool=self._db_pool)
109109

110-
async def execute_query(self, query):
110+
async def execute_query(self, query, get_inserted_id=False):
111111
try:
112112
async with self.acquire_connection() as connection:
113113
self.log.debug(query)
@@ -153,6 +153,7 @@ def __init__(self, connection_name, pool=None, connection=None):
153153
self._transaction_class = self.__class__
154154
self._old_context_value = None
155155
self.connection_name = connection_name
156+
self.transaction = None
156157

157158
def acquire_connection(self):
158159
return ConnectionWrapper(self._connection)

tortoise/backends/base/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def acquire_connection(self):
3737
def _in_transaction(self):
3838
raise NotImplementedError() # pragma: nocoverage
3939

40-
async def execute_query(self, query):
40+
async def execute_query(self, query, get_inserted_id=False):
4141
raise NotImplementedError() # pragma: nocoverage
4242

4343
async def execute_script(self, script):
@@ -62,10 +62,12 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
6262

6363

6464
class SingleConnectionWrapper(BaseDBAsyncClient):
65+
# pylint: disable=W0223,W0231
66+
6567
def __init__(self, connection_name, connection, closing_callback=None):
68+
self.log = logging.getLogger('db_client')
6669
self.connection_name = connection_name
6770
self.connection = connection
68-
self.log = logging.getLogger('db_client')
6971
self.single_connection = True
7072
self.closing_callback = closing_callback
7173

tortoise/backends/base/config_generator.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
DB_LOOKUP = {
1111
'postgres': {
1212
'engine': 'tortoise.backends.asyncpg',
13-
'vars': {
13+
'vmap': {
1414
'path': 'database',
1515
'hostname': 'host',
1616
'port': 'port',
@@ -21,13 +21,13 @@
2121
'sqlite': {
2222
'engine': 'tortoise.backends.sqlite',
2323
'skip_first_char': False,
24-
'vars': {
24+
'vmap': {
2525
'path': 'file_path',
2626
},
2727
},
2828
'mysql': {
2929
'engine': 'tortoise.backends.mysql',
30-
'vars': {
30+
'vmap': {
3131
'path': 'database',
3232
'hostname': 'host',
3333
'port': 'port',
@@ -61,20 +61,20 @@ def expand_db_url(db_url: str, testing: bool = False) -> dict:
6161
path = path.replace('\\{', '{').replace('\\}', '}')
6262
path = path.format(uuid.uuid4().hex)
6363

64-
vars = {} # type: dict
65-
vars.update(db['vars'])
66-
params[vars['path']] = path
67-
if vars.get('hostname'):
68-
params[vars['hostname']] = str(url.hostname or '')
64+
vmap = {} # type: dict
65+
vmap.update(db['vmap'])
66+
params[vmap['path']] = path
67+
if vmap.get('hostname'):
68+
params[vmap['hostname']] = str(url.hostname or '')
6969
try:
70-
if vars.get('port'):
71-
params[vars['port']] = str(url.port or '')
70+
if vmap.get('port'):
71+
params[vmap['port']] = str(url.port or '')
7272
except ValueError:
7373
raise ConfigurationError('Port is not an integer')
74-
if vars.get('username'):
75-
params[vars['username']] = str(url.username or '')
76-
if vars.get('password'):
77-
params[vars['password']] = str(url.password or '')
74+
if vmap.get('username'):
75+
params[vmap['username']] = str(url.username or '')
76+
if vmap.get('password'):
77+
params[vmap['password']] = str(url.password or '')
7878

7979
return {
8080
'engine': db['engine'],

tortoise/backends/base/schema_generator.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ def _get_primary_key_create_string(self, field_name):
4747
# has to implement in children
4848
raise NotImplementedError() # pragma: nocoverage
4949

50-
def _get_auto_now_add_default(self):
51-
raise NotImplementedError() # pragma: nocoverage
52-
5350
def _get_table_sql(self, model) -> dict:
5451
fields_to_create = []
5552
m2m_tables_for_create = []

tortoise/backends/mysql/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class MySQLClient(BaseDBAsyncClient):
1818
executor_class = MySQLExecutor
1919
schema_generator = MySQLSchemaGenerator
2020

21-
def __init__(self, user, password, database, host, port, *args, **kwargs):
22-
super().__init__(*args, **kwargs)
21+
def __init__(self, user, password, database, host, port, **kwargs):
22+
super().__init__(**kwargs)
2323

2424
self.user = user
2525
self.password = password

tortoise/backends/sqlite/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class SqliteClient(BaseDBAsyncClient):
1616
executor_class = SqliteExecutor
1717
schema_generator = SqliteSchemaGenerator
1818

19-
def __init__(self, file_path, *args, **kwargs):
20-
super().__init__(*args, **kwargs)
19+
def __init__(self, file_path, **kwargs):
20+
super().__init__(**kwargs)
2121
self.filename = file_path
2222
self._transaction_class = type(
2323
'TransactionWrapper', (TransactionWrapper, self.__class__), {}

0 commit comments

Comments
 (0)