-
Notifications
You must be signed in to change notification settings - Fork 248
Fix profiler benchmarking sending unserializable data #6985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| -- Global access: 0.122ms | ||
| -- Global CFunc Access: 6.35ms | ||
| -- Upvalue access: 0.078ms | ||
| -- Upvalued CFunc Access: 6.23ms | ||
| -- Local access: 0.061ms | ||
| -- Local CFunc Access: 6.29ms | ||
|
|
||
| -- Conclusion: It is faster to access values in any way through lua instead of using CFuncs. | ||
|
|
||
| ModuleName = "Value access" | ||
| BenchmarkData = { | ||
| BrainGlobalAccess = "Global access", | ||
| BrainUpvalueAccess = "Upvalue access", | ||
| BrainLocalAccess = "Local access", | ||
| BrainGlobalCFuncAccess = "Global CFunc Access", | ||
| BrainUpvaluedCFuncAccess = "Upvalued CFunc Access", | ||
| BrainLocalCFuncAccess = "Local CFunc Access", | ||
| } | ||
|
|
||
| function BrainGlobalAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = ArmyBrains[1] | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end | ||
|
|
||
| local ArmyBrains = ArmyBrains | ||
| function BrainUpvalueAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = ArmyBrains[1] | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end | ||
|
|
||
| function BrainLocalAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
| local armyBrains = ArmyBrains | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = armyBrains[1] | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end | ||
|
|
||
| function BrainGlobalCFuncAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = GetArmyBrain(1) | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end | ||
|
|
||
| local GetArmyBrain = GetArmyBrain | ||
| function BrainUpvaluedCFuncAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = GetArmyBrain(1) | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end | ||
|
|
||
| function BrainLocalCFuncAccess(loop) | ||
| local timer = GetSystemTimeSecondsOnlyForProfileUse | ||
|
|
||
| local getArmyBrain = GetArmyBrain | ||
|
|
||
| local a | ||
|
|
||
| local start = timer() | ||
|
|
||
| for _ = 1, loop do | ||
| a = getArmyBrain(1) | ||
| end | ||
|
|
||
| local final = timer() | ||
| return final - start | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -569,3 +569,55 @@ function RotateVectorXYZByQuat(vX, vY, vZ, q) | |
| vY * qW + vZ * qX + vW * qY - vX * qZ, | ||
| vZ * qW - vY * qX + vX * qY + vW * qZ | ||
| end | ||
|
|
||
| --- These are the types that the engine can send across the ui-sim boundary. | ||
| --- Tables have to be handled for loops and unserializable keys/values. | ||
| SERIALIZABLE_TYPES = { | ||
| number = true, | ||
| string = true, | ||
| boolean = true, | ||
| ["nil"] = true, | ||
| } | ||
| local serializableTypes = SERIALIZABLE_TYPES | ||
|
|
||
| --- Returns a deep copy with unserializable values converted using `tostring` | ||
| --- and cyclic references converted to strings referencing the `_seenTableId` key. | ||
| --- The result can be sent across the ui-sim boundary. | ||
| ---@generic T | ||
| ---@param t T | ||
| ---@return T | ||
| function SerializableDeepCopy(t) | ||
| local st = serializableTypes | ||
| local type_t = type(t) | ||
| if type_t ~= 'table' then | ||
| return st[type_t] and t or tostring(t) | ||
| end | ||
|
|
||
| local backrefs = {} | ||
| local function CreateSerializableAny(_t) | ||
| local type_t = type(_t) | ||
| if type_t ~= 'table' then | ||
| return st[type_t] and _t or tostring(_t) | ||
| end | ||
|
|
||
| local b = backrefs[_t] | ||
| if b then | ||
| -- store table id since table `tostring` uses memory location which won't be the same between Sim and UI | ||
| if not b._seenTableId then | ||
| b._seenTableId = tostring(_t) | ||
Hdt80bro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| -- format makes it easy to find in repr output | ||
| return '_seenTableId = "' .. b._seenTableId .. '"' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the table ID never changes, why dirty the table with an unoriginal field to store the seen table ID? We could use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The table ID should change after being copied across the sim-ui boundary, so the table
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you already are using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is not true because the table is presumedly copied across sim-ui, where the field will be filled with tostring applied to the old table before the copying. When you read the copied table you need the old
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see - I thought |
||
| end | ||
|
|
||
| local r = {} | ||
| backrefs[_t] = r | ||
| for k, v in _t do | ||
| r[CreateSerializableAny(k)] = CreateSerializableAny(v) | ||
| end | ||
|
|
||
| return r | ||
| end | ||
|
|
||
| return CreateSerializableAny(t) | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.