Re: REQ: High Performance Access to a Static Object's List<string>

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Mark S. wrote:
How would that make it any less efficient?

If it's a reference then why create a new list? I'd simply loop over the static object's list directly.

Well, one reason to copy to a local variable would be so that the static object's reference could be updated by one thread without affecting the instance being used by another.

This would assume that the list instances themselves are immutable, as I suggested in my other reply.

If changes to the list are always in place then, yes...you'll need to synchronize access to the list so that, at a minimum, writing to the list only occurs when no other thread is also trying to read from it.

Easier would be to synchronize all access to the list, but then even readers of the list would wind up serialized, which could hinder performance. It's a classic trade-off...easy and optimal are not always the same. :)

Maximum concurrency is what I'm striving for. If only N number of requests can read the static object's list then I was wondering if making a copy of the list would more quickly release the read to be used by another request.

To some extent, that depends on what you're doing during the read. If each element of the list requires extensive time to process during the iteration then it's possible copying the list could speed things up. However, you may still have synchronization issues between individual list elements if you do a shallow copy, or you may not find there's any significant performance benefit if you do a deep copy.

Otherwise that read hold stays in effect during the loop. Which then begs question, does the creating the copy consume more the CPU thereby negating any gains.

Only by measuring it will you know for sure. It all depends on how expensive a deep copy is, whether you even need a deep copy, and how much work the actual use of the list involves.

Personally, I'd go for the immutable list design, but then that's probably already apparent since I mentioned it twice already. :) I think that, at the very least, copying the list each time it changes (which would be required if the list instance itself is immutable) is likely to be much more efficient than copying it each time you actually use it.

Pete
.



Relevant Pages

  • Re: Thread safety and atomic assignment (again)
    ... If I understand this correctly, then synchronizing set and get would guarantee the correct counter behavior by making set and get *mutually* exclusive, which is not guaranteed by volatile. ... What you need to do to ensure your counter works as expected is to synchronize the "increment" operation. ... It is an excellent reference on what, why, and how in handle multi-threaded programming. ...
    (comp.lang.java.programmer)
  • Re: RE DateFunction
    ... That $A1 reference was given to allow the copying down of the formula so ... Excel and Word Function Lists available free to good homes. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: [PATCH 2.6.10-rc1 2/5] driver-model: bus_recan_devices() locking fix
    ... Unless the caller already has a reference to the ... get_buscannot synchronize with the ... > (individual bus drivers are expected to perform the synchronization). ... > NULL or not and incrementing the reference count. ...
    (Linux-Kernel)
  • Re: Integer 128 != Integer 128 ??
    ... indeed become less likely to properly synchronize over time. ... the reference that's being used for synchronization). ... integer value in an actual "int" instead of boxing it. ... made the error message go away. ...
    (comp.lang.java.programmer)
  • Re: Class Destroys itself straight away!
    ... passing it by reference will ... generate more instructions than simply copying it. ... for bigger types like `std::wstring' and other classes. ... If the parameter is of class type, then passing in a variable by value ...
    (microsoft.public.vc.language)