Skip to content

Commit

Permalink
MSS: use __slots__ for better performances
Browse files Browse the repository at this point in the history
Using sys.getsizeof(sct) shows interesting improvements:
- GNU/Linux: 104 -> 88
- macOS: 80 -> 64
- Windows: 112 -> 64
  • Loading branch information
BoboTiG committed Feb 24, 2019
1 parent bfa20aa commit 417075b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ History:
5.0.0 2019/xx/xx
- removed support for Python 2.7
- MSS: improve type annotations and add CI check
- MSS: use __slots__ for better performances
- Windows: use our own instances of GDI32 and User32 DLLs
- doc: add an example using the multiprocessing module (closes #82)

Expand Down
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
5.0.0 (2019-xx-xx)
==================

darwin.py
---------
- Added `MSS.__slots__`

linux.py
--------
- Added `MSS.__slots__`
- Deleted ``LAST_ERROR`` constant. Use ``ERROR`` namespace instead, specially the ``ERROR.details`` attribute.

models.py
Expand All @@ -16,9 +21,14 @@ models.py

screenshot.py
-------------
- Added `ScreenShot.__slots__`
- Removed ``Pos``. Use ``models.Pos`` instead.
- Removed ``Size``. Use ``models.Size`` instead.

windows.py
----------
- Added `MSS.__slots__`


4.0.1 (2019-01-26)
==================
Expand Down
8 changes: 6 additions & 2 deletions mss/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
class MSSMixin:
""" This class will be overloaded by a system specific one. """

cls_image = ScreenShot # type: Type[ScreenShot]
compression_level = 6
__slots__ = {"_monitors", "cls_image", "compression_level"}

def __init__(self):
self.cls_image = ScreenShot # type: Type[ScreenShot]
self.compression_level = 6
self._monitors = [] # type: Monitors

def __enter__(self):
# type: () -> MSSMixin
Expand Down
5 changes: 4 additions & 1 deletion mss/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ class MSS(MSSMixin):
It uses intensively the CoreGraphics library.
"""

__slots__ = {"core", "max_displays"}

def __init__(self, **_):
""" macOS initialisations. """

self._monitors = [] # type: Monitors
super().__init__()

self.max_displays = 32

coregraphics = ctypes.util.find_library("CoreGraphics")
Expand Down
4 changes: 3 additions & 1 deletion mss/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ class MSS(MSSMixin):
It uses intensively the Xlib and its Xrandr extension.
"""

__slots__ = {"display", "drawable", "root", "xlib", "xrandr"}

def __init__(self, display=None):
# type: (Optional[Union[bytes, str]]) -> None
""" GNU/Linux initialisations. """

self._monitors = [] # type: Monitors
super().__init__()

if not display:
try:
Expand Down
2 changes: 2 additions & 0 deletions mss/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ScreenShot:
with PIL.Image, it has been decided to use *ScreenShot*.
"""

__slots__ = {"__pixels", "__rgb", "pos", "raw", "size"}

def __init__(self, data, monitor, size=None):
# type: (bytearray, Monitor, Optional[Size]) -> None

Expand Down
15 changes: 14 additions & 1 deletion mss/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,24 @@ class BITMAPINFO(ctypes.Structure):
class MSS(MSSMixin):
""" Multiple ScreenShots implementation for Microsoft Windows. """

__slots__ = {
"_bbox",
"_bmi",
"_bmp",
"_data",
"_memdc",
"_srcdc",
"gdi32",
"monitorenumproc",
"user32",
}

def __init__(self, **_):
# type: (Any) -> None
""" Windows initialisations. """

self._monitors = [] # type: Monitors
super().__init__()

self._bbox = {"height": 0, "width": 0}
self._bmp = None
self._data = ctypes.create_string_buffer(0) # type: ctypes.Array[ctypes.c_char]
Expand Down
3 changes: 1 addition & 2 deletions tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Source: https://github.com/BoboTiG/python-mss
"""

import ctypes.util
import platform

import mss
Expand All @@ -18,6 +17,6 @@
def test_implementation(monkeypatch):
# Test bad data retrieval
with mss.mss() as sct:
monkeypatch.setattr(ctypes.windll.gdi32, "GetDIBits", lambda *args: 0)
monkeypatch.setattr(sct.gdi32, "GetDIBits", lambda *args: 0)
with pytest.raises(ScreenShotError):
sct.shot()

0 comments on commit 417075b

Please sign in to comment.