Efficient Array<> of valuetype entry manipulation

  • Thread starter Thread starter Francisco
  • Start date Start date
F

Francisco

Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;

....
_tabularView.a=amount1;
_tabularView.b=amount2;
_tabularView.c=amount3;
_tabularView.d=amount4;
_tabularView.e=amount5;
....

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView"?

Thanks in advance
 
Rick said:
TreeNodeTableItem tvi = _tabularView;
tvi.a = amount1;
tvi.b = amount2;
.. . . etc.

This saves an indexing step for each structure member access. Whether
it's noticeably faster for you depends on your usage, of course. This
looks like a pattern that a decent optimizer could recognize and handle
for you, though.


Posting late always bites me. But I think the code you suggested is
_required_ (sort of), since the List<> type is a struct (value type).
That is, the indexer returns a copy of the value, not the indexed
element itself. So the only way to change the value within the list is
to initialize an existing value type variable with the desired values,
and then assign that to the indexed list item.

I wrote "sort of", because if one is overwriting all of the values in
the struct, obviously there's no point in retrieving the indexed list
item at the start (as in the assignment in the variable declaration in
the above code).

The code would make more sense as an _alternative_ rather than a
requirement if the list type was a class instead of a struct. Then the
value being returned by the indexer is the reference to the instance,
and the instance itself can be modified in-place without having to
reassign anything back to the list. And of course in that situation,
the assignment in the variable declaration is necessary.

Pete
 
Francisco said:
Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;

...
_tabularView.a=amount1;
_tabularView.b=amount2;
_tabularView.c=amount3;
_tabularView.d=amount4;
_tabularView.e=amount5;
...

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView"?



TreeNodeTableItem tvi = _tabularView;
tvi.a = amount1;
tvi.b = amount2;
.. . . etc.

This saves an indexing step for each structure member access. Whether it's
noticeably faster for you depends on your usage, of course. This looks like a
pattern that a decent optimizer could recognize and handle for you, though.

HTH,
-rick-
 
Francisco said:
Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;

...
_tabularView.a=amount1;
_tabularView.b=amount2;
_tabularView.c=amount3;
_tabularView.d=amount4;
_tabularView.e=amount5;
...

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView"?

Thanks in advance


You can store the values in a local structure, and copy the structure to
the list:

TreeNodeTableItem item;
item.a = amount1;
item.b = amount2;
item.c = amount3;
item.d = amount4;
item.e = amount5;
_tabularView = item;

The local structure is allocated on the stack, so populating it is very
efficient. The drawback is that the entire structure value is copied
another time to put it in the list.

Whether this is actually faster or not, is hard to tell, especially as
your structure is larger than the recommended 16 bytes.
 
Rick said:
Francisco said:
Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;

...
_tabularView.a=amount1;
_tabularView.b=amount2;
_tabularView.c=amount3;
_tabularView.d=amount4;
_tabularView.e=amount5;
...

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView"?



TreeNodeTableItem tvi = _tabularView;
tvi.a = amount1;
tvi.b = amount2;
. . . etc.

This saves an indexing step for each structure member access.


A small drawback is that it's not changing the value in the list at
all... ;)
 
Göran Andersson said:
Rick said:
Francisco said:
Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;

...
_tabularView.a=amount1;
_tabularView.b=amount2;
_tabularView.c=amount3;
_tabularView.d=amount4;
_tabularView.e=amount5;
...

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView"?



TreeNodeTableItem tvi = _tabularView;
tvi.a = amount1;
tvi.b = amount2;
. . . etc.

This saves an indexing step for each structure member access.


A small drawback is that it's not changing the value in the list at
all... ;)


Yikes! You are correct, of course. Now I don't know whether to laugh out loud
at myself or just go hide under the desk. But first I will make myself write
the code I should have run before posting . . .

Thank you Goran,
-rick-

using System;

namespace ConsoleApplication1
{
public class Ctest
{
public string c;
public Ctest(string init)
{
this.c = init;
}
}

public struct Stest
{
public string s;
public Stest(string init)
{
this.s = init;
}
}

class Class1
{
[STAThread]
static void Main()
{
Ctest c1 = new Ctest("c");
Ctest c2 = c1;

Stest s1 = new Stest("s");
Stest s2 = s1;

Console.WriteLine("Before: c1.c = " + c1.c + ", s1.s = " + s1.s);
c2.c = "c_new";
s2.s = "s_new";
Console.WriteLine("After: c1.c = " + c1.c + ", s1.s = " + s1.s);
Console.ReadLine();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top