luau icon indicating copy to clipboard operation
luau copied to clipboard

Inlining doesn't always occur for functions that are used once

Open gaymeowing opened this issue 1 year ago • 0 comments

Here I have a function for parsing a date included in a git log:

local GIT_DATE_FORMAT = "(%w+) (%w+) (%d+) (%d+):(%d+):(%d+) (%d+) ([?%+%-]%d%d)(%d%d)"
local MONTHS = table.freeze({ 
	Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, 
	Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12
}) :: { [string]: number }

local MATCH = string.match

local function parse_git_date(date: string): number
	-- git log date format: "Day Mon DD HH:MM:SS YYYY +-ZZZZ"
	local _, month, day, hour, minutes, seconds, year, zone_hours, zone_minutes = MATCH(date, GIT_DATE_FORMAT)
	local zone_minutes = tonumber(zone_minutes)
	local zone_hours = tonumber(zone_hours)
	local seconds = tonumber(seconds)
	local minutes = tonumber(minutes)
	local month = MONTHS[month]
	local year = tonumber(year)
	local hour = tonumber(hour)
	local day = tonumber(day)

	if 
		day and month and hour and minutes and seconds and 
		year and zone_hours and zone_minutes
	then
		local timestamp = os.time({
			min = minutes, sec = seconds, month = month, 
			day = day, year = year, hour = hour,
		})

		return timestamp - ((zone_hours * 3600) + (zone_minutes * 60))
	else
		error(`[GIT DATE] could not parse {date} as it wasnt in format "Day Mon DD HH:MM:SS YYYY +-ZZZZ"`)
	end
end

I only use this function once in my code, and it doesn't get inlined as its deemed too expensive even though it should always be inlined because its only used once.

gaymeowing avatar Aug 23 '24 20:08 gaymeowing