Makes json writer use 90% less string tree operations

"[thing1][thing2][thing3]" and text() both compile to use only 1 string tree operation regardless of the number of args.

list2text abuses this to write 128 items of the list in 1 go, and it also handles the separator in the proper way, so no reason to not use it.

this also cuts 1 string tree operation out of every associative list write as well as 1 out of the writing of every item in that list.

So now the string tree O notation should be O(n log2(M)) rather then O(N^M) (ish)
This commit is contained in:
Kyle Spier-Swenson
2015-11-30 22:45:31 -08:00
parent 2713ceb0d5
commit 03f29f3b99
+7 -15
View File
@@ -2,15 +2,12 @@ json_writer/var/use_cache = 0
json_writer/proc/WriteObject(list/L)
if(use_cache && L["__json_cache"])
return L["__json_cache"]
. = "{"
var/i = 1
. = ""
for(var/k in L)
var/val = L[k]
. += {"\"[k]\":[write(val)]"}
if(i++ < L.len)
. += ","
. += "}"
. += text("\"[]\":[][]", k, write(L[k]), (i++ < L.len ? "," : ""))
. = text([][][], "{", ., "}")
json_writer/proc/write(val)
if(isnum(val))
@@ -26,12 +23,7 @@ json_writer/proc/write(val)
. += write_string("[val]")
json_writer/proc/write_array(list/L)
. = "\["
for(var/i = 1 to L.len)
. += write(L[i])
if(i < L.len)
. += ","
. += "]"
return "\[[list2text(L, ",")]]"
json_writer/proc/write_string(txt)
var/static/list/json_escape = list("\\" = "\\\\", "\"" = "\\\"", "\n" = "\\n")
@@ -51,4 +43,4 @@ json_writer/proc/is_associative(list/L)
for(var/key in L)
// if the key is a list that means it's actually an array of lists (stupid Byond...)
if(!isnum(key) && !isnull(L[key]) && !istype(key, /list))
return TRUE
return TRUE