authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-31 01:31:23-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-31 01:31:23-05:00
log76fa6cdce36671bd9ad54248d77a3941f2eb3e34
tree85595ca0bb300121c12544cb401eac4f43054ff0
parent29bb175f4f9018bfb0e17686c05165527530de25

eradicate use of zeroes in std


5 files changed, 16 insertions(+), 35 deletions(-)

src/all_types.hpp+1-1
......@@ -1382,7 +1382,7 @@ enum AtomicOrder {
13821382// A basic block contains no branching. Branches send control flow
13831383// to another basic block.
13841384// Phi instructions must be first in a basic block.
1385// The last instruction in a basic block must be an expression of type unreachable.
1385// The last instruction in a basic block must be of type unreachable.
13861386struct IrBasicBlock {
13871387 ZigList<IrInstruction *> instruction_list;
13881388 IrBasicBlock *other;
src/ir.cpp+6-5
......@@ -2741,10 +2741,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
27412741static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
27422742 bool gen_error_defers, bool gen_maybe_defers)
27432743{
2744 while (inner_scope != outer_scope) {
2745 assert(inner_scope);
2746 if (inner_scope->id == ScopeIdDefer) {
2747 AstNode *defer_node = inner_scope->source_node;
2744 Scope *scope = inner_scope;
2745 while (scope != outer_scope) {
2746 assert(scope);
2747 if (scope->id == ScopeIdDefer) {
2748 AstNode *defer_node = scope->source_node;
27482749 assert(defer_node->type == NodeTypeDefer);
27492750 ReturnKind defer_kind = defer_node->data.defer.kind;
27502751 if (defer_kind == ReturnKindUnconditional ||
......@@ -2756,7 +2757,7 @@ static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
27562757 }
27572758
27582759 }
2759 inner_scope = inner_scope->parent;
2760 scope = scope->parent;
27602761 }
27612762}
27622763
std/hash_map.zig+7-27
......@@ -9,22 +9,12 @@ const debug_u32 = if (want_modification_safety) u32 else void;
99
1010pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,
1111 inline eql: fn(a: K, b: K)->bool) -> type
12{
13 SmallHashMap(K, V, hash, eql, @sizeOf(usize))
14}
15
16pub fn SmallHashMap(inline K: type, inline V: type,
17 inline hash: fn(key: K)->u32, inline eql: fn(a: K, b: K)->bool,
18 inline static_size: usize) -> type
1912{
2013 struct {
2114 entries: []Entry,
2215 size: usize,
2316 max_distance_from_start_index: usize,
2417 allocator: &Allocator,
25 // if the hash map is small enough, we use linear search through these
26 // entries instead of allocating memory
27 prealloc_entries: [static_size]Entry,
2818 // this is used to detect bugs where a hashtable is edited while an iterator is running.
2919 modification_count: debug_u32,
3020
......@@ -64,18 +54,16 @@ pub fn SmallHashMap(inline K: type, inline V: type,
6454 };
6555
6656 pub fn init(hm: &Self, allocator: &Allocator) {
67 hm.entries = hm.prealloc_entries[0...];
57 hm.entries = []Entry{};
6858 hm.allocator = allocator;
6959 hm.size = 0;
7060 hm.max_distance_from_start_index = 0;
71 hm.prealloc_entries = zeroes; // sets used to false for all entries
72 hm.modification_count = zeroes;
61 // it doesn't actually matter what we set this to since we use wrapping integer arithmetic
62 hm.modification_count = undefined;
7363 }
7464
7565 pub fn deinit(hm: &Self) {
76 if (hm.entries.ptr != &hm.prealloc_entries[0]) {
77 hm.allocator.free(Entry, hm.entries);
78 }
66 hm.allocator.free(Entry, hm.entries);
7967 }
8068
8169 pub fn clear(hm: &Self) {
......@@ -90,14 +78,8 @@ pub fn SmallHashMap(inline K: type, inline V: type,
9078 pub fn put(hm: &Self, key: K, value: V) -> %void {
9179 hm.incrementModificationCount();
9280
93 const resize = if (hm.entries.ptr == &hm.prealloc_entries[0]) {
94 // preallocated entries table is full
95 hm.size == hm.entries.len
96 } else {
97 // if we get too full (60%), double the capacity
98 hm.size * 5 >= hm.entries.len * 3
99 };
100 if (resize) {
81 // if we get too full (60%), double the capacity
82 if (hm.size * 5 >= hm.entries.len * 3) {
10183 const old_entries = hm.entries;
10284 %return hm.initCapacity(hm.entries.len * 2);
10385 // dump all of the old elements into the new table
......@@ -106,9 +88,7 @@ pub fn SmallHashMap(inline K: type, inline V: type,
10688 hm.internalPut(old_entry.key, old_entry.value);
10789 }
10890 }
109 if (old_entries.ptr != &hm.prealloc_entries[0]) {
110 hm.allocator.free(Entry, old_entries);
111 }
91 hm.allocator.free(Entry, old_entries);
11292 }
11393
11494 hm.internalPut(key, value);
std/list.zig+1-1
......@@ -13,7 +13,7 @@ pub fn List(inline T: type) -> type{
1313
1414 pub fn init(allocator: &Allocator) -> Self {
1515 Self {
16 .items = zeroes,
16 .items = []T{},
1717 .len = 0,
1818 .allocator = allocator,
1919 }
std/mem.zig+1-1
......@@ -67,7 +67,7 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
6767}
6868
6969pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
70 var result: T = zeroes;
70 var result: T = undefined;
7171 const result_slice = ([]u8)((&result)[0...1]);
7272 const padding = @sizeOf(T) - buf.len;
7373