Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with unique references singleton implementation #1791

Closed
abuts opened this issue Dec 16, 2024 · 2 comments
Closed

Problems with unique references singleton implementation #1791

abuts opened this issue Dec 16, 2024 · 2 comments
Labels
bug Something isn't working Design Task relating to project design enhancement New feature or request Optimisation Code optimisation required

Comments

@abuts
Copy link
Member

abuts commented Dec 16, 2024

ISSUE DESCRIPTION

This ticket addresses two issues: Design flaw and bug. Bug can be actually sorted differently, but still mentioned here.

Simple demonstration illustrates this:

  1. fix list option of the container to be able to explore its contents, namely, add return value: glc = glcontainer; to row 862 of the global_references_container code base to be able to explore its contents.
  2. clear memory (clear classes, clear all)
  3. open _test\test_miltifit\test_multifit_horace_1 and set up break point at row 32, immediatly after Horace loads reference cuts.
  4. Run code and stop at break point
  5. Do:
>>glc  = unique_references_container.global_container('list','GLOBAL_NAME_DETECTORS_CONTAINER')
    GLOBAL_NAME_INSTRUMENTS_CONTAINER: [1×1 unique_objects_container]
      GLOBAL_NAME_DETECTORS_CONTAINER: [1×1 unique_objects_container]
        GLOBAL_NAME_SAMPLES_CONTAINER: [1×1 unique_objects_container]


glc = 

  struct with fields:

    GLOBAL_NAME_INSTRUMENTS_CONTAINER: [1×1 unique_objects_container]
      GLOBAL_NAME_DETECTORS_CONTAINER: [1×1 unique_objects_container]
        GLOBAL_NAME_SAMPLES_CONTAINER: [1×1 unique_objects_container]

K>> glc.GLOBAL_NAME_DETECTORS_CONTAINER

ans = 

  unique_objects_container with properties:

         n_objects: 24
            n_runs: 24
          n_unique: 24
         baseclass: 'IX_detector_array'
               idx: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
    unique_objects: {1×24 cell}
      n_duplicates: [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

K>> glc.GLOBAL_NAME_DETECTORS_CONTAINER.unique_objects

ans =

  1×24 cell array

  Columns 1 through 4

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

  Columns 5 through 8

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

  Columns 9 through 12

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

  Columns 13 through 16

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

  Columns 17 through 20

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

  Columns 21 through 24

    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}    {1×1 IX_detector_array}

K>> glc.GLOBAL_NAME_DETECTORS_CONTAINER.unique_objects{1} == glc.GLOBAL_NAME_DETECTORS_CONTAINER.unique_objects{2}

ans =

  logical

   1

There are 3 reference cuts in this reference file, each contains 8 runs with all detector files for each run are the same.

BUG:

unique_reference_container does not count and eliminate unique runs desipite they are equal according to existing cmparison rules. It just adds them together and puts them all in memory.

Due to design flaws described below, this bug should not be fixed on the basis of this very customized singleton implementation. Standard singleton implementation should be implemented and properly deployed in Horace.

DESIGN FLAW DESCRIPTION:
Singleton is a standard pattern in software engeneering. Search "Singleton in Matlab" returns bunch of references, e.g.:

etc.
They are all rerfer to a bit different implementations which have the same main features:

  • static protected storage
  • private constructor
  • static instance method which provides access to internal storage and methods which interact with this storage.

Using standard code patterns higly improves readability, maintainability and stability of code.

Horace has couple of places where singleton is already used according to the pattern above (e.g. config_store and sqw_formats factory)

unique_references_container contains different implementation of singleton, designed as static variable stored within unique_references_container

This implementation has number of disadvantages:

  1. it is non-standard and Horace-specific. When maintainer looks at the code, he/she would not immediately understand what is there and need to read complex custom description on what is this and how to work with it, while any software graduate student is familiar with a standard singleton.
  2. The storage and unique_references_contanter are mixed together, despite having different purposes.
  3. becauls of (2), methods of "would be singleton" are implemented as a switch over group of keys. This is inferior design in comparison with normal set of class methods, which are visible and easy testable.

The work of converting this custom design into standard singleton implementation is not too difficult and should be performed before doing any modifications to unique references container. I would say that this work is prerequest for doing Re #1781, part 3.

@abuts abuts added enhancement New feature or request Optimisation Code optimisation required Design Task relating to project design labels Dec 16, 2024
@abuts abuts added the bug Something isn't working label Dec 26, 2024
abuts added a commit that referenced this issue Dec 29, 2024
abuts added a commit that referenced this issue Dec 30, 2024
abuts added a commit that referenced this issue Jan 1, 2025
…tainer.m May be returned later as convenience methods with unit tests added.

fixed bunch of tests and removed some warnings.
abuts added a commit that referenced this issue Jan 1, 2025
abuts added a commit that referenced this issue Jan 2, 2025
abuts added a commit that referenced this issue Jan 2, 2025
abuts added a commit that referenced this issue Jan 2, 2025
…ent from container type, throw errors as warning may cause strange unexpected results.

renamed test test_unique_objects_container to test_objects_containers
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
…iner and removed redundant and duplicated methods
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 5, 2025
abuts added a commit that referenced this issue Jan 7, 2025
abuts added a commit that referenced this issue Jan 9, 2025
abuts added a commit that referenced this issue Jan 9, 2025
cmarooney-stfc added a commit that referenced this issue Jan 10, 2025
* fix fetch of glc

* Re #1791 Alternative solution, unit tests

* Re #1791 one more test to prove unique storage operations

* extend commenting of tests

---------

Co-authored-by: abuts <[email protected]>
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
# Conflicts:
#	_test/test_multifit/test_multifit_horace_1.m
#	_test/test_unique_objects_container/test_unique_references.m
#	herbert_core/utilities/classes/@unique_objects_container/unique_objects_container.m
#	herbert_core/utilities/classes/@unique_references_container/unique_references_container.m
#	horace_core/sqw/@Experiment/private/build_from_binfile_headers_.m
#	horace_core/sqw/@sqw/sqw.m
#	horace_core/sqw/file_io/@faccess_sqw_v4/get_sqw.m
#	horace_core/sqw/file_io/@sqw_binfile_common/get_sqw.m
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 20, 2025
abuts added a commit that referenced this issue Jan 23, 2025
@abuts
Copy link
Member Author

abuts commented Jan 24, 2025

Addressed by Re #1800

abuts added a commit that referenced this issue Jan 24, 2025
@abuts
Copy link
Member Author

abuts commented Feb 5, 2025

Fixed by Re #1800

@abuts abuts closed this as completed Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Design Task relating to project design enhancement New feature or request Optimisation Code optimisation required
Projects
None yet
Development

No branches or pull requests

1 participant