View Single Post
07-12-19, 10:40 AM   #12
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Originally Posted by Kanegasi View Post
I feel it's important to point out that RAM usage is not a performance issue, except for leak cases such as this thread you created or playing WoW with less than 8GB of RAM. Many addons, "Details" being a great example, save on a lot of CPU by caching as much info as possible.

CPU will always be WoW's bottleneck. Do not be afraid to cache as much data as possible instead of querying the API. The less you do each frame, especially in combat, the better.
In my addon Scorpio, I try to solve the performance problem not by less operations, but with a task schedule system to smoothing the operations like

Lua Code:
  1. Scorpio "TestBigLoop" ""
  2.  
  3. __Async__()
  4. function LongLoop()
  5.     local time = GetTime()
  6.     local prev = 0
  7.  
  8.     for i = 1, 10^7 do
  9.         if i%10 == 0 then
  10.             Continue() -- The frame will freeze if miss this
  11.  
  12.             if time ~= GetTime() then
  13.                 -- Means the thread is resumed in the next frame through OnUpdate
  14.                 time = GetTime()
  15.  
  16.                 -- Here is the current time and the cycle count of the previous phase
  17.                 print(time, i - prev)
  18.                 prev = i
  19.             end
  20.         end
  21.     end
  22. end
  23.  
  24. LongLoop()

The function LongLoop will be processed in a coroutine, and the Continue method will yield it if there is no more time for tasks in the same frame and resume it in the next frame if there is enough time.

This is how the script runs in my old laptop(i5-3230M), I changed a setting to keep the fps to 60, so for one frame the loop will be processed for 2k6-4k times, with the task schedule system I can smooth all my addons to keep the high fps(for my old hardware).

Click image for larger version

Name:	test.jpg
Views:	246
Size:	91.9 KB
ID:	9251

Besides the Continue, there are many others like Wait to wait for several system events, so for me, I can re-code Terenna's code like :

Lua Code:
  1. Scorpio "ShowPosition" ""
  2.  
  3. tCoordsFrame = UIParent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  4. tCoordsFrame:SetPoint("CENTER")
  5.  
  6. __Async__()
  7. function RefreshPosition()
  8.     local GetBestMapForUnit     = C_Map.GetBestMapForUnit  
  9.     local MapRects              = {}
  10.     local UnitPosition          = UnitPosition
  11.     local GetWorldPosFromMapPos = C_Map.GetWorldPosFromMapPos
  12.     local CreateVector2D        = CreateVector2D
  13.  
  14.     while true do
  15.         if IsPlayerMoving() then
  16.             local mapID         = GetBestMapForUnit('player')
  17.  
  18.             local x, y          = UnitPosition('player')
  19.             if x and mapID then
  20.                 local rects     = MapRects[mapID]
  21.                 if not rects then
  22.                     local _, topleft        = GetWorldPosFromMapPos(mapID, CreateVector2D(0,0))
  23.                     local _, bottomright    = GetWorldPosFromMapPos(mapID, CreateVector2D(1,1))
  24.          
  25.                     bottomright:Subtract(topleft)
  26.                     rects = { topleft.x, topleft.y, bottomright.x, bottomright.y }
  27.                     MapRects[mapID]         = rects
  28.                 end
  29.                
  30.                 x, y = x - rects[1], y - rects[2]
  31.                 x, y = y / rects[4], x / rects[3]
  32.  
  33.                 tCoordsFrame:SetFormattedText('%.1f, %.1f', x * 100, y * 100)
  34.  
  35.                 Delay(0.5)   -- wait 0.5 sec
  36.             else
  37.                 tCoordsFrame:SetText("")
  38.                 -- wait player change zone
  39.                 Wait("PLAYER_ENTERING_WORLD", "ZONE_CHANGED", "ZONE_CHANGED_INDOORS")
  40.             end
  41.         else
  42.             Wait("PLAYER_STARTED_MOVING") -- wait player move
  43.         end
  44.     end
  45. end
  46.  
  47. RefreshPosition()

Although I need more codes(include the framework) to do the same operations, we still can gain better performance through carefully coroutine controls.

Last edited by kurapica.igas : 07-12-19 at 10:44 AM.
  Reply With Quote