diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 5f1adfeae9..569ba74a25 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -261,6 +261,17 @@ pub fn (mut a array) pop() voidptr { return memdup(last_elem, a.element_size) } +// array.delete_last efficiently deletes the last element of the array +pub fn (mut a array) delete_last() { + // copy pasting code for performance + $if !no_bounds_checking ? { + if a.len == 0 { + panic('array.pop: array is empty') + } + } + a.len-- +} + // array.slice returns an array using the same buffer as original array // but starting from the `start` element and ending with the element before // the `end` element of the original array with the length and capacity diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 0537f24ee7..f85392f5b3 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1047,7 +1047,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { g.writeln('// TypeDecl') } } - g.stmt_path_pos.pop() + g.stmt_path_pos.delete_last() } fn (mut g Gen) write_defer_stmts() { @@ -2773,8 +2773,8 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str g.match_sumtype_exprs << node.cond g.match_sumtype_syms << type_sym defer { - g.match_sumtype_exprs.pop() - g.match_sumtype_syms.pop() + g.match_sumtype_exprs.delete_last() + g.match_sumtype_syms.delete_last() } for j, branch in node.branches { mut sumtype_index := 0 @@ -4762,7 +4762,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table. if g.inside_ternary == 0 && !(expr_stmt.expr is ast.IfExpr) { g.writeln(';') } - g.stmt_path_pos.pop() + g.stmt_path_pos.delete_last() } else { g.stmt(stmt) }