History

Use QHash for _timerFunctionMap instead of std::unordered_map

Ada committed 4 months ago

Commit 42bbbaf1 parent 461e32f2 files · +18 −14Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+17−13
@@ -1171,8 +1171,8 @@ void ScriptManager::run() { · 1170 unmodified lines
11711171 // NOTE: This is private because it must be called on the same thread that created the timers, which is why
11721172 // we want to only call it in our own run "shutdown" processing.
11731173 void ScriptManager::stopAllTimers() {
1174− for (const auto& [handle, value] : _timerFunctionMap) {
1175− QTimer* timer = std::get<0>(value);
1174+ for (const auto& pair : _timerFunctionMap) {
1175+ QTimer* timer = std::get<0>(pair);
11761176
11771177 timer->stop();
11781178 delete timer;
@@ -1183,10 +1183,15 @@ void ScriptManager::stopAllTimers() { · 4 unmodified lines
11831183 }
11841184
11851185 void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) {
1186− std::vector<int> toDelete;
1186+ std::vector<int> toDelete = {};
11871187
1188− for (const auto& [handle, value] : _timerFunctionMap) {
1189− const auto& [timer, callbackData] = value;
1188+ for (
1189+ auto i = _timerFunctionMap.constKeyValueBegin();
1190+ i != _timerFunctionMap.constKeyValueEnd();
1191+ ++i
1192+ ) {
1193+ const auto& handle = i->first;
1194+ const auto& [timer, callbackData] = i->second;
11901195
11911196 if (callbackData.definingEntityIdentifier != entityID) { continue; }
11921197
@@ -1197,7 +1202,7 @@ void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) { · 4 unmodified lines
11971202 }
11981203
11991204 for (auto handle : toDelete) {
1200− _timerFunctionMap.erase(handle);
1205+ _timerFunctionMap.remove(handle);
12011206 }
12021207
12031208 // the timer map is potentially shared across multiple entity scripts,
@@ -1252,11 +1257,11 @@ void ScriptManager::timerFired(int handle) { · 48 unmodified lines
12521257 return;
12531258 }
12541259
1255− auto [timer, data] = _timerFunctionMap.at(handle);
1260+ auto [timer, data] = _timerFunctionMap.value(handle);
12561261
12571262 if (!timer->isActive()) {
12581263 // this timer is done, we can kill it
1259− _timerFunctionMap.erase(handle);
1264+ _timerFunctionMap.remove(handle);
12601265 delete timer;
12611266 }
12621267
@@ -1278,8 +1283,7 @@ void ScriptManager::timerFired(int handle) { · 15 unmodified lines
12781283 }
12791284
12801285 int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int intervalMS, bool isSingleShot) {
1281− int handle = _timerHandleCounter;
1282− _timerHandleCounter += 1;
1286+ int handle = _timerHandleCounter++;
12831287
12841288 // create the timer, add it to the map, and start it
12851289 QTimer* newTimer = new QTimer(this);
@@ -1297,7 +1301,7 @@ int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int inter · 11 unmodified lines
12971301 connect(this, &ScriptManager::scriptEnding, newTimer, &QTimer::stop);
12981302
12991303 CallbackData timerData = { function, currentEntityIdentifier, currentSandboxURL };
1300− _timerFunctionMap.insert({ handle, { newTimer, timerData } });
1304+ _timerFunctionMap.insert(handle, { newTimer, timerData });
13011305
13021306 if (intervalMS < 0) {
13031307 int lineNumber = -1;
@@ -1350,9 +1354,9 @@ int ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) { · 46 unmodified lines
13501354
13511355 void ScriptManager::stopTimer(int handle) {
13521356 if (_timerFunctionMap.contains(handle)) {
1353− auto [timer, callbackData] = _timerFunctionMap.at(handle);
1357+ auto [timer, callbackData] = _timerFunctionMap.value(handle);
13541358 timer->stop();
1355− _timerFunctionMap.erase(handle);
1359+ _timerFunctionMap.remove(handle);
13561360 delete timer;
13571361 } else {
13581362 qCDebug(scriptengine) << "stopTimer -- not in _timerFunctionMap" << handle;