From: Francois Fleuret Date: Mon, 5 Dec 2016 10:33:09 +0000 (+0100) Subject: Decorating the model themselves (and not the class) makes more sense as it allows... X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=profiler-torch.git;a=commitdiff_plain;h=545f83595445ffd1212cbe7f23e76f906b1ff4b4 Decorating the model themselves (and not the class) makes more sense as it allows to decorate only some models or parts of models, which may be desirable sometimes. --- diff --git a/profiler.lua b/profiler.lua index 6f63861..b95d750 100644 --- a/profiler.lua +++ b/profiler.lua @@ -48,22 +48,11 @@ function profiler.decor(model, functionsToDecorate) for _, name in pairs(functionsToDecorate) do model.accTime = {} - local functionTable = model - - -- We decorate the function where it is defined in the class - -- hierarchy, so we have to go up the metatables until we find - -- it with rawget - - while functionTable and not rawget(functionTable, name) do - functionTable = getmetatable(functionTable) - end - local nameOrig = name .. '__orig' - if functionTable[name] and not functionTable[nameOrig] then - print('Profiler decoring ' .. functionTable.__typename .. '.' .. name) - functionTable[nameOrig] = functionTable[name] - functionTable[name] = function(self, ...) + if model[name] and not model[nameOrig] then + model[nameOrig] = model[name] + model[name] = function(self, ...) local startTime = sys.clock() local result = { self[nameOrig](self, unpack({...})) } local endTime = sys.clock()