dagop: minor python optimization to `headrevs`
Less lookup and less function call never hurt. This provides a small speedup
on various run of the 'heads()' revset. This also buys back some of the slow
down we observed in the previous changesets for single value lookup.
Performance number:
0) before dagop.headrevs usage
1) after dagop.headrevs usage
2) after this change
revset: heads(all())
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 0.036503 0.032564 0.030024 0.032378 0.030887 0.036367 0.031713 0.032205 0.036467 0.032286 0.030300
1) 0.036668 0.035347 108% 0.035611 118% 0.035358 109% 0.035726 115% 0.036411 0.035261 111% 0.036096 112% 0.036052 0.035095 108% 0.035792 118%
2) 0.034254 93% 0.034482 0.035003 0.034353 0.033754 94% 0.034689 0.034361 0.035059 0.034636 0.034662 0.035465
revset: heads(-10000:-1)
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 0.003936 0.003218 0.003227 0.003302 0.003328 0.003848 0.003305 0.003252 0.003839 0.003306 0.003279
1) 0.003870 0.003785 117% 0.003821 118% 0.003780 114% 0.003769 113% 0.003776 0.003792 114% 0.003805 117% 0.003810 0.003798 114% 0.003840 117%
2) 0.003666 94% 0.003577 94% 0.003632 0.003644 0.003614 0.003638 0.003652 0.003632 0.003661 0.003660 0.003658
revset: (-5000:-1000) and heads(-10000:-1)
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 0.004244 0.003368 0.003313 0.003367 0.003327 0.004325 0.003401 0.003379 0.004310 0.003359 0.003396
1) 0.003969 93% 0.003862 114% 0.003834 115% 0.003810 113% 0.003822 114% 0.003940 91% 0.003908 114% 0.003814 112% 0.003986 92% 0.003954 117% 0.003816 112%
2) 0.003728 93% 0.003638 94% 0.003659 0.003685 0.003628 94% 0.003716 94% 0.003653 93% 0.003655 0.003748 94% 0.003740 94% 0.003686
revset: heads(matching(tip, "author"))
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 7.574666 7.545950 7.570743 7.578697 7.525725 7.509929 7.443854 7.488442 7.452880 7.445411 7.689107
1) 7.549390 7.389162 7.529790 7.536297 7.450467 7.555347 7.404586 7.514948 7.542794 7.524787 7.536918
2) 7.568294 7.479326 7.578624 7.380375 7.440102 7.454218 7.515189 7.556511 7.524585 7.537566 7.507418
revset: heads(matching(tip, "author")) and -10000:-1
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 7.512533 7.605877 7.382894 7.462109 7.420086 7.575034 7.448452 7.549374 7.457880 7.450308 7.515019
1) 7.548677 7.551832 7.629598 7.494857 7.550554 7.521838 7.451794 error 7.321781 7.546885 7.557523
2) 7.451985 7.541044 7.506563 7.470928 7.512618 7.474988 7.498887 7.547930 7.560276 7.618599 7.465442
revset: (-10000:-1) and heads(matching(tip, "author"))
plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast
0) 7.465419 7.570089 7.439594 7.521221 7.498716 7.492922 7.479108 7.552397 7.407888 error 7.468264
1) 7.539866 7.548045 7.491761 7.517170 7.469824 7.501990 7.579102 7.502568 7.578102 7.555754 7.567622
2) 7.370463 7.514712 7.497024 7.679428 7.638138 7.490775 7.472273 7.652587 7.584139 7.511893 7.466384
# badserverext.py - Extension making servers behave badly
#
# Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
# no-check-code
"""Extension to make servers behave badly.
This extension is useful for testing Mercurial behavior when various network
events occur.
Various config options in the [badserver] section influence behavior:
closebeforeaccept
If true, close() the server socket when a new connection arrives before
accept() is called. The server will then exit.
closeafteraccept
If true, the server will close() the client socket immediately after
accept().
closeafterrecvbytes
If defined, close the client socket after receiving this many bytes.
closeaftersendbytes
If defined, close the client socket after sending this many bytes.
"""
from __future__ import absolute_import
import socket
from mercurial import(
registrar,
)
from mercurial.hgweb import (
server,
)
configtable = {}
configitem = registrar.configitem(configtable)
configitem(b'badserver', b'closeafteraccept',
default=False,
)
configitem(b'badserver', b'closeafterrecvbytes',
default='0',
)
configitem(b'badserver', b'closeaftersendbytes',
default='0',
)
configitem(b'badserver', b'closebeforeaccept',
default=False,
)
# We can't adjust __class__ on a socket instance. So we define a proxy type.
class socketproxy(object):
__slots__ = (
'_orig',
'_logfp',
'_closeafterrecvbytes',
'_closeaftersendbytes',
)
def __init__(self, obj, logfp, closeafterrecvbytes=0,
closeaftersendbytes=0):
object.__setattr__(self, '_orig', obj)
object.__setattr__(self, '_logfp', logfp)
object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)
def __getattribute__(self, name):
if name in ('makefile',):
return object.__getattribute__(self, name)
return getattr(object.__getattribute__(self, '_orig'), name)
def __delattr__(self, name):
delattr(object.__getattribute__(self, '_orig'), name)
def __setattr__(self, name, value):
setattr(object.__getattribute__(self, '_orig'), name, value)
def makefile(self, mode, bufsize):
f = object.__getattribute__(self, '_orig').makefile(mode, bufsize)
logfp = object.__getattribute__(self, '_logfp')
closeafterrecvbytes = object.__getattribute__(self,
'_closeafterrecvbytes')
closeaftersendbytes = object.__getattribute__(self,
'_closeaftersendbytes')
return fileobjectproxy(f, logfp,
closeafterrecvbytes=closeafterrecvbytes,
closeaftersendbytes=closeaftersendbytes)
# We can't adjust __class__ on socket._fileobject, so define a proxy.
class fileobjectproxy(object):
__slots__ = (
'_orig',
'_logfp',
'_closeafterrecvbytes',
'_closeaftersendbytes',
)
def __init__(self, obj, logfp, closeafterrecvbytes=0,
closeaftersendbytes=0):
object.__setattr__(self, '_orig', obj)
object.__setattr__(self, '_logfp', logfp)
object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)
def __getattribute__(self, name):
if name in ('read', 'readline', 'write', '_writelog'):
return object.__getattribute__(self, name)
return getattr(object.__getattribute__(self, '_orig'), name)
def __delattr__(self, name):
delattr(object.__getattribute__(self, '_orig'), name)
def __setattr__(self, name, value):
setattr(object.__getattribute__(self, '_orig'), name, value)
def _writelog(self, msg):
msg = msg.replace('\r', '\\r').replace('\n', '\\n')
object.__getattribute__(self, '_logfp').write(msg)
object.__getattribute__(self, '_logfp').write('\n')
object.__getattribute__(self, '_logfp').flush()
def read(self, size=-1):
remaining = object.__getattribute__(self, '_closeafterrecvbytes')
# No read limit. Call original function.
if not remaining:
result = object.__getattribute__(self, '_orig').read(size)
self._writelog('read(%d) -> (%d) (%s) %s' % (size,
len(result),
result))
return result
origsize = size
if size < 0:
size = remaining
else:
size = min(remaining, size)
result = object.__getattribute__(self, '_orig').read(size)
remaining -= len(result)
self._writelog('read(%d from %d) -> (%d) %s' % (
size, origsize, len(result), result))
object.__setattr__(self, '_closeafterrecvbytes', remaining)
if remaining <= 0:
self._writelog('read limit reached, closing socket')
self._sock.close()
# This is the easiest way to abort the current request.
raise Exception('connection closed after receiving N bytes')
return result
def readline(self, size=-1):
remaining = object.__getattribute__(self, '_closeafterrecvbytes')
# No read limit. Call original function.
if not remaining:
result = object.__getattribute__(self, '_orig').readline(size)
self._writelog('readline(%d) -> (%d) %s' % (
size, len(result), result))
return result
origsize = size
if size < 0:
size = remaining
else:
size = min(remaining, size)
result = object.__getattribute__(self, '_orig').readline(size)
remaining -= len(result)
self._writelog('readline(%d from %d) -> (%d) %s' % (
size, origsize, len(result), result))
object.__setattr__(self, '_closeafterrecvbytes', remaining)
if remaining <= 0:
self._writelog('read limit reached; closing socket')
self._sock.close()
# This is the easiest way to abort the current request.
raise Exception('connection closed after receiving N bytes')
return result
def write(self, data):
remaining = object.__getattribute__(self, '_closeaftersendbytes')
# No byte limit on this operation. Call original function.
if not remaining:
self._writelog('write(%d) -> %s' % (len(data), data))
result = object.__getattribute__(self, '_orig').write(data)
return result
if len(data) > remaining:
newdata = data[0:remaining]
else:
newdata = data
remaining -= len(newdata)
self._writelog('write(%d from %d) -> (%d) %s' % (
len(newdata), len(data), remaining, newdata))
result = object.__getattribute__(self, '_orig').write(newdata)
object.__setattr__(self, '_closeaftersendbytes', remaining)
if remaining <= 0:
self._writelog('write limit reached; closing socket')
self._sock.close()
raise Exception('connection closed after sending N bytes')
return result
def extsetup(ui):
# Change the base HTTP server class so various events can be performed.
# See SocketServer.BaseServer for how the specially named methods work.
class badserver(server.MercurialHTTPServer):
def __init__(self, ui, *args, **kwargs):
self._ui = ui
super(badserver, self).__init__(ui, *args, **kwargs)
recvbytes = self._ui.config(b'badserver', b'closeafterrecvbytes')
recvbytes = recvbytes.split(',')
self.closeafterrecvbytes = [int(v) for v in recvbytes if v]
sendbytes = self._ui.config(b'badserver', b'closeaftersendbytes')
sendbytes = sendbytes.split(',')
self.closeaftersendbytes = [int(v) for v in sendbytes if v]
# Need to inherit object so super() works.
class badrequesthandler(self.RequestHandlerClass, object):
def send_header(self, name, value):
# Make headers deterministic to facilitate testing.
if name.lower() == 'date':
value = 'Fri, 14 Apr 2017 00:00:00 GMT'
elif name.lower() == 'server':
value = 'badhttpserver'
return super(badrequesthandler, self).send_header(name,
value)
self.RequestHandlerClass = badrequesthandler
# Called to accept() a pending socket.
def get_request(self):
if self._ui.configbool(b'badserver', b'closebeforeaccept'):
self.socket.close()
# Tells the server to stop processing more requests.
self.__shutdown_request = True
# Simulate failure to stop processing this request.
raise socket.error('close before accept')
if self._ui.configbool('badserver', 'closeafteraccept'):
request, client_address = super(badserver, self).get_request()
request.close()
raise socket.error('close after accept')
return super(badserver, self).get_request()
# Does heavy lifting of processing a request. Invokes
# self.finish_request() which calls self.RequestHandlerClass() which
# is a hgweb.server._httprequesthandler.
def process_request(self, socket, address):
# Wrap socket in a proxy if we need to count bytes.
if self.closeafterrecvbytes:
closeafterrecvbytes = self.closeafterrecvbytes.pop(0)
else:
closeafterrecvbytes = 0
if self.closeaftersendbytes:
closeaftersendbytes = self.closeaftersendbytes.pop(0)
else:
closeaftersendbytes = 0
if closeafterrecvbytes or closeaftersendbytes:
socket = socketproxy(socket, self.errorlog,
closeafterrecvbytes=closeafterrecvbytes,
closeaftersendbytes=closeaftersendbytes)
return super(badserver, self).process_request(socket, address)
server.MercurialHTTPServer = badserver