Skip to content

Commit

Permalink
builtin,strconv: replace the simple internal string interpolation usa…
Browse files Browse the repository at this point in the history
…ges, to speed up compilation of small programs with -prod
  • Loading branch information
spytheman committed Sep 30, 2023
1 parent dc54a73 commit ccca8a5
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 83 deletions.
30 changes: 19 additions & 11 deletions vlib/builtin/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ fn (mut a array) ensure_cap(required int) {
return
}
if a.flags.has(.nogrow) {
panic('array.ensure_cap: array with the flag `.nogrow` cannot grow in size, array required new size: ${required}')
panic(
'array.ensure_cap: array with the flag `.nogrow` cannot grow in size, array required new size: ' +
required.str())
}
mut cap := if a.cap > 0 { a.cap } else { 2 }
for required > cap {
Expand Down Expand Up @@ -218,7 +220,7 @@ pub fn (a array) repeat(count int) array {
[direct_array_access; unsafe]
pub fn (a array) repeat_to_depth(count int, depth int) array {
if count < 0 {
panic('array.repeat: count is negative: ${count}')
panic('array.repeat: count is negative: ' + count.str())
}
mut size := u64(count) * u64(a.len) * u64(a.element_size)
if size == 0 {
Expand Down Expand Up @@ -272,7 +274,8 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
pub fn (mut a array) insert(i int, val voidptr) {
$if !no_bounds_checking {
if i < 0 || i > a.len {
panic('array.insert: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.insert: index out of range (i == ' + i.str() + ', a.len == ' +
a.len.str() + ')')
}
}
if a.len >= a.cap {
Expand All @@ -291,7 +294,8 @@ pub fn (mut a array) insert(i int, val voidptr) {
fn (mut a array) insert_many(i int, val voidptr, size int) {
$if !no_bounds_checking {
if i < 0 || i > a.len {
panic('array.insert_many: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.insert_many: index out of range (i == ' + i.str() + ', a.len == ' +
a.len.str() + ')')
}
}
a.ensure_cap(a.len + size)
Expand Down Expand Up @@ -350,8 +354,9 @@ pub fn (mut a array) delete(i int) {
pub fn (mut a array) delete_many(i int, size int) {
$if !no_bounds_checking {
if i < 0 || i + size > a.len {
endidx := if size > 1 { '..${i + size}' } else { '' }
panic('array.delete: index out of range (i == ${i}${endidx}, a.len == ${a.len})')
endidx := if size > 1 { '..' + (i + size).str() } else { '' }
panic('array.delete: index out of range (i == ' + i.str() + endidx + ', a.len == ' +
a.len.str() + ')')
}
}
if a.flags.all(.noshrink | .noslices) {
Expand Down Expand Up @@ -432,7 +437,8 @@ fn (a array) get_unsafe(i int) voidptr {
fn (a array) get(i int) voidptr {
$if !no_bounds_checking {
if i < 0 || i >= a.len {
panic('array.get: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.get: index out of range (i == ' + i.str() + ', a.len == ' + a.len.str() +
')')
}
}
unsafe {
Expand Down Expand Up @@ -533,13 +539,14 @@ fn (a array) slice(start int, _end int) array {
end := if _end == 2147483647 { a.len } else { _end } // max_int
$if !no_bounds_checking {
if start > end {
panic('array.slice: invalid slice index (${start} > ${end})')
panic('array.slice: invalid slice index (' + start.str() + ' > ' + end.str() + ')')
}
if end > a.len {
panic('array.slice: slice bounds out of range (${end} >= ${a.len})')
panic('array.slice: slice bounds out of range (' + end.str() + ' >= ' + a.len.str() +
')')
}
if start < 0 {
panic('array.slice: slice bounds out of range (${start} < 0)')
panic('array.slice: slice bounds out of range (' + start.str() + ' < 0)')
}
}
// TODO: integrate reference counting
Expand Down Expand Up @@ -659,7 +666,8 @@ fn (mut a array) set_unsafe(i int, val voidptr) {
fn (mut a array) set(i int, val voidptr) {
$if !no_bounds_checking {
if i < 0 || i >= a.len {
panic('array.set: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.set: index out of range (i == ' + i.str() + ', a.len == ' + a.len.str() +
')')
}
}
unsafe { vmemcpy(&u8(a.data) + u64(a.element_size) * u64(i), val, a.element_size) }
Expand Down
12 changes: 8 additions & 4 deletions vlib/builtin/array_d_gcboehm_opt.v
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ fn (mut a array) ensure_cap_noscan(required int) {
return
}
if a.flags.has(.nogrow) {
panic('array.ensure_cap_noscan: array with the flag `.nogrow` cannot grow in size, array required new size: ${required}')
panic(
'array.ensure_cap_noscan: array with the flag `.nogrow` cannot grow in size, array required new size: ' +
required.str())
}
mut cap := if a.cap > 0 { a.cap } else { 2 }
for required > cap {
Expand All @@ -118,7 +120,7 @@ fn (mut a array) ensure_cap_noscan(required int) {
[unsafe]
fn (a array) repeat_to_depth_noscan(count int, depth int) array {
if count < 0 {
panic('array.repeat: count is negative: ${count}')
panic('array.repeat: count is negative: ' + count.str())
}
mut size := u64(count) * u64(a.len) * u64(a.element_size)
if size == 0 {
Expand Down Expand Up @@ -153,7 +155,8 @@ fn (a array) repeat_to_depth_noscan(count int, depth int) array {
fn (mut a array) insert_noscan(i int, val voidptr) {
$if !no_bounds_checking {
if i < 0 || i > a.len {
panic('array.insert: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.insert: index out of range (i == ' + i.str() + ', a.len == ' +
a.len.str() + ')')
}
}
a.ensure_cap_noscan(a.len + 1)
Expand All @@ -169,7 +172,8 @@ fn (mut a array) insert_noscan(i int, val voidptr) {
fn (mut a array) insert_many_noscan(i int, val voidptr, size int) {
$if !no_bounds_checking {
if i < 0 || i > a.len {
panic('array.insert_many: index out of range (i == ${i}, a.len == ${a.len})')
panic('array.insert_many: index out of range (i == ' + i.str() + ', a.len == ' +
a.len.str() + ')')
}
}
a.ensure_cap_noscan(a.len + size)
Expand Down
10 changes: 7 additions & 3 deletions vlib/builtin/backtraces_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fn print_backtrace_skipping_top_frames(xskipframes int) bool {
} $else $if linux {
return print_backtrace_skipping_top_frames_linux(skipframes)
} $else {
println('print_backtrace_skipping_top_frames is not implemented. skipframes: ${skipframes}')
println('print_backtrace_skipping_top_frames is not implemented. skipframes: ' +
skipframes.str())
}
}
return false
Expand Down Expand Up @@ -71,7 +72,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
executable := sframe.all_before('(')
addr := sframe.all_after('[').all_before(']')
beforeaddr := sframe.all_before('[')
cmd := 'addr2line -e ${executable} ${addr}'
cmd := 'addr2line -e ' + executable + ' ' + addr
// taken from os, to avoid depending on the os module inside builtin.v
f := C.popen(&char(cmd.str), c'r')
if f == unsafe { nil } {
Expand All @@ -98,7 +99,10 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
// Note: it is shortened here to just d. , just so that it fits, and so
// that the common error file:lineno: line format is enforced.
output = output.replace(' (discriminator', ': (d.')
eprintln('${output:-55s} | ${addr:14s} | ${beforeaddr}')
// eprintln('${output:-55s} | ${addr:14s} | ${beforeaddr}')
eprintln(output)
eprintln(addr)
eprintln(beforeaddr)
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions vlib/builtin/builtin.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
bare_panic(s)
} $else {
eprintln('================ V panic ================')
eprintln(' module: ${mod}')
eprintln(' function: ${fn_name}()')
eprintln(' message: ${s}')
eprintln(' file: ${file}:${line_no}')
eprintln(' v hash: ${vcommithash()}')
eprintln(' module: ' + mod)
eprintln(' function: ' + fn_name + '()')
eprintln(' message: ' + s)
eprintln(' file: ' + file + ':' + line_no.str())
eprintln(' v hash: ' + vcommithash())
eprintln('=========================================')
$if exit_after_panic_message ? {
C.exit(1)
Expand Down Expand Up @@ -87,14 +87,14 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
// It ends the program with a panic.
[noreturn]
pub fn panic_option_not_set(s string) {
panic('option not set (${s})')
panic('option not set (' + s + ')')
}

// panic_result_not_set is called by V, when you use result error propagation in your main function
// It ends the program with a panic.
[noreturn]
pub fn panic_result_not_set(s string) {
panic('result not set (${s})')
panic('result not set (' + s + ')')
}

// panic prints a nice error message, then exits the process with exit code of 1.
Expand All @@ -106,7 +106,7 @@ pub fn panic(s string) {
} $else {
eprint('V panic: ')
eprintln(s)
eprintln('v hash: ${vcommithash()}')
eprintln('v hash: ' + vcommithash())
$if exit_after_panic_message ? {
C.exit(1)
} $else $if no_backtrace ? {
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn panic(s string) {
pub fn c_error_number_str(errnum int) string {
mut err_msg := ''
$if freestanding {
err_msg = 'error ${errnum}'
err_msg = 'error ' + errnum.str()
} $else {
$if !vinix {
c_msg := C.strerror(errnum)
Expand Down Expand Up @@ -323,7 +323,7 @@ pub fn malloc(n isize) &u8 {
// print_backtrace()
}
if n < 0 {
panic('malloc(${n} < 0)')
panic('malloc(' + n.str() + ' < 0)')
}
$if vplayground ? {
if n > 10000 {
Expand All @@ -348,7 +348,7 @@ pub fn malloc(n isize) &u8 {
res = unsafe { C.malloc(n) }
}
if res == 0 {
panic('malloc(${n}) failed')
panic('malloc(' + n.str() + ') failed')
}
$if debug_malloc ? {
// Fill in the memory with something != 0 i.e. `M`, so it is easier to spot
Expand All @@ -366,7 +366,7 @@ pub fn malloc_noscan(n isize) &u8 {
// print_backtrace()
}
if n < 0 {
panic('malloc_noscan(${n} < 0)')
panic('malloc_noscan(' + n.str() + ' < 0)')
}
$if vplayground ? {
if n > 10000 {
Expand Down Expand Up @@ -395,7 +395,7 @@ pub fn malloc_noscan(n isize) &u8 {
res = unsafe { C.malloc(n) }
}
if res == 0 {
panic('malloc_noscan(${n}) failed')
panic('malloc_noscan(' + n.str() + ') failed')
}
$if debug_malloc ? {
// Fill in the memory with something != 0 i.e. `M`, so it is easier to spot
Expand Down Expand Up @@ -425,7 +425,7 @@ pub fn malloc_uncollectable(n isize) &u8 {
// print_backtrace()
}
if n < 0 {
panic('malloc_uncollectable(${n} < 0)')
panic('malloc_uncollectable(' + n.str() + ' < 0)')
}
$if vplayground ? {
if n > 10000 {
Expand All @@ -448,7 +448,7 @@ pub fn malloc_uncollectable(n isize) &u8 {
res = unsafe { C.malloc(n) }
}
if res == 0 {
panic('malloc_uncollectable(${n}) failed')
panic('malloc_uncollectable(' + n.str() + ') failed')
}
$if debug_malloc ? {
// Fill in the memory with something != 0 i.e. `M`, so it is easier to spot
Expand Down Expand Up @@ -480,7 +480,7 @@ pub fn v_realloc(b &u8, n isize) &u8 {
new_ptr = unsafe { C.realloc(b, n) }
}
if new_ptr == 0 {
panic('realloc(${n}) failed')
panic('realloc(' + n.str() + ') failed')
}
return new_ptr
}
Expand Down Expand Up @@ -526,7 +526,8 @@ pub fn realloc_data(old_data &u8, old_size int, new_size int) &u8 {
nptr = unsafe { C.realloc(old_data, new_size) }
}
if nptr == 0 {
panic('realloc_data(${old_data}, ${old_size}, ${new_size}) failed')
panic('realloc_data(' + old_data.str() + ', ' + old_size.str() + ', ' + new_size.str() +
') failed')
}
return nptr
}
Expand All @@ -540,7 +541,7 @@ pub fn vcalloc(n isize) &u8 {
C.fprintf(C.stderr, c'vcalloc %6d total %10d\n', n, total_m)
}
if n < 0 {
panic('calloc(${n} < 0)')
panic('calloc(' + n.str() + ' < 0)')
} else if n == 0 {
return &u8(0)
}
Expand Down Expand Up @@ -569,7 +570,7 @@ pub fn vcalloc_noscan(n isize) &u8 {
}
}
if n < 0 {
panic('calloc_noscan(${n} < 0)')
panic('calloc_noscan(' + n.str() + ' < 0)')
}
return $if gcboehm_opt ? {
unsafe { &u8(C.memset(C.GC_MALLOC_ATOMIC(n), 0, n)) }
Expand Down Expand Up @@ -683,8 +684,7 @@ pub fn gc_memory_use() usize {
fn v_fixed_index(i int, len int) int {
$if !no_bounds_checking {
if i < 0 || i >= len {
s := 'fixed array index out of range (index: ${i}, len: ${len})'
panic(s)
panic('fixed array index out of range (index: ' + i.str() + ', len: ' + len.str() + ')')
}
}
return i
Expand Down
12 changes: 6 additions & 6 deletions vlib/builtin/builtin.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr {
expected_name = x.tname.clone()
}
}
panic('as cast: cannot cast `${obj_name}` to `${expected_name}`')
panic('as cast: cannot cast `' + obj_name + '` to `' + expected_name + '`')
}
return obj
}
Expand Down Expand Up @@ -76,17 +76,17 @@ pub fn (ami &VAssertMetaInfo) free() {
}

fn __print_assert_failure(i &VAssertMetaInfo) {
eprintln('${i.fpath}:${i.line_nr + 1}: FAIL: fn ${i.fn_name}: assert ${i.src}')
eprintln(i.fpath + ':' + (i.line_nr + 1).str() + ': FAIL: fn ' + i.fn_name + ': assert ' + i.src)
if i.op.len > 0 && i.op != 'call' {
eprintln(' left value: ${i.llabel} = ${i.lvalue}')
eprintln(' left value: ' + i.llabel + ' = ' + i.lvalue)
if i.rlabel == i.rvalue {
eprintln(' right value: ${i.rlabel}')
eprintln(' right value: ' + i.rlabel)
} else {
eprintln(' right value: ${i.rlabel} = ${i.rvalue}')
eprintln(' right value: ' + i.rlabel + ' = ' + i.rvalue)
}
}
if i.has_msg {
eprintln(' message: ${i.message}')
eprintln(' message: ' + i.message)
}
}

Expand Down
3 changes: 2 additions & 1 deletion vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type byte = u8
type i32 = int

// ptr_str returns the address of `ptr` as a `string`.
[markused]
pub fn ptr_str(ptr voidptr) string {
buf1 := u64(ptr).hex()
return buf1
Expand Down Expand Up @@ -570,7 +571,7 @@ pub fn (b []u8) byterune() !rune {
// repeat returns a new string with `count` number of copies of the byte it was called on.
pub fn (b u8) repeat(count int) string {
if count < 0 {
panic('byte.repeat: count is negative: ${count}')
panic('byte.repeat: count is negative: ' + count.str())
} else if count == 0 {
return ''
} else if count == 1 {
Expand Down
8 changes: 4 additions & 4 deletions vlib/builtin/option.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn (err IError) str() string {
// TODO: remove once deprecation period for `IError` methods has ended
// old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between
// <<
'${err.type_name()}: ${err.msg()}'
err.type_name() + ': ' + err.msg()
}
}
}
Expand All @@ -56,7 +56,7 @@ pub:
// msg returns the message of MessageError
pub fn (err MessageError) msg() string {
if err.code > 0 {
return '${err.msg}; code: ${err.code}'
return err.msg + '; code: ' + err.code.str()
}
return err.msg
}
Expand All @@ -83,7 +83,7 @@ fn (_ None__) str() string {

[if trace_error ?]
fn trace_error(x string) {
eprintln('> ${@FN} | ${x}')
eprintln(@FN + ' | ' + x)
}

// error returns a default error instance containing the error given in `message`.
Expand All @@ -100,7 +100,7 @@ pub fn error(message string) IError {
// Example: if ouch { return error_with_code('an error occurred', 1) }
[inline]
pub fn error_with_code(message string, code int) IError {
trace_error('${message} | code: ${code}')
trace_error(message + ' | code: ' + code.str())
return &MessageError{
msg: message
code: code
Expand Down
Loading

0 comments on commit ccca8a5

Please sign in to comment.