From: Brendan Hansen Date: Mon, 24 Aug 2020 21:28:16 +0000 (-0500) Subject: performance improvements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=0d6aee301ca1fb933dd652d37710c3761a330235;p=onyx.git performance improvements --- diff --git a/core/string.onyx b/core/string.onyx index ebfbfaaa..1743e19f 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -51,12 +51,7 @@ string_concat :: proc (a: Allocator, s1: string, s2: string) -> string { string_free :: proc (a: Allocator, s: string) do free(a, s.data); -StringSplitResult :: struct { - tokens: ^string; - count: u32; -} - -string_split :: proc (a: Allocator, str: string, delim: u8) -> StringSplitResult { +string_split :: proc (a: Allocator, str: string, delim: u8) -> []string { delim_count := 0; for i: 0, str.count do if str.data[i] == delim do delim_count += 1; @@ -75,7 +70,7 @@ string_split :: proc (a: Allocator, str: string, delim: u8) -> StringSplitResult strarr[curr_str] = str.data[begin : str.count]; - return StringSplitResult.{ strarr, delim_count + 1 }; + return strarr[0 : delim_count + 1]; } string_substr :: proc (str: string, sub: string) -> string { @@ -193,7 +188,7 @@ string_builder_add_u64 :: proc (use sb: ^StringBuilder, n: u64) -> ^StringBuilde string_builder_add_u64_with_base :: proc (use sb: ^StringBuilder, n: u64, base: u64) -> ^StringBuilder { buf : [256] u8; - s := u64_to_string(n, 10l, Buffer.{ cast(^u8) buf, 256 }); + s := u64_to_string(n, base, Buffer.{ cast(^u8) buf, 256 }); return string_builder_add_string(sb, s); } diff --git a/core/wasi.onyx b/core/wasi.onyx index c146415e..027dbf6e 100644 --- a/core/wasi.onyx +++ b/core/wasi.onyx @@ -475,5 +475,5 @@ proc #export "_start" { args_get(argv, argv_buf); - main.main(argc, argv); + main.main(argv[0 : argc]); } diff --git a/misc/onyx.vim b/misc/onyx.vim index 73eb5d1b..13337566 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -29,9 +29,11 @@ syn keyword onyxConstant true false null syn keyword onyxCommentStart contained TODO NOTE BUG HACK -syn region onyxComment start="//" end="$" keepend contains=onyxCommentStart +syn region onyxComment start="//" end="$" keepend contains=onyxCommentStart -syn region onyxDirective start="#" end=" " keepend +syn region onyxDirective start="#" end=" " keepend + +syn region onyxString display start=+"+ skip=+\\\\\|\\"+ end=+"+ keepend hi def link onyxKeyword Statement hi def link onyxType Type @@ -39,6 +41,7 @@ hi def link onyxComment Comment hi def link onyxCommentStart Todo hi def link onyxConstant Constant hi def link onyxDirective Constant +hi def link onyxString String let b:current_syntax = "onyx" let &cpo = s:cpo_save diff --git a/onyx b/onyx index 429da335..8d63f147 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/wasi_test.onyx b/progs/wasi_test.onyx index 6743d1b4..ec764c00 100644 --- a/progs/wasi_test.onyx +++ b/progs/wasi_test.onyx @@ -202,7 +202,12 @@ print_arr :: proc (sb: ^StringBuilder, arr: []i32) { sb |> sba("\n") |> string_builder_to_string() |> print(); } -main :: proc (argc: u32, argv: ^cstring) { +make_i32_arr :: proc (a: Allocator, len: u32) -> [] i32 { + arr := cast(^i32) alloc(a, sizeof i32 * len); + return arr[0 : len]; +} + +main :: proc (args: []cstring) { sb := string_builder_make(heap_allocator, 256); timer := timer_start(); @@ -216,16 +221,16 @@ main :: proc (argc: u32, argv: ^cstring) { } ^sb |> sba("There are ") - |> sba(cast(u64) argc) + |> sba(cast(u64) args.count) |> sba(" arguments.\n"); - for i: 0, argc do ^sb |> sba(argv[i]) |> sba(" "); + for i: 0, args.count do ^sb |> sba(args.data[i]) |> sba(" "); ^sb |> sba("\n") |> string_builder_to_string() |> print(); fd: FileDescriptor = -1; - if err := path_open(3, LookupFlags.SymLinkFollow, string_make(argv[1]), cast(OFlags) 0, Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile, Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile, FDFlags.Sync, ^fd); err != Errno.Success { + if err := path_open(3, LookupFlags.SymLinkFollow, string_make(args.data[1]), cast(OFlags) 0, Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile, Rights.DataSync | Rights.Write | Rights.Read | Rights.Tell | Rights.Seek | Rights.Advise | Rights.PathOpen | Rights.PathCreateFile, FDFlags.Sync, ^fd); err != Errno.Success { print("Failed to open file\n"); print("Error code: "); print_u64_with_base(cast(u64) err, 16l); @@ -251,13 +256,12 @@ main :: proc (argc: u32, argv: ^cstring) { print_u64_with_base(sum, 10l); print("\n"); - matches := string_split(heap_allocator, "This is a test string to test splitting. It surprisingly works very well.", #char " "); - defer free(heap_allocator, matches.tokens); + defer free(heap_allocator, matches.data); string_builder_clear(^sb); for i: 0, matches.count { - ^sb |> sba(matches.tokens[i]) + ^sb |> sba(matches.data[i]) |> sba("\n"); } @@ -265,11 +269,11 @@ main :: proc (argc: u32, argv: ^cstring) { program := "+ + * s - /"; tokens := string_split(heap_allocator, program, #char " "); - defer free(heap_allocator, tokens.tokens); + defer free(heap_allocator, tokens.data); acc := 0; for i: 0, tokens.count { - tok :: tokens.tokens[i].data[0]; + tok :: tokens.data[i].data[0]; if tok == #char "+" do acc += 1; elseif tok == #char "-" do acc -= 1; @@ -308,7 +312,7 @@ main :: proc (argc: u32, argv: ^cstring) { ^sb |> string_builder_clear(); while i := 0; i < 10 { ^sb |> sba("Loop: ") |> sba(cast(u64) i) |> sba("\n"); - + i += 1; } else { print("Never ran the loop\n"); @@ -320,11 +324,9 @@ main :: proc (argc: u32, argv: ^cstring) { arr : [128] i32; for i: 0, 128 do arr[i] = i * i; - slice := arr[14 : 20]; - - print_arr(^sb, slice); + print_arr(^sb, arr[5 : 10]); - ss := string_substr("Hello, WoWorld!", "World"); + ss := string_substr("Hello, World!", "World"); if ss.count > 0 { print(ss); } diff --git a/src/onyxlex.c b/src/onyxlex.c index 5439dda5..e826ce6b 100644 --- a/src/onyxlex.c +++ b/src/onyxlex.c @@ -122,8 +122,22 @@ OnyxToken* onyx_get_token(OnyxTokenizer* tokenizer) { OnyxToken tk; // Skip whitespace - while (char_is_whitespace(*tokenizer->curr) && tokenizer->curr != tokenizer->end) - INCREMENT_CURR_TOKEN(tokenizer) + while (1) { + if (tokenizer->curr == tokenizer->end) break; + + switch (*tokenizer->curr) { + case ' ': + case '\n': + case '\t': + case '\r': + INCREMENT_CURR_TOKEN(tokenizer); + break; + default: + goto whitespace_skipped; + } + } + +whitespace_skipped: tk.type = Token_Type_Unknown; tk.text = tokenizer->curr; @@ -367,7 +381,7 @@ OnyxTokenizer onyx_tokenizer_create(bh_allocator allocator, bh_file_contents *fc .tokens = NULL, }; - bh_arr_new(allocator, tknizer.tokens, 512); + bh_arr_new(allocator, tknizer.tokens, 1 << 16); return tknizer; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 57978683..89025298 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -1516,8 +1516,10 @@ COMPILE_FUNC(expression, AstTyped* expr) { compile_expression(mod, &code, sl->lo); WIL(WI_LOCAL_TEE, tmp_local); - WID(WI_I32_CONST, sl->elem_size); - WI(WI_I32_MUL); + if (sl->elem_size != 1) { + WID(WI_I32_CONST, sl->elem_size); + WI(WI_I32_MUL); + } compile_location(mod, &code, sl->addr); WI(WI_I32_ADD); compile_expression(mod, &code, sl->hi);