c# - lock performance cloning object or not -


i have datatable can contains large number of datarows; datatable can accessed several threads. threads can change values rows. there search functions locks datatable, search using linq , return expected value, similar of:

lock(tablecontent) {     var t = (from row in tablecontent.asenumerable()             row[fieldname] != dbnull.value && row.field<t>(fieldname).equals(somevalue) select row);     if (t.any())     { ... } }                      

the question is: if clone (locking original) datatable , search cloned object, locked period faster searching directly original?

i think cloning operation take o(n) copy each row, time same search, don't know if there optimizations (memory copy, ...) reduces cloning time or similar.

clone o(n), doesn't tell full story. clone can shallow clone (just copy references in table) or deep clone (copy objects themselves). deep clone can expensive operation. searching time can vary, too, quick search checks single integer field, complex search compares multiple values , pretty expensive. in addition, if data sorted on field you're searching, search o(log n), considerably faster o(n).

if need take account possibility add, modify, or delete rows, either have lock or clone. if you're doing single search, cloning doesn't make sense because you'd have lock table in order clone it. , cloning take longer searching, unless searches unusually expensive.

you modifications rare , searches frequent. in case, suggest use reader-writer lock, support unlimited number of readers, or 1 writer. in .net, want readerwriterlockslim. using that, code this:

private readerwriterlockslim tablelock = new readerwriterlockslim();  public bool search(string s) {     tablelock.enterreadlock();     try     {         // search here         return result;     }         {         tablelock.exitreadlock();     } } 

any number of readers can searching table concurrently. provided, of course, don't modify it. if want modify table, have acquire write lock:

public void modify(string s) {     tablelock.enterwritelock();     try     {         // modification here         return;     }         {         tablelock.exitwritelock();     } } 

when thread tries enter write lock, has wait existing readers exit. readers come in after write lock requested have wait existing readers exit, , writer acquire , release lock.

the reader/writer lock works me in situations similar 1 described: frequent reads , infrequent writes. it's worth looking into, if nothing else because it's easy test.

the reader/writer lock still works if searches , updates approximately equal because still allows multiple readers when possible. come think of it, works if writes more frequent, again because allow multiple reads when possible. use readerwriterlockslim when have data structure can searched , updated.

there other solutions, involve custom data structures can more difficult implement , maintain. i'd suggest give reader/writer lock try. if after that, profiling shows threads still waiting on lock , slowing application's response time, can alternatives.

i'm little concerned, though, you're doing more searching. sample selects bunch of rows , if (t.any()) { ... }. doing in { ... }? if takes long time, might better off making code clone rows select. can release lock , party on result set heart's content without affecting other threads need access data structure.


Comments