Changeset 21010
- Timestamp:
- 01/17/08 07:30:22 (8 months ago)
- Files:
-
- juggler/trunk/modules/vapor/test/TestSuite/TestCases/IO/Socket/AveragingAllocationStrategyTest.cpp (modified) (2 diffs)
- juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.cpp (modified) (4 diffs)
- juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
juggler/trunk/modules/vapor/test/TestSuite/TestCases/IO/Socket/AveragingAllocationStrategyTest.cpp
r21007 r21010 37 37 for ( unsigned int i = 0; i < inputs.size(); ++i ) 38 38 { 39 // Keep track of the current average of allocation sizes.40 const size_t average(cur_total / (i + 1));41 42 39 // Get the allocation size for the case of a buffer that is currently 43 40 // empty. 44 41 const size_t result = s(0, inputs[i]); 45 42 cur_total += result; 43 44 // Keep track of the current average of allocation sizes. 45 const size_t average(cur_total / (i + 1)); 46 46 47 47 // The allocation size for this strategy is the maximum of the requested … … 69 69 expected[9] = 25600; 70 70 71 size_t writes( 1);71 size_t writes(0); 72 72 73 73 for ( unsigned int i = 0; i < 10; ++i, ++writes ) juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.cpp
r21007 r21010 50 50 } 51 51 52 template<typename T>53 inline T minValue(const T v1, const T v2)54 {55 return v1 < v2 ? v1 : v2;56 }57 58 52 } 59 53 … … 65 59 : mWindowSize(windowSize) 66 60 , mIndex(0) 67 // mAllocSizes is set to a fixed size so that it is not necessary to resize68 // it as allocations occur, nor is it necessary to stop resizing it when69 // the number of allocations eventually equals windowSize.70 , mAllocSizes(windowSize, 0)71 // mAllocOffset is initialized to 1 because we add it to72 // mAllocSizes.begin() in order to compute the average of the allocations73 // made. See computeAverage().74 , mAllocOffset(1)75 61 { 76 /* Do nothing. */ ; 62 // Reserve the memory required for storing the sliding window of allocation 63 // sizes so that the buffer does not need to grow as we add values to it. 64 mAllocSizes.reserve(windowSize); 77 65 } 78 66 … … 86 74 87 75 // Record the current amount allocated for future computations of the 88 // average allocation amount. 89 mAllocSizes[mIndex] = alloc_size; 76 // average allocation amount. We accomplish this by first populating 77 // mAllocSizes until its size equals mWindowSize. After that, we just 78 // assign to the entry identified by mIndex. 79 if ( mAllocSizes.size() < mWindowSize ) 80 { 81 mAllocSizes.push_back(alloc_size); 82 } 83 else 84 { 85 mAllocSizes[mIndex] = alloc_size; 86 } 87 90 88 mIndex = (mIndex + 1) % mWindowSize; 91 92 // Incrementing mAllocOffset beyond mWindowSize eliminates the need for it.93 // Perhaps there is a better way of keeping track of the number of94 // allocation sizes stored in mAllocSizes.95 ++mAllocOffset;96 89 97 90 return alloc_size; … … 100 93 size_t AveragingAllocationStrategy::computeAverage() const 101 94 { 102 // The smaller of mAllocOffset and mWindowSize provide the number of 103 // allocation sizes that have been stored in mAllocSizes. 104 const size_type count(minValue(mAllocOffset, mWindowSize)); 105 const size_t sum = std::accumulate(mAllocSizes.begin(), 106 mAllocSizes.begin() + count, 0); 95 size_t average(0); 107 96 108 // Return the average allocation size. 109 return sum / count; 97 if ( ! mAllocSizes.empty() ) 98 { 99 const size_t sum(std::accumulate(mAllocSizes.begin(), 100 mAllocSizes.end(), 0)); 101 102 // Compute the average allocation size. 103 average = sum / mAllocSizes.size(); 104 } 105 106 return average; 110 107 } 111 108 juggler/trunk/modules/vapor/vpr/IO/Socket/AveragingAllocationStrategy.h
r21007 r21010 100 100 size_type mIndex; 101 101 std::vector<size_t> mAllocSizes; 102 size_type mAllocOffset;103 102 //@} 104 103 };
