data_structures
The Data Structures module is a developer utility module. It provides two sets of tools to developers: drush generators for value objects, and classes for typed sequences on the most common base types.
Toolset
- Drush generator commands provide an easy way to create classes to replace associative and indexed arrays with type safe objects.
- Data structure classes that instantiate typed sequences using the most common base types.
- Classes and interfaces for set operations
Background
After bumping into an archived presentation by Crell we conducted a performance evaluation using 1,000,000 instances of data structures with two items, one integer and one string. Averaging 300 iterations of instantiating and executing with the given data structure clearly shows that properly structured objects are faster and more memory efficient than associative arrays.
Sorting Runtime Delta Memory Delta Assoc. Array 4.92563 499.547 MB Object (public) 4.59630 -6.7% 211.928 MB -57.6% Object (readonly) 4.15491 -15.6% 211.928 MB -57.6% Object (private) 6.33438 +28.6% 211.930 MB -57.6% Serialization Runtime Delta Memory Delta Assoc. Array 1.07523 1032.741 MB Object (public) 1.09756 +2.1% 849.519 MB -17.7% Object (readonly) 1.35624 +27.0% 934.409 MB -9.5% Object (private) 1.09738 +2.1% 856.520 MB -17.7%Efficiency should increase with larger data sets. Typed sequences need to use an indexed array as an internal data structure, so there are not equivalent efficiencies, however they do enforce a reliable type. If Adopt php-ds for data structures were ever adopted, we could use a more efficient data structure and this penalty would likely evaporate.
Usage
Install as you would normally install a contributed Drupal module: composer require drupal/data_structures. See Add a module with Composer for further information.
Drush Commands
drush gen data-structures:map: Generates a typed key-value class with public or readonly propertiesdata-structures:typed-sequence: Generates a typed sequence class with utility methods.
Sequences
Classes that can be used or extended in custom code to enforce and manage typed sequences.
CallableImmutableSequenceFloatImmutableSequenceIntImmutableSequenceObjectImmutableSequenceStringImmutableSequence
Sets
A Set class and interface, which can be extended to provide typed data, that ensures data elements added to an instance are unique and has methods for performing set operations between instances.
Additional Requirements
Requires Drush version 13 or higher.