This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ryetoc's profile picture

CPU usage and Computer Craft Rednet

Started by ryetoc, 04 May 2013 - 06:43 PM
ryetoc #1
Posted 04 May 2013 - 08:43 PM
I am using FTB (Direwolf20 Pack) and We use rednet quite a bit for remote control of resource points. However I have noticed large amounts of CPU usage. This seems to me related to rednet. If I delete the computers folder (so nothing boots) cpu usage drops to next to nothing. Profiling the jar shows a lot of CPU time spent in dan200 classes.

The question is two fold. First, what can you recommend from a listener standpoint, to reduce CPU usage. The listeners all listen like so:


function remoteControl()
  while true do
	--main listening for commands loop
	event, senderId, message, distance = os.pullEvent("rednet_message")
	if ryetoc.stringStarts(message, "CONTROL") then --filter out noise
	  values = ryetoc.split(message, "|")
	  if table.getn(values)== 3 then -- it's a control message
		if values[2] == name then --it's for me
		  term.setCursorPos(1, 5)
		  ryetoc.setColor(colors.orange, colors.black)
		  print("Received Message...")
		  running = ryetoc.fromString(values[3])
		end
	  end
	end
  end
end

ryetoc.from string return true if string == "1" else returns false. Simple. The stringStarts is:


function stringStarts(String , Start)
   return string.sub(String,1,string.len(Start))==Start
end

and ryetoc.split is: (this is something I found online)


function split(string, divider)
	local function find(string, match, startIndex)
		if not match then return nil end
		_ = startIndex or 1
		local _s = nil
		local _e = nil
		_len = match:len()
		while true do
			_t = string:sub( _ , _len + _ - 1)
			if _t == match then
				_s = _
				_e = _ + _len - 1
				break
			end
			_ = _ + 1
			if _ > string:len() then break end
		end
		if _s == nil then return nil else return _s, _e end
	end
	if not divider then return nil end
	local start = {}
	local endS = {}
	local n=1
	repeat
		if n==1 then
			start[n], endS[n] = find(string, divider)
		else
			start[n], endS[n] = find(string, divider, endS[n-1]+1)
		end
		n=n+1
	until start[n-1]==nil
	local subs = {}
	for n=1, #start+1 do
		if n==1 then
			subs[n] = string:sub(1, start[n]-1)
		elseif n==#start+1 then
			subs[n] = string:sub(endS[n-1]+1)
		else
			subs[n] = string:sub(endS[n-1]+1, start[n]-1)
		end
	end
	return subs
end

Any help would be appreciated. I would like to keep the listeners listening to all rednet, and not just specific machines.
Lyqyd #2
Posted 05 May 2013 - 02:30 AM
Split into new topic.

That listener is fine, but that is one horrendous split function. You could try this instead, since you're using single-character delimiters:


function split(str, marker)
  local subs = {}
  for match in string.gmatch(str, "[^"..marker.."]+") do
    table.insert(subs, match)
  end
  return subs
end
ryetoc #3
Posted 05 May 2013 - 03:51 PM
I will give that a try, I knew there was something easier, but I hate regex, lua-patterns are like another form of regex….

Guess that's what I get for being lazy.