Skip to content
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

Notably faster fromhex() #2055

Closed
nnposter opened this issue May 26, 2020 · 3 comments
Closed

Notably faster fromhex() #2055

nnposter opened this issue May 26, 2020 · 3 comments

Comments

@nnposter
Copy link

nnposter commented May 26, 2020

Function stdnse.fromhex() is used extensively throughout the NSE codebase, which makes it worth optimizing. The following drop-in replacement performs about 43% faster[1] and creates only a quarter of temporary Lua strings than the current implementation:

function fromhex (hex)
  local p = hex:find("[^%x%s]")
  if p then
    return nil, "Invalid hexadecimal digits at position " .. p
  end
  hex = hex:gsub("%s+", "")
  if #hex % 2 ~= 0 then
    return nil, "Odd number of hexadecimal digits"
  end
  return hex:gsub("..", function (h) return string.char(tonumber(h, 16)) end)
end

I will commit the change in a few weeks unless concerns are raised.

[1] https://gist.github.com/nnposter/b9e4e1a9b9d2637f8c8c6b2948866640

@dmiller-nmap
Copy link

I like it! Thanks for taking the time to work it out. I don't see any issues.

@nnposter
Copy link
Author

nnposter commented Jun 5, 2020

Committed as r37938 (f5b4d98)

@nnposter nnposter closed this as completed Jun 5, 2020
nnposter referenced this issue Jun 5, 2020
Over 40% faster and creates only a quarter of temporary Lua strings than
the legacy implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@nnposter @dmiller-nmap and others