Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit fa39a3a

Browse files
committed
[[ Foundation ]] Make single byte fetching more efficient.
When copying a data range which is of length 1, libfoundation will now use a pre-allocated, immutable MCDataRef. A vector of 256 MCDataRefs, corresponding to each single byte value is created on startup. (cherry picked from commit 215f593)
1 parent 5ba5423 commit fa39a3a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

libfoundation/src/foundation-data.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include "foundation-private.h"
2121

2222

23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
// An array of all possible (256) single byte DataRefs.
26+
static MCDataRef *__kMCSingleBytes;
2327

2428
////////////////////////////////////////////////////////////////////////////////
2529

@@ -368,6 +372,14 @@ bool MCDataCopyRange(MCDataRef self, MCRange p_range, MCDataRef& r_new_data)
368372

369373
__MCDataClampRange(self, p_range);
370374

375+
// We special-case when the length of the range to be copied is 1. Rather
376+
// than create a brand new DataRef, we use one of the pre-allocated ones.
377+
if (p_range . length == 1)
378+
{
379+
r_new_data = MCValueRetain(__kMCSingleBytes[self -> bytes[p_range . offset]]);
380+
return true;
381+
}
382+
371383
return MCDataCreateWithBytes(self -> bytes + p_range . offset, p_range . length, r_new_data);
372384
}
373385

@@ -715,11 +727,29 @@ bool __MCDataInitialize(void)
715727
if (!MCDataCreateWithBytes(nil, 0, kMCEmptyData))
716728
return false;
717729

730+
// Allocate the array for the 256 single byte DataRefs.
731+
if (!MCMemoryNewArray(256, __kMCSingleBytes))
732+
return false;
733+
734+
// Create each single byte DataRef.
735+
for(uindex_t i = 0; i < 256; i++)
736+
{
737+
byte_t t_byte;
738+
t_byte = (byte_t)i;
739+
if (!MCDataCreateWithBytes(&t_byte, 1, __kMCSingleBytes[i]))
740+
return false;
741+
}
742+
718743
return true;
719744
}
720745

721746
void __MCDataFinalize(void)
722747
{
748+
// Deallocate all the single byte DataRefs and their holding array.
749+
for(uindex_t i = 0; i < 256; i++)
750+
MCValueRelease(__kMCSingleBytes[i]);
751+
MCMemoryDeleteArray(__kMCSingleBytes);
752+
723753
MCValueRelease(kMCEmptyData);
724754
}
725755

0 commit comments

Comments
 (0)