c# - Adding fixed size array to IEnumerable -


this method gets:

ienumerable<object[]> - in every array in fixed size (it represent relational data structure).

dataenumerable.column[] - metadata columns,mostly have same value rows.

expected outcome:

each "row" should value each of these columns (so data structure remains relational).

    private ienumerable<object[]> bindextracolumns(ienumerable<object[]> basedata, int datasize, dataenumerable.column[] columnstoadd)     {         int extracolumnslength = columnstoadd.length;         object[] row = new object[datasize + extracolumnslength];          string columnname;         int rownumbercolumnindex = -1;          (int = 0; < extracolumnslength; i++)         {             //assign values doesn't change between lines..             // assign rownumbercolumnindex if row number column exists         }          //assign values change here, since support row number         // i'ts not generic enough                 if (rownumbercolumnindex != -1)         {             int rownumber = 1;              foreach (var baserow in basedata)             {                 row[rownumbercolumnindex] = rownumber;                  array.copy(baserow, 0, row, extracolumnslength, datasize);                  yield return row;                  rownumber++;             }         }         else         {             foreach (var baserow in basedata)             {                 array.copy(baserow, 0, row, extracolumnslength, datasize);                  yield return row;             }         }     } 

this method can called hundreds of threads relatively big data sets performance here critical, , tried create minimum new objects possible.

please note - private method, used only by datareader, read each line, , passes array prior reading next line.

so - copying arrays here optimized here somehow, , should use (carefully) memory boost things here?

thanks

your code fundamentally broken. you're returning reference same array every time, means unless caller uses data within each item immediately, gets lost. example, suppose use:

list<object[]> rows = bindextracolumns(data, size, toadd).tolist(); 

then when iterate on rows, find same data in every row. that's not experience.

i think make much more sense create new array each iteration. yes, that's lot of memory being used - doesn't surprise callers much.

if don't want that, suggest change approach caller has pass in action<object[]> executed on each row, documented proviso if caller stashes reference array, may surprised results.

you're concerned performance, if data coming database i'd expect array creation/copying performance insignificant. should write simplest (and reliable) code works first, , benchmark see whether performs enough. unless you've got evidence need make surprising design choice, feels you're optimizing way early.

edit: know it's private method only used in 1 specific place, still avoid reuse. it's fragile. change passing in action<object[]> or copying data new array every time. wouldn't keep current approach without strong evidence it's bottleneck: said before, i'd expect database communication more important. leaving timebombs in code works out well.

if really, really want keep doing this, should document very strongly, giving severe warnings result non-idiomatic.

in terms of whether there's more optimization - well... 1 alternative avoid having work single array in first place. create class held references both arrays (the current base row , fixed data) , exposed indexer returned value 1 array or other based on index being requested. don't know you're doing data ("passes array" doesn't mean anything) don't know whether that's feasible, efficient and implemented without odd behaviour.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -