Skip to content

Commit 602d37c

Browse files
committed
EnsureLargeEnoughWave: Avoid resizing when not needed
We don't need to look at the minimum wave size when the requested index already fits. We now also ensure that we double the wave sizes so that we should end up always with a power of two when we started with one.
1 parent 43a1433 commit 602d37c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Packages/MIES/MIES_Utilities_WaveHandling.ipf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ threadsafe Function EnsureLargeEnoughWave(WAVE wv, [variable indexShouldExist, v
4545

4646
if(ParamIsDefault(dimension))
4747
dimension = ROWS
48+
else
49+
ASSERT_TS(dimension == ROWS || dimension == COLS || dimension == LAYERS || dimension == CHUNKS, "Invalid dimension")
4850
endif
4951

5052
if(ParamIsDefault(checkFreeMemory))
@@ -53,21 +55,18 @@ threadsafe Function EnsureLargeEnoughWave(WAVE wv, [variable indexShouldExist, v
5355
checkFreeMemory = !!checkFreeMemory
5456
endif
5557

56-
ASSERT_TS(dimension == ROWS || dimension == COLS || dimension == LAYERS || dimension == CHUNKS, "Invalid dimension")
5758
ASSERT_TS(WaveExists(wv), "Wave does not exist")
5859
ASSERT_TS(IsFinite(indexShouldExist) && indexShouldExist >= 0, "Invalid minimum size")
5960

6061
if(ParamIsDefault(indexShouldExist))
6162
indexShouldExist = MINIMUM_WAVE_SIZE - 1
62-
else
63-
indexShouldExist = max(MINIMUM_WAVE_SIZE - 1, indexShouldExist)
6463
endif
6564

6665
if(indexShouldExist < DimSize(wv, dimension))
6766
return 0
6867
endif
6968

70-
indexShouldExist *= 2
69+
indexShouldExist = max(max(2^FindNextPower(indexShouldExist, 2), 2 * DimSize(wv, dimension)), MINIMUM_WAVE_SIZE)
7170

7271
if(checkFreeMemory)
7372
if((GetWaveSize(wv) * (indexShouldExist / DimSize(wv, dimension)) / 1024 / 1024 / 1024) >= GetFreeMemory())

Packages/tests/Basic/UTF_Utils_WaveHandling.ipf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,33 @@ Function ELE_AbortsWithTooLargeValue()
160160
endtry
161161
End
162162

163+
Function ELE_DoesNothingIfIndexIsGood()
164+
165+
Make/FREE/N=(1) wv
166+
EnsurelargeEnoughWave(wv, indexShouldExist = 0)
167+
CHECK_EQUAL_VAR(DimSize(wv, ROWS), 1)
168+
End
169+
170+
Function ELE_ResizesGeometrically()
171+
172+
Make/FREE/N=(MINIMUM_WAVE_SIZE) wv
173+
EnsurelargeEnoughWave(wv, indexShouldExist = MINIMUM_WAVE_SIZE + 1)
174+
CHECK_EQUAL_VAR(DimSize(wv, ROWS), 2 * MINIMUM_WAVE_SIZE)
175+
176+
EnsurelargeEnoughWave(wv, indexShouldExist = 2 * MINIMUM_WAVE_SIZE + 1)
177+
CHECK_EQUAL_VAR(DimSize(wv, ROWS), 4 * MINIMUM_WAVE_SIZE)
178+
179+
EnsurelargeEnoughWave(wv, indexShouldExist = 9 * MINIMUM_WAVE_SIZE)
180+
CHECK_EQUAL_VAR(DimSize(wv, ROWS), 16 * MINIMUM_WAVE_SIZE)
181+
End
182+
183+
Function ELE_WorksWithEmtpyInitialSize()
184+
185+
Make/FREE/N=(0) wv
186+
EnsurelargeEnoughWave(wv, indexShouldExist = 0)
187+
CHECK_EQUAL_VAR(DimSize(wv, ROWS), MINIMUM_WAVE_SIZE)
188+
End
189+
163190
/// @}
164191

165192
// GetWaveSize

0 commit comments

Comments
 (0)