Skip to content

python: Rework class system to use slots#76

Merged
JPEWdev merged 3 commits intomainfrom
python-slots
Feb 25, 2026
Merged

python: Rework class system to use slots#76
JPEWdev merged 3 commits intomainfrom
python-slots

Conversation

@JPEWdev
Copy link
Owner

@JPEWdev JPEWdev commented Feb 24, 2026

Reworks the way that SHACLObjects are constructed to use native python object properties to store the values instead of an array of IRIs. This allows the properties to be listed in __slots__, which improves the storage space required when there are a lot of these objects present.

In order for this to work, the way that classes are defined is completely reworked. Now, instead of using the @register function, a custom metaclass is implemented for SHACLObject and the parameters for the class are dealt with in the __new__() constructor (instead of arguments to @register). Set the documented properties of SHACLObject for the parameters that a class can set.

In real world testing with the SPDX 3 model, using slots for the objects resulted in a 24% reduction in both memory usage and load time for large documents.

Usage of slots means that multiple inheritance is not supported; by default shacl2code will check if the model uses multiple inheritance and if not, slots will be enabled. This can be overridden with the --use-slots=(yes,no,auto) command line option.

@JPEWdev JPEWdev requested a review from bact February 24, 2026 22:49
@JPEWdev JPEWdev added the enhancement New feature or request label Feb 24, 2026
@github-actions
Copy link

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/shacl2code/lang
  common.py
  cpp.py
  golang.py
  jsonschema.py
  python.py 63-66
Project Total  

This report was generated by python-coverage-comment-action

Reworks the way that SHACLObjects are constructed to use native python
object properties to store the values instead of an array of IRIs. This
allows the properties to be listed in `__slots__`, which improves the
storage space required when there are a lot of these objects present.

In order for this to work, the way that classes are defined is
completely reworked. Now, instead of using the `@register` function, a
custom metaclass is implemented for SHACLObject and the parameters for
the class are dealt with in the `__new__()` constructor (instead of
arguments to `@register`). Set the documented properties of SHACLObject
for the parameters that a class can set.

In real world testing with the SPDX 3 model, using slots for the objects
resulted in a 24% reduction in both memory usage and load time for large
documents.

Usage of slots means that multiple inheritance is not supported; by
default shacl2code will check if the model uses multiple inheritance and
if not, slots will be enabled. This can be overridden with the
--use-slots=(yes,no,auto) command line option.
bact
bact previously approved these changes Feb 25, 2026
Copy link
Collaborator

@bact bact left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Two optional change suggestions to save few lookups.

Co-authored-by: Arthit Suriyawongkul <arthit@gmail.com>
Co-authored-by: Arthit Suriyawongkul <arthit@gmail.com>
@JPEWdev JPEWdev merged commit 91ce861 into main Feb 25, 2026
19 checks passed
@JPEWdev JPEWdev deleted the python-slots branch February 25, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants