You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a vague proposal / food for thought / example of how I use AppleCake. It might not make sense to add this functionality to AppleCake itself, but I would like to at least share what I did - maybe it's going to be useful to somebody else.
Ever since I started using AppleCake more extensively, I realised it became a bit of a pain to wrangle the profile objects and to make sure the methods on different objects weren't re-using calls of the same method on different objects or recursive calls on the same method.
I realise the OO approach isn't a first-class thing in Lua and there's many ways to get it (I'm using middleclass right now), but it was an important enough use-case for me that I built up an AppleCake utility mixin to handle that.
Here's how I use it in real code:
ShaderStructArray=class('ShaderStructArray')
-- object fields to include in the profile argsProfiledObject.includeIn(ShaderStructArray, {fields= {'name'}})
-- example methodfunctionShaderStructArray:markUnchanged()
self:trace()
for_, structinipairs(self._elements) dostruct:markUnchanged()
endself:untrace()
end
This allows me to easily get more complicated traces like this one:
There's probably a better and more generic way of implementing it, but here's the code for the ProfiledObject mixin (you'll find some chunks of it stolen from the AppleCake implementation):
localfunction_methodNameAndPath(self)
localclassName=self.class.nameor'UnknownClass'localname='unknownName'localpath='unknownPath'localinfo=debug.getinfo(3, "fnS")
localline=""ifinfothenifinfo.namethenname=info.nameelseifinfo.functhen-- Attempt to create a name from memory addressname=tostring(info.func):sub(10)
elseerror("Could not generate name for this function")
endifinfo.short_srcthenline= (info.linedefinedand"#"..info.linedefinedorline)
path=info.short_src..lineendend-- return className .. '.' .. name .. '[' .. line .. ']', pathreturnstring.format('%s.%s[%s]', className, name, line), pathendlocalprofilesFieldName='_objectProfiles'localfunction_profiles(self)
localprofiles=rawget(self, profilesFieldName)
ifnotprofilesthenprofiles= {}
rawset(self, profilesFieldName, profiles)
endreturnprofilesendlocalfunctionbuildArgs(self, args)
args=argsor {}
ifself.class.profiledFieldNamesthenfor_, fieldNameinipairs(self.class.profiledFieldNames) doargs[fieldName] =rawget(self, fieldName)
endendreturnargsendlocalfunction_profileName(baseName, recursionDepth)
ifrecursionDepthandrecursionDepth>0then-- baseName ..returnbaseName..'_level_' ..tostring(recursionDepth)
elsereturnbaseNameendendlocalfunctionstartProfile(self, args, recursionDepth)
localname, path=_methodNameAndPath(self)
args=buildArgs(self, args)
-- print('method', _methodNameAndPath(self))args.path=pathlocalprofileName=_profileName(name, recursionDepth)
localprofile=_profiles(self)[profileName]
profile=AppleCake.profile(name, args, profile)
_profiles(self)[profileName] =profileendlocalfunctionmark(self, name, subname)
name=tostring(name)
ifsubnamethenAppleCake.mark(name..' -> ' ..tostring(subname))
elseAppleCake.mark(name)
endendlocalfunctionstopProfile(self, recursionDepth)
localprofileName, _=_methodNameAndPath(self)
profileName=_profileName(profileName, recursionDepth)
localprofile=_profiles(self)[profileName]
profile:stop()
endProfiledObject= {
startProfile=startProfile,
stopProfile=stopProfile,
trace=startProfile,
untrace=stopProfile,
mark=mark,
}
ProfiledObject.includeIn=function(klass, opts)
klass.profiledFieldNames=opts.fieldsor {}
klass:include(ProfiledObject)
end
AppleCake itself is impressively low-overhead, so I tried to keep the code light here and to have it not do too much unnecessary work.
Feel free to close the issue as I'm not sure it's really actionable, but again - I thought it was worth sharing
The text was updated successfully, but these errors were encountered:
This is a vague proposal / food for thought / example of how I use AppleCake. It might not make sense to add this functionality to AppleCake itself, but I would like to at least share what I did - maybe it's going to be useful to somebody else.
Ever since I started using AppleCake more extensively, I realised it became a bit of a pain to wrangle the
profile
objects and to make sure the methods on different objects weren't re-using calls of the same method on different objects or recursive calls on the same method.I realise the OO approach isn't a first-class thing in Lua and there's many ways to get it (I'm using middleclass right now), but it was an important enough use-case for me that I built up an AppleCake utility mixin to handle that.
Here's how I use it in real code:
This allows me to easily get more complicated traces like this one:

There's probably a better and more generic way of implementing it, but here's the code for the
ProfiledObject
mixin (you'll find some chunks of it stolen from the AppleCake implementation):AppleCake itself is impressively low-overhead, so I tried to keep the code light here and to have it not do too much unnecessary work.
Feel free to close the issue as I'm not sure it's really actionable, but again - I thought it was worth sharing
The text was updated successfully, but these errors were encountered: