Building a CellsU / CellsSRC dictionary in .NET (VB.NET or C#)

Started by Visisthebest, July 06, 2024, 08:34:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

As people from Microsoft have said and I can see in benchmarking, CellsSRC is a lot faster than CellsU. I do not know why this is, as if Microsoft uses a Dictionary/MAP in Visio I would have guessed this to be just as fast.

But it turns out to be much slower:
https://surrogate-tm.github.io/mailant/2004/09/22/dev-luv-visio-development-top-five-performance-tips.htm

Now for all the Cells with an unchanging CellsU name, I would like to build a Dictionary with a string key and a Value stored in such class instances:

Public Class SRCValues
    Private ReadOnly _Section As Short
    Private ReadOnly _Row As Short
    Private ReadOnly _Column As Short

    Public ReadOnly Property Section As Short
        Get
            Return _Section
        End Get
    End Property

    Public ReadOnly Property Row As Short
        Get
            Return _Row
        End Get
    End Property

    Public ReadOnly Property Column As Short
        Get
            Return _Column
        End Get
    End Property

    Public Sub New(section As Short, row As Short, column As Short)
        _Section = section
        _Row = row
        _Column = column
    End Sub
End Class

The SRC values are immutable, but the dictionary itself is mutable (I think an immutable dictionary is slower, no?)

Dictionaries provide superfast lookup so this provides me a way to change "PinX" or anything other standard cell without having to look up these damn values each time :).

The problem is Microsoft has not organized the values for SRC I need in a way I can just read them in to the dictionary:
https://learn.microsoft.com/en-us/office/vba/api/visio.vissectionindices
https://learn.microsoft.com/en-us/office/vba/api/visio.visrowindices
https://learn.microsoft.com/en-us/office/vba/api/visio.viscellindices

Is there another resource or other way (in .NET) to get these values in a format that allows me to write them in to my dictionary without having to go through them one by one and match cells with their rows and sections with the info from Microsoft above?

Thank you for your help with this, I will share my code for this and the final data to be read-directly-in-to-the-dictionary here on the forum for everyone to use.

For Visio add-ins it is recommended to use CellsSRC when you do a lot of reading and writing to and from Visio via the slow .NET interop so I hope this will help more people.

Dictionaries are superfast in .NET so this solution will mary convenience with speed!
Visio 2021 Professional

Visisthebest

I see I can use a builder for an immutable dictionary, then it is just as fast practically as the mutable one.

Now how to get all those standard SRC values in to the dictionary in the simplest manner!

Visio 2021 Professional

Visisthebest

Ok, using some VBA to get all this information out of Visio programmatically.

Please see the text file attached with all the information that was exported.

I of course have to filter this list for Cells that will always be static (on any shape) for the dictionary, like PinX etc.

Are those likely to be all the cells in a Rectangle shape I just dropped on to the page?

If you want the code to be able to do this on any shape, please let me know and I will share it here!
Visio 2021 Professional

Yacine

Sorry Visisthebest,
I did not read your 3 posts in detail, but there's a thought I'd nevertheless would like to share.

A mixed approach might be useful.
There are immutable sections and variable ones.

The immutable can directly be processed with CellSRC.
Then there are some that switch in different states depending on properties. I am specially thinking of the general section of 1- and 2-D shapes. Not much dynamic, but still 2 states that are fast to fast to check.

Then you have sections that either exist or not.
These sections are usually different in size (Geometriy, Connection Points Controls, ...) and are  less prone to be handled by CellSRC.
Unless - and that can be worked out - you go carefully and inspect more than the standard return value of the row, but consider also the columns for name, etc.

I don't know your exact scenario, but I have one or two workarounds for handling several thousand cell values nearly instantaneously (seconds instead of minutes) between different applications (visio/access). Tell me, if you want to hear more about this.
Yacine

Visisthebest

Hi Yacine,

Thank you, my first step is to map out the Sections and Cells that are static, that is the main use case for the immutable dictionary.

Geometry, User, Shape Data I know are flexible.

Sections that are static but may be missing is not a problem, as we always check before using an optional section.
Visio 2021 Professional

Visisthebest

Is this the only section with always the same Cells?

visSectionObject (1)

I think I remember the Scratch section visSectionScratch (6) is also the same amount of cells?

Any other sections that are static in terms of exactly the same cells, if the section is present in a shape?
Visio 2021 Professional

Visisthebest

visSectionObject (1) I see is huge, is has 371 cells!

Quite a few of which I don't think I have ever seen on the visible shapesheet in Visio...
Visio 2021 Professional

Surrogate


Visisthebest

Visio 2021 Professional

Surrogate

Quote from: Visisthebest on July 07, 2024, 07:20:01 PMSurrogate thank you I will check it out!
Graham Wideman early collect this info
 http://diagramantics.com/v11dsp/support/download/V11DSP_RefCh0300.zip
Quote from: John Marshall in article Graham Wideman's Book AddendumThis is an Excel spreadsheet of part of the main table from Graham's 2003 book, that includes RowType. New items are highlighted in blue.

https://johnvisiomvp.ca/wp-content/uploads/2021/03/src-cell-description-mar-19-2021.zip

Thomas Winkel

Ok, CellsSCR is 20% faster:
You cannot view this attachment.
Read / write performance for 100 x 4211 Shapes.

I use CellsSRC only if necessary for iterating over variable sections (User, Prop, Connections, ...).

Btw. you can copy & paste the tables directly from the webpage to Excel:
You cannot view this attachment.

Visisthebest

Thank you Surrogate!

Thomas what I find is CellsSRC makes an even bigger difference in a Visio VSTO .NET addin (either C# or VB.NET), I think there it is about 50% faster because the .NET COM interop is so slow.
Visio 2021 Professional

Thomas Winkel

Hmm... the same test in .NET gives:
CellsU: 19s / CellsSRC: 15s
Which is 21% faster.

But the interesting point is that .NET ist faster than VBA.
I would have expected vice versa.

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    Visio.Page page = Globals.ThisAddIn.Application.ActivePage;
    if (page is null) return;

    var timer = new Stopwatch();
    timer.Start();

    for (int i = 1; i < 100; i++)
    {
        foreach (Visio.Shape shape in page.Shapes)
        {
            shape.CellsU["User.Row_1"].ResultIU = shape.CellsU["User.Row_1.Prompt"].ResultIU;
        }
    }

    timer.Stop();
    MessageBox.Show( timer.Elapsed.ToString(@"m\:ss\.fff"));
}

private void button2_Click(object sender, RibbonControlEventArgs e)
{
    Visio.Page page = Globals.ThisAddIn.Application.ActivePage;
    if (page is null) return;

    var timer = new Stopwatch();
    timer.Start();

    for (int i = 1; i < 100; i++)
    {
        foreach (Visio.Shape shape in page.Shapes)
        {
            shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionUser, 0, (short)Visio.VisCellIndices.visUserValue].ResultIU = shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionUser, 0, (short)Visio.VisCellIndices.visUserPrompt].ResultIU;
        }
    }

    timer.Stop();
    MessageBox.Show(timer.Elapsed.ToString(@"m\:ss\.fff"));
}

Visisthebest

Thomas thank you for this test, surprising .NET is faster than VBA indeed.

The user section is now very simple in this test (I assume), when I have time I'll see if we can make the test a little more complicated.

If the user section has only one field it may be easy to optimize, and there may be some optimization going on in some use cases.

20% is significant it has been a few years ago but at that time some tests showed more like a 50% difference. I don't remember if that was just the reads or also the writes, I think in particular the reads are faster. Mutation in Visio dilutes the effect of writing via SRC maybe.
Visio 2021 Professional

Yacine

Yacine

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 434 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1283KB.
Tokens: post-login.
Cache hits: 14: 0.00214s for 26,590 bytes (show)
Cache misses: 4: (show)
Queries used: 18.

[Show Queries]