mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 21:52:59 +00:00 
			
		
		
		
	Speed up JSON string parsing
We now use Lua patterns to find runs of characters. This makes string parsing 3-4x faster. Ish - I've not run any exact benchmarks. Closes #1408
This commit is contained in:
		| @@ -533,12 +533,18 @@ do | |||||||
|     local function parse_string(str, pos, terminate) |     local function parse_string(str, pos, terminate) | ||||||
|         local buf, n = {}, 1 |         local buf, n = {}, 1 | ||||||
|  |  | ||||||
|  |         -- We attempt to match all non-special characters at once using Lua patterns, as this | ||||||
|  |         -- provides a significant speed boost. This is all characters >= " " except \ and the | ||||||
|  |         -- terminator (' or "). | ||||||
|  |         local char_pat = "^[ !#-[%]^-\255]+" | ||||||
|  |         if terminate == "'" then char_pat = "^[ -&(-[%]^-\255]+" end | ||||||
|  |  | ||||||
|         while true do |         while true do | ||||||
|             local c = sub(str, pos, pos) |             local c = sub(str, pos, pos) | ||||||
|             if c == "" then error_at(pos, "Unexpected end of input, expected '\"'.") end |             if c == "" then error_at(pos, "Unexpected end of input, expected '\"'.") end | ||||||
|             if c == terminate then break end |             if c == terminate then break end | ||||||
|  |  | ||||||
|             if c == '\\' then |             if c == "\\" then | ||||||
|                 -- Handle the various escapes |                 -- Handle the various escapes | ||||||
|                 c = sub(str, pos + 1, pos + 1) |                 c = sub(str, pos + 1, pos + 1) | ||||||
|                 if c == "" then error_at(pos, "Unexpected end of input, expected escape sequence.") end |                 if c == "" then error_at(pos, "Unexpected end of input, expected escape sequence.") end | ||||||
| @@ -552,8 +558,10 @@ do | |||||||
|                     if not unesc then error_at(pos + 1, "Unknown escape character %q.", c) end |                     if not unesc then error_at(pos + 1, "Unknown escape character %q.", c) end | ||||||
|                     buf[n], n, pos = unesc, n + 1, pos + 2 |                     buf[n], n, pos = unesc, n + 1, pos + 2 | ||||||
|                 end |                 end | ||||||
|             elseif c >= '\x20' then |             elseif c >= " " then | ||||||
|                 buf[n], n, pos = c, n + 1, pos + 1 |                 local _, finish = find(str, char_pat, pos) | ||||||
|  |                 buf[n], n = sub(str, pos, finish), n + 1 | ||||||
|  |                 pos = finish + 1 | ||||||
|             else |             else | ||||||
|                 error_at(pos + 1, "Unescaped whitespace %q.", c) |                 error_at(pos + 1, "Unescaped whitespace %q.", c) | ||||||
|             end |             end | ||||||
|   | |||||||
| @@ -47,7 +47,7 @@ | |||||||
|     ], |     ], | ||||||
|     "depends": { |     "depends": { | ||||||
|         "fabricloader": ">=0.14.17", |         "fabricloader": ">=0.14.17", | ||||||
|         "fabric-api": ">=0.78.0", |         "fabric-api": ">=0.80.0", | ||||||
|         "minecraft": ">=1.19.4 <1.20" |         "minecraft": ">=1.19.4 <1.20" | ||||||
|     }, |     }, | ||||||
|     "accessWidener": "computercraft.accesswidener" |     "accessWidener": "computercraft.accesswidener" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates