NANCY: Add reserve() calls before initializing arrays

Added calls to reserve() before initializing most arrays in the engine to reduce unnecessary allocations when pushing back elements one by one.
This commit is contained in:
fracturehill 2021-03-20 18:18:29 +02:00 committed by Eugene Sandulenko
parent 99da486454
commit bfc6586f78
18 changed files with 72 additions and 23 deletions

View file

@ -127,6 +127,7 @@ bool ActionManager::addNewActionRecord(Common::SeekableReadStream &inputData) {
// Initialize the dependencies data // Initialize the dependencies data
inputData.seek(localChunkSize); inputData.seek(localChunkSize);
newRecord->_dependencies.reserve(numDependencies);
for (uint16 i = 0; i < numDependencies; ++i) { for (uint16 i = 0; i < numDependencies; ++i) {
newRecord->_dependencies.push_back(DependencyRecord()); newRecord->_dependencies.push_back(DependencyRecord());
DependencyRecord &dep = newRecord->_dependencies.back(); DependencyRecord &dep = newRecord->_dependencies.back();

View file

@ -44,14 +44,17 @@ void LeverPuzzle::init() {
void LeverPuzzle::readData(Common::SeekableReadStream &stream) { void LeverPuzzle::readData(Common::SeekableReadStream &stream) {
readFilename(stream, _imageName); readFilename(stream, _imageName);
_srcRects.reserve(3);
for (uint leverID = 0; leverID < 3; ++leverID) { for (uint leverID = 0; leverID < 3; ++leverID) {
_srcRects.push_back(Common::Array<Common::Rect>()); _srcRects.push_back(Common::Array<Common::Rect>());
_srcRects.back().reserve(3);
for (uint i = 0; i < 4; ++i) { for (uint i = 0; i < 4; ++i) {
_srcRects.back().push_back(Common::Rect()); _srcRects.back().push_back(Common::Rect());
readRect(stream, _srcRects.back().back()); readRect(stream, _srcRects.back().back());
} }
} }
_destRects.reserve(3);
for (uint leverID = 0; leverID < 3; ++leverID) { for (uint leverID = 0; leverID < 3; ++leverID) {
_destRects.push_back(Common::Rect()); _destRects.push_back(Common::Rect());
readRect(stream, _destRects.back()); readRect(stream, _destRects.back());
@ -63,11 +66,14 @@ void LeverPuzzle::readData(Common::SeekableReadStream &stream) {
} }
} }
_playerSequence.reserve(3);
_leverDirection.reserve(3);
for (uint leverID = 0; leverID < 3; ++leverID) { for (uint leverID = 0; leverID < 3; ++leverID) {
_playerSequence.push_back(stream.readByte()); _playerSequence.push_back(stream.readByte());
_leverDirection.push_back(true); _leverDirection.push_back(true);
} }
_correctSequence.reserve(3);
for (uint leverID = 0; leverID < 3; ++leverID) { for (uint leverID = 0; leverID < 3; ++leverID) {
_correctSequence.push_back(stream.readByte()); _correctSequence.push_back(stream.readByte());
} }

View file

@ -56,6 +56,7 @@ void OrderingPuzzle::readData(Common::SeekableReadStream &stream) {
readFilename(stream, _imageName); readFilename(stream, _imageName);
uint16 numElements = stream.readUint16LE(); uint16 numElements = stream.readUint16LE();
_srcRects.reserve(numElements);
for (uint i = 0; i < numElements; ++i) { for (uint i = 0; i < numElements; ++i) {
_srcRects.push_back(Common::Rect()); _srcRects.push_back(Common::Rect());
readRect(stream, _srcRects.back()); readRect(stream, _srcRects.back());
@ -63,6 +64,8 @@ void OrderingPuzzle::readData(Common::SeekableReadStream &stream) {
stream.skip(16 * (15 - numElements)); stream.skip(16 * (15 - numElements));
_destRects.reserve(numElements);
_drawnElements.reserve(numElements);
for (uint i = 0; i < numElements; ++i) { for (uint i = 0; i < numElements; ++i) {
_destRects.push_back(Common::Rect()); _destRects.push_back(Common::Rect());
readRect(stream, _destRects.back()); readRect(stream, _destRects.back());
@ -80,6 +83,7 @@ void OrderingPuzzle::readData(Common::SeekableReadStream &stream) {
_sequenceLength = stream.readUint16LE(); _sequenceLength = stream.readUint16LE();
_correctSequence.reserve(15);
for (uint i = 0; i < 15; ++i) { for (uint i = 0; i < 15; ++i) {
_correctSequence.push_back(stream.readByte()); _correctSequence.push_back(stream.readByte());
} }

View file

@ -78,6 +78,7 @@ void PlayPrimaryVideoChan0::ConditionFlag::set() const {
void PlayPrimaryVideoChan0::ConditionFlags::read(Common::SeekableReadStream &stream) { void PlayPrimaryVideoChan0::ConditionFlags::read(Common::SeekableReadStream &stream) {
uint16 numFlags = stream.readUint16LE(); uint16 numFlags = stream.readUint16LE();
conditionFlags.reserve(numFlags);
for (uint i = 0; i < numFlags; ++i) { for (uint i = 0; i < numFlags; ++i) {
conditionFlags.push_back(ConditionFlag()); conditionFlags.push_back(ConditionFlag());
conditionFlags.back().read(stream); conditionFlags.back().read(stream);
@ -180,21 +181,20 @@ void PlayPrimaryVideoChan0::readData(Common::SeekableReadStream &stream) {
uint16 numResponses = stream.readUint16LE(); uint16 numResponses = stream.readUint16LE();
rawText = new char[400]; rawText = new char[400];
if (numResponses > 0) { _responses.reserve(numResponses);
for (uint i = 0; i < numResponses; ++i) { for (uint i = 0; i < numResponses; ++i) {
_responses.push_back(ResponseStruct()); _responses.push_back(ResponseStruct());
ResponseStruct &response = _responses[i]; ResponseStruct &response = _responses[i];
response.conditionFlags.read(stream); response.conditionFlags.read(stream);
stream.read(rawText, 400); stream.read(rawText, 400);
UI::Textbox::assembleTextLine(rawText, response.text, 400); UI::Textbox::assembleTextLine(rawText, response.text, 400);
readFilename(stream, response.soundName); readFilename(stream, response.soundName);
stream.skip(1); stream.skip(1);
response._sceneChange.readData(stream); response._sceneChange.readData(stream);
response.flagDesc.label = stream.readSint16LE(); response.flagDesc.label = stream.readSint16LE();
response.flagDesc.flag = (NancyFlag)stream.readByte(); response.flagDesc.flag = (NancyFlag)stream.readByte();
stream.skip(0x32); stream.skip(0x32);
}
} }
delete[] rawText; delete[] rawText;
@ -205,15 +205,14 @@ void PlayPrimaryVideoChan0::readData(Common::SeekableReadStream &stream) {
} }
uint16 numFlagsStructs = stream.readUint16LE(); uint16 numFlagsStructs = stream.readUint16LE();
if (numFlagsStructs > 0) { _flagsStructs.reserve(numFlagsStructs);
for (uint16 i = 0; i < numFlagsStructs; ++i) { for (uint16 i = 0; i < numFlagsStructs; ++i) {
_flagsStructs.push_back(FlagsStruct()); _flagsStructs.push_back(FlagsStruct());
FlagsStruct &flagsStruct = _flagsStructs.back(); FlagsStruct &flagsStruct = _flagsStructs.back();
flagsStruct.conditions.read(stream); flagsStruct.conditions.read(stream);
flagsStruct.flagToSet.type = (ConditionFlag::ConditionType)stream.readByte(); flagsStruct.flagToSet.type = (ConditionFlag::ConditionType)stream.readByte();
flagsStruct.flagToSet.flag.label = stream.readSint16LE(); flagsStruct.flagToSet.flag.label = stream.readSint16LE();
flagsStruct.flagToSet.flag.flag = (NancyFlag)stream.readByte(); flagsStruct.flagToSet.flag.flag = (NancyFlag)stream.readByte();
}
} }
} }

View file

@ -53,6 +53,7 @@ void HotMultiframeSceneChange::readData(Common::SeekableReadStream &stream) {
SceneChange::readData(stream); SceneChange::readData(stream);
uint16 numHotspots = stream.readUint16LE(); uint16 numHotspots = stream.readUint16LE();
_hotspots.reserve(numHotspots);
for (uint i = 0; i < numHotspots; ++i) { for (uint i = 0; i < numHotspots; ++i) {
_hotspots.push_back(HotspotDescription()); _hotspots.push_back(HotspotDescription());
HotspotDescription &newDesc = _hotspots[i]; HotspotDescription &newDesc = _hotspots[i];
@ -152,6 +153,7 @@ void MapCallHot1Fr::execute() {
void MapCallHotMultiframe::readData(Common::SeekableReadStream &stream) { void MapCallHotMultiframe::readData(Common::SeekableReadStream &stream) {
uint16 numDescs = stream.readUint16LE(); uint16 numDescs = stream.readUint16LE();
_hotspots.reserve(numDescs);
for (uint i = 0; i < numDescs; ++i) { for (uint i = 0; i < numDescs; ++i) {
_hotspots.push_back(HotspotDescription()); _hotspots.push_back(HotspotDescription());
_hotspots[i].readData(stream); _hotspots[i].readData(stream);
@ -254,6 +256,7 @@ void EventFlagsMultiHS::readData(Common::SeekableReadStream &stream) {
EventFlags::readData(stream); EventFlags::readData(stream);
uint16 numHotspots = stream.readUint16LE(); uint16 numHotspots = stream.readUint16LE();
_hotspots.reserve(numHotspots);
for (uint16 i = 0; i < numHotspots; ++i) { for (uint16 i = 0; i < numHotspots; ++i) {
_hotspots.push_back(HotspotDescription()); _hotspots.push_back(HotspotDescription());
HotspotDescription &newDesc = _hotspots[i]; HotspotDescription &newDesc = _hotspots[i];
@ -360,6 +363,7 @@ void ShowInventoryItem::readData(Common::SeekableReadStream &stream) {
uint16 numFrames = stream.readUint16LE(); uint16 numFrames = stream.readUint16LE();
_bitmaps.reserve(numFrames);
for (uint i = 0; i < numFrames; ++i) { for (uint i = 0; i < numFrames; ++i) {
_bitmaps.push_back(BitmapDescription()); _bitmaps.push_back(BitmapDescription());
_bitmaps[i].readData(stream); _bitmaps[i].readData(stream);
@ -462,6 +466,7 @@ void PlaySoundMultiHS::readData(Common::SeekableReadStream &stream) {
stream.skip(2); stream.skip(2);
uint16 numHotspots = stream.readUint16LE(); uint16 numHotspots = stream.readUint16LE();
_hotspots.reserve(numHotspots);
for (uint i = 0; i < numHotspots; ++i) { for (uint i = 0; i < numHotspots; ++i) {
_hotspots.push_back(HotspotDescription()); _hotspots.push_back(HotspotDescription());
_hotspots.back().frameID = stream.readUint16LE(); _hotspots.back().frameID = stream.readUint16LE();

View file

@ -48,11 +48,13 @@ void RotatingLockPuzzle::readData(Common::SeekableReadStream &stream) {
uint numDials = stream.readUint16LE(); uint numDials = stream.readUint16LE();
_srcRects.reserve(10);
for (uint i = 0; i < 10; ++i) { for (uint i = 0; i < 10; ++i) {
_srcRects.push_back(Common::Rect()); _srcRects.push_back(Common::Rect());
readRect(stream, _srcRects.back()); readRect(stream, _srcRects.back());
} }
_destRects.reserve(numDials);
for (uint i = 0; i < numDials; ++i) { for (uint i = 0; i < numDials; ++i) {
_destRects.push_back(Common::Rect()); _destRects.push_back(Common::Rect());
readRect(stream, _destRects.back()); readRect(stream, _destRects.back());
@ -66,11 +68,13 @@ void RotatingLockPuzzle::readData(Common::SeekableReadStream &stream) {
stream.skip((8 - numDials) * 16); stream.skip((8 - numDials) * 16);
_upHotspots.reserve(numDials);
for (uint i = 0; i < numDials; ++i) { for (uint i = 0; i < numDials; ++i) {
_upHotspots.push_back(Common::Rect()); _upHotspots.push_back(Common::Rect());
readRect(stream, _upHotspots.back()); readRect(stream, _upHotspots.back());
} }
_downHotspots.reserve(numDials);
stream.skip((8 - numDials) * 16); stream.skip((8 - numDials) * 16);
for (uint i = 0; i < numDials; ++i) { for (uint i = 0; i < numDials; ++i) {
@ -80,6 +84,7 @@ void RotatingLockPuzzle::readData(Common::SeekableReadStream &stream) {
stream.skip((8 - numDials) * 16); stream.skip((8 - numDials) * 16);
_correctSequence.reserve(numDials);
for (uint i = 0; i < numDials; ++i) { for (uint i = 0; i < numDials; ++i) {
_correctSequence.push_back(stream.readByte()); _correctSequence.push_back(stream.readByte());
} }

View file

@ -59,6 +59,7 @@ void PlaySecondaryMovie::readData(Common::SeekableReadStream &stream) {
_sceneChange.readData(stream); _sceneChange.readData(stream);
uint16 numVideoDescs = stream.readUint16LE(); uint16 numVideoDescs = stream.readUint16LE();
_videoDescs.reserve(numVideoDescs);
for (uint i = 0; i < numVideoDescs; ++i) { for (uint i = 0; i < numVideoDescs; ++i) {
_videoDescs.push_back(SecondaryVideoDescription()); _videoDescs.push_back(SecondaryVideoDescription());
_videoDescs[i].readData(stream); _videoDescs[i].readData(stream);

View file

@ -170,6 +170,7 @@ void PlaySecondaryVideo::readData(Common::SeekableReadStream &stream) {
} }
uint16 numVideoDescs = stream.readUint16LE(); uint16 numVideoDescs = stream.readUint16LE();
_videoDescs.reserve(numVideoDescs);
for (uint i = 0; i < numVideoDescs; ++i) { for (uint i = 0; i < numVideoDescs; ++i) {
_videoDescs.push_back(SecondaryVideoDescription()); _videoDescs.push_back(SecondaryVideoDescription());
_videoDescs[i].readData(stream); _videoDescs[i].readData(stream);

View file

@ -49,19 +49,26 @@ void SliderPuzzle::readData(Common::SeekableReadStream &stream) {
_width = stream.readUint16LE(); _width = stream.readUint16LE();
_height = stream.readUint16LE(); _height = stream.readUint16LE();
_srcRects.reserve(_height);
for (uint y = 0; y < _height; ++y) { for (uint y = 0; y < _height; ++y) {
_srcRects.push_back(Common::Array<Common::Rect>()); _srcRects.push_back(Common::Array<Common::Rect>());
_srcRects.back().reserve(_width);
for (uint x = 0; x < _width; ++x) { for (uint x = 0; x < _width; ++x) {
_srcRects.back().push_back(Common::Rect()); _srcRects.back().push_back(Common::Rect());
readRect(stream, _srcRects.back().back()); readRect(stream, _srcRects.back().back());
} }
stream.skip((6 - _width) * 16); stream.skip((6 - _width) * 16);
} }
stream.skip((6 - _height) * 6 * 16); stream.skip((6 - _height) * 6 * 16);
_destRects.reserve(_height);
for (uint y = 0; y < _height; ++y) { for (uint y = 0; y < _height; ++y) {
_destRects.push_back(Common::Array<Common::Rect>()); _destRects.push_back(Common::Array<Common::Rect>());
_destRects.back().reserve(_width);
for (uint x = 0; x < _width; ++x) { for (uint x = 0; x < _width; ++x) {
_destRects.back().push_back(Common::Rect()); _destRects.back().push_back(Common::Rect());
readRect(stream, _destRects.back().back()); readRect(stream, _destRects.back().back());
@ -77,11 +84,15 @@ void SliderPuzzle::readData(Common::SeekableReadStream &stream) {
stream.skip((6 - _height) * 6 * 16); stream.skip((6 - _height) * 6 * 16);
_correctTileOrder.reserve(_height);
for (uint y = 0; y < _height; ++y) { for (uint y = 0; y < _height; ++y) {
_correctTileOrder.push_back(Common::Array<int16>()); _correctTileOrder.push_back(Common::Array<int16>());
_correctTileOrder.back().reserve(_width);
for (uint x = 0; x < _width; ++x) { for (uint x = 0; x < _width; ++x) {
_correctTileOrder.back().push_back(stream.readSint16LE()); _correctTileOrder.back().push_back(stream.readSint16LE());
} }
stream.skip((6 - _width) * 2); stream.skip((6 - _width) * 2);
} }

View file

@ -69,11 +69,13 @@ void PlayStaticBitmapAnimation::readData(Common::SeekableReadStream &stream) {
_sound.read(stream, SoundDescription::kNormal); _sound.read(stream, SoundDescription::kNormal);
uint numViewportFrames = stream.readUint16LE(); uint numViewportFrames = stream.readUint16LE();
_srcRects.reserve(_loopLastFrame - _firstFrame);
for (uint i = _firstFrame; i <= _loopLastFrame; ++i) { for (uint i = _firstFrame; i <= _loopLastFrame; ++i) {
_srcRects.push_back(Common::Rect()); _srcRects.push_back(Common::Rect());
readRect(stream, _srcRects[i]); readRect(stream, _srcRects[i]);
} }
_bitmaps.reserve(numViewportFrames);
for (uint i = 0; i < numViewportFrames; ++i) { for (uint i = 0; i < numViewportFrames; ++i) {
_bitmaps.push_back(BitmapDescription()); _bitmaps.push_back(BitmapDescription());
BitmapDescription &rects = _bitmaps.back(); BitmapDescription &rects = _bitmaps.back();

View file

@ -52,11 +52,13 @@ void Telephone::init() {
void Telephone::readData(Common::SeekableReadStream &stream) { void Telephone::readData(Common::SeekableReadStream &stream) {
readFilename(stream, _imageName); readFilename(stream, _imageName);
_srcRects.reserve(12);
for (uint i = 0; i < 12; ++i) { for (uint i = 0; i < 12; ++i) {
_srcRects.push_back(Common::Rect()); _srcRects.push_back(Common::Rect());
readRect(stream, _srcRects.back()); readRect(stream, _srcRects.back());
} }
_destRects.reserve(12);
for (uint i = 0; i < 12; ++i) { for (uint i = 0; i < 12; ++i) {
_destRects.push_back(Common::Rect()); _destRects.push_back(Common::Rect());
readRect(stream, _destRects.back()); readRect(stream, _destRects.back());
@ -75,6 +77,7 @@ void Telephone::readData(Common::SeekableReadStream &stream) {
_dialAgainSound.read(stream, SoundDescription::kNormal); _dialAgainSound.read(stream, SoundDescription::kNormal);
_hangUpSound.read(stream, SoundDescription::kNormal); _hangUpSound.read(stream, SoundDescription::kNormal);
_buttonSoundNames.reserve(12);
for (uint i = 0; i < 12; ++i) { for (uint i = 0; i < 12; ++i) {
Common::String buttonSoundName; Common::String buttonSoundName;
readFilename(stream, buttonSoundName); readFilename(stream, buttonSoundName);
@ -100,10 +103,12 @@ void Telephone::readData(Common::SeekableReadStream &stream) {
uint numCalls = stream.readUint16LE(); uint numCalls = stream.readUint16LE();
_calls.reserve(numCalls);
for (uint i = 0; i < numCalls; ++i) { for (uint i = 0; i < numCalls; ++i) {
_calls.push_back(PhoneCall()); _calls.push_back(PhoneCall());
PhoneCall &call = _calls.back(); PhoneCall &call = _calls.back();
call.phoneNumber.reserve(11);
for (uint j = 0; j < 11; ++j) { for (uint j = 0; j < 11; ++j) {
call.phoneNumber.push_back(stream.readByte()); call.phoneNumber.push_back(stream.readByte());
} }

View file

@ -38,6 +38,7 @@ void CursorManager::init() {
Common::String inventoryCursorsImageName = chunk->readString(); Common::String inventoryCursorsImageName = chunk->readString();
chunk = g_nancy->getBootChunkStream("CURS"); chunk = g_nancy->getBootChunkStream("CURS");
_cursors.reserve(56);
for (uint i = 0; i < 56; ++i) { for (uint i = 0; i < 56; ++i) {
_cursors.push_back(Cursor()); _cursors.push_back(Cursor());
chunk->seek(i * 16, SEEK_SET); chunk->seek(i * 16, SEEK_SET);

View file

@ -70,6 +70,7 @@ void Font::read(Common::SeekableReadStream &stream) {
_semicolonOffset = stream.readUint16LE(); _semicolonOffset = stream.readUint16LE();
_slashOffset = stream.readUint16LE(); _slashOffset = stream.readUint16LE();
_symbolRects.reserve(78);
for (uint i = 0; i < 78; ++i) { for (uint i = 0; i < 78; ++i) {
_symbolRects.push_back(Common::Rect()); _symbolRects.push_back(Common::Rect());
Common::Rect &cur = _symbolRects[i]; Common::Rect &cur = _symbolRects[i];

View file

@ -146,6 +146,7 @@ uint32 IFF::stringToId(const Common::String &s) {
} }
void IFF::list(Common::Array<Common::String> &nameList) { void IFF::list(Common::Array<Common::String> &nameList) {
nameList.reserve(_chunks.size());
for (uint i = 0; i < _chunks.size(); ++i) { for (uint i = 0; i < _chunks.size(); ++i) {
nameList.push_back(idToString(_chunks[i].id)); nameList.push_back(idToString(_chunks[i].id));
} }

View file

@ -233,6 +233,7 @@ bool CifTree::initialize() {
if (f.eos()) if (f.eos())
error("Error reading CifTree '%s'", _name.c_str()); error("Error reading CifTree '%s'", _name.c_str());
_cifInfo.reserve(infoBlockCount);
for (int i = 0; i < infoBlockCount; i++) { for (int i = 0; i < infoBlockCount; i++) {
CifInfoChain chain; CifInfoChain chain;
readCifInfo(f, chain); readCifInfo(f, chain);

View file

@ -84,6 +84,7 @@ void Map::init() {
_locations.clear(); _locations.clear();
_locations.reserve(4);
for (uint i = 0; i < 4; ++i) { for (uint i = 0; i < 4; ++i) {
chunk->seek(0x162 + i * 16, SEEK_SET); chunk->seek(0x162 + i * 16, SEEK_SET);
_locations.push_back(Location()); _locations.push_back(Location());
@ -96,6 +97,7 @@ void Map::init() {
loc.isActive = true; loc.isActive = true;
} }
loc.scenes.reserve(2);
for (uint j = 0; j < 2; ++j) { for (uint j = 0; j < 2; ++j) {
loc.scenes.push_back(Location::SceneChange()); loc.scenes.push_back(Location::SceneChange());
Location::SceneChange &sc = loc.scenes[j]; Location::SceneChange &sc = loc.scenes[j];

View file

@ -397,6 +397,7 @@ void Scene::init() {
_hintsRemaining.clear(); _hintsRemaining.clear();
_hintsRemaining.reserve(3);
for (uint i = 0; i < 3; ++i) { for (uint i = 0; i < 3; ++i) {
_hintsRemaining.push_back(chunk->readByte()); _hintsRemaining.push_back(chunk->readByte());
} }
@ -575,6 +576,7 @@ void Scene::initStaticData() {
// Hardcoded by original engine // Hardcoded by original engine
_mapAccessSceneIDs.clear(); _mapAccessSceneIDs.clear();
_mapAccessSceneIDs.reserve(8);
_mapAccessSceneIDs.push_back(9); _mapAccessSceneIDs.push_back(9);
_mapAccessSceneIDs.push_back(10); _mapAccessSceneIDs.push_back(10);
_mapAccessSceneIDs.push_back(11); _mapAccessSceneIDs.push_back(11);

View file

@ -114,6 +114,7 @@ AVFDecoder::AVFVideoTrack::AVFVideoTrack(Common::SeekableReadStream *stream, uin
_surface->create(_width, _height, _pixelFormat); _surface->create(_width, _height, _pixelFormat);
_frameSize = _width * _height * _pixelFormat.bytesPerPixel; _frameSize = _width * _height * _pixelFormat.bytesPerPixel;
_chunkInfo.reserve(_frameCount);
for (uint i = 0; i < _frameCount; i++) { for (uint i = 0; i < _frameCount; i++) {
ChunkInfo info; ChunkInfo info;