Skip to content

Commit 2d1b9d6

Browse files
committed
Remove unnecessary cleanup when we know we have an arena
Change a few old alloc+memcpy to dupe Some other smaller cleanups.
1 parent e603a17 commit 2d1b9d6

File tree

5 files changed

+67
-153
lines changed

5 files changed

+67
-153
lines changed

src/browser/dom/html_collection.zig

Lines changed: 43 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const std = @import("std");
20+
const Allocator = std.mem.Allocator;
2021

2122
const parser = @import("../netsurf.zig");
2223

@@ -42,25 +43,11 @@ const Matcher = union(enum) {
4243

4344
pub fn match(self: Matcher, node: *parser.Node) !bool {
4445
switch (self) {
45-
inline .matchTrue => return true,
46-
inline .matchFalse => return false,
47-
inline .matchByTagName => |case| return case.match(node),
48-
inline .matchByClassName => |case| return case.match(node),
49-
inline .matchByName => |case| return case.match(node),
50-
inline .matchByLinks => return MatchByLinks.match(node),
51-
inline .matchByAnchors => return MatchByAnchors.match(node),
52-
}
53-
}
54-
55-
pub fn deinit(self: Matcher, alloc: std.mem.Allocator) void {
56-
switch (self) {
57-
inline .matchTrue => return,
58-
inline .matchFalse => return,
59-
inline .matchByTagName => |case| return case.deinit(alloc),
60-
inline .matchByClassName => |case| return case.deinit(alloc),
61-
inline .matchByName => |case| return case.deinit(alloc),
62-
inline .matchByLinks => return,
63-
inline .matchByAnchors => return,
46+
.matchTrue => return true,
47+
.matchFalse => return false,
48+
.matchByLinks => return MatchByLinks.match(node),
49+
.matchByAnchors => return MatchByAnchors.match(node),
50+
inline else => |m| return m.match(node),
6451
}
6552
}
6653
};
@@ -71,54 +58,49 @@ pub const MatchByTagName = struct {
7158
tag: []const u8,
7259
is_wildcard: bool,
7360

74-
fn init(alloc: std.mem.Allocator, tag_name: []const u8) !MatchByTagName {
75-
const tag_name_alloc = try alloc.alloc(u8, tag_name.len);
76-
@memcpy(tag_name_alloc, tag_name);
77-
return MatchByTagName{
78-
.tag = tag_name_alloc,
79-
.is_wildcard = std.mem.eql(u8, tag_name, "*"),
61+
fn init(arena: Allocator, tag_name: []const u8) !MatchByTagName {
62+
if (std.mem.eql(u8, tag_name, "*")) {
63+
return .{ .tag = "*", .is_wildcard = true };
64+
}
65+
66+
return .{
67+
.tag = try arena.dupe(u8, tag_name),
68+
.is_wildcard = false,
8069
};
8170
}
8271

8372
pub fn match(self: MatchByTagName, node: *parser.Node) !bool {
8473
return self.is_wildcard or std.ascii.eqlIgnoreCase(self.tag, try parser.nodeName(node));
8574
}
86-
87-
fn deinit(self: MatchByTagName, alloc: std.mem.Allocator) void {
88-
alloc.free(self.tag);
89-
}
9075
};
9176

9277
pub fn HTMLCollectionByTagName(
93-
alloc: std.mem.Allocator,
78+
arena: Allocator,
9479
root: ?*parser.Node,
9580
tag_name: []const u8,
9681
include_root: bool,
9782
) !HTMLCollection {
9883
return HTMLCollection{
9984
.root = root,
100-
.walker = Walker{ .walkerDepthFirst = .{} },
101-
.matcher = Matcher{
102-
.matchByTagName = try MatchByTagName.init(alloc, tag_name),
103-
},
85+
.walker = .{ .walkerDepthFirst = .{} },
86+
.matcher = .{ .matchByTagName = try MatchByTagName.init(arena, tag_name) },
10487
.include_root = include_root,
10588
};
10689
}
10790

10891
pub const MatchByClassName = struct {
109-
classNames: []const u8,
92+
class_names: []const u8,
11093

111-
fn init(alloc: std.mem.Allocator, classNames: []const u8) !MatchByClassName {
112-
const class_names_alloc = try alloc.alloc(u8, classNames.len);
113-
@memcpy(class_names_alloc, classNames);
114-
return MatchByClassName{
115-
.classNames = class_names_alloc,
94+
fn init(arena: Allocator, class_names: []const u8) !MatchByClassName {
95+
return .{
96+
.class_names = try arena.dupe(u8, class_names),
11697
};
11798
}
11899

119100
pub fn match(self: MatchByClassName, node: *parser.Node) !bool {
120-
var it = std.mem.splitAny(u8, self.classNames, " ");
121101
const e = parser.nodeToElement(node);
102+
103+
var it = std.mem.splitScalar(u8, self.class_names, ' ');
122104
while (it.next()) |c| {
123105
if (!try parser.elementHasClass(e, c)) {
124106
return false;
@@ -127,36 +109,28 @@ pub const MatchByClassName = struct {
127109

128110
return true;
129111
}
130-
131-
fn deinit(self: MatchByClassName, alloc: std.mem.Allocator) void {
132-
alloc.free(self.classNames);
133-
}
134112
};
135113

136114
pub fn HTMLCollectionByClassName(
137-
alloc: std.mem.Allocator,
115+
arena: Allocator,
138116
root: ?*parser.Node,
139117
classNames: []const u8,
140118
include_root: bool,
141119
) !HTMLCollection {
142120
return HTMLCollection{
143121
.root = root,
144-
.walker = Walker{ .walkerDepthFirst = .{} },
145-
.matcher = Matcher{
146-
.matchByClassName = try MatchByClassName.init(alloc, classNames),
147-
},
122+
.walker = .{ .walkerDepthFirst = .{} },
123+
.matcher = .{ .matchByClassName = try MatchByClassName.init(arena, classNames) },
148124
.include_root = include_root,
149125
};
150126
}
151127

152128
pub const MatchByName = struct {
153129
name: []const u8,
154130

155-
fn init(alloc: std.mem.Allocator, name: []const u8) !MatchByName {
156-
const names_alloc = try alloc.alloc(u8, name.len);
157-
@memcpy(names_alloc, name);
158-
return MatchByName{
159-
.name = names_alloc,
131+
fn init(arena: Allocator, name: []const u8) !MatchByName {
132+
return .{
133+
.name = try arena.dupe(u8, name),
160134
};
161135
}
162136

@@ -165,24 +139,18 @@ pub const MatchByName = struct {
165139
const nname = try parser.elementGetAttribute(e, "name") orelse return false;
166140
return std.mem.eql(u8, self.name, nname);
167141
}
168-
169-
fn deinit(self: MatchByName, alloc: std.mem.Allocator) void {
170-
alloc.free(self.name);
171-
}
172142
};
173143

174144
pub fn HTMLCollectionByName(
175-
alloc: std.mem.Allocator,
145+
arena: Allocator,
176146
root: ?*parser.Node,
177147
name: []const u8,
178148
include_root: bool,
179149
) !HTMLCollection {
180150
return HTMLCollection{
181151
.root = root,
182-
.walker = Walker{ .walkerDepthFirst = .{} },
183-
.matcher = Matcher{
184-
.matchByName = try MatchByName.init(alloc, name),
185-
},
152+
.walker = .{ .walkerDepthFirst = .{} },
153+
.matcher = .{ .matchByName = try MatchByName.init(arena, name) },
186154
.include_root = include_root,
187155
};
188156
}
@@ -193,8 +161,8 @@ pub fn HTMLCollectionAll(
193161
) !HTMLCollection {
194162
return HTMLCollection{
195163
.root = root,
196-
.walker = Walker{ .walkerDepthFirst = .{} },
197-
.matcher = Matcher{ .matchTrue = .{} },
164+
.walker = .{ .walkerDepthFirst = .{} },
165+
.matcher = .{ .matchTrue = .{} },
198166
.include_root = include_root,
199167
};
200168
}
@@ -205,17 +173,17 @@ pub fn HTMLCollectionChildren(
205173
) !HTMLCollection {
206174
return HTMLCollection{
207175
.root = root,
208-
.walker = Walker{ .walkerChildren = .{} },
209-
.matcher = Matcher{ .matchTrue = .{} },
176+
.walker = .{ .walkerChildren = .{} },
177+
.matcher = .{ .matchTrue = .{} },
210178
.include_root = include_root,
211179
};
212180
}
213181

214182
pub fn HTMLCollectionEmpty() !HTMLCollection {
215183
return HTMLCollection{
216184
.root = null,
217-
.walker = Walker{ .walkerNone = .{} },
218-
.matcher = Matcher{ .matchFalse = .{} },
185+
.walker = .{ .walkerNone = .{} },
186+
.matcher = .{ .matchFalse = .{} },
219187
.include_root = false,
220188
};
221189
}
@@ -240,10 +208,8 @@ pub fn HTMLCollectionByLinks(
240208
) !HTMLCollection {
241209
return HTMLCollection{
242210
.root = root,
243-
.walker = Walker{ .walkerDepthFirst = .{} },
244-
.matcher = Matcher{
245-
.matchByLinks = MatchByLinks{},
246-
},
211+
.walker = .{ .walkerDepthFirst = .{} },
212+
.matcher = .{ .matchByLinks = MatchByLinks{} },
247213
.include_root = include_root,
248214
};
249215
}
@@ -267,10 +233,8 @@ pub fn HTMLCollectionByAnchors(
267233
) !HTMLCollection {
268234
return HTMLCollection{
269235
.root = root,
270-
.walker = Walker{ .walkerDepthFirst = .{} },
271-
.matcher = Matcher{
272-
.matchByAnchors = MatchByAnchors{},
273-
},
236+
.walker = .{ .walkerDepthFirst = .{} },
237+
.matcher = .{ .matchByAnchors = MatchByAnchors{} },
274238
.include_root = include_root,
275239
};
276240
}
@@ -321,7 +285,7 @@ pub const HTMLCollection = struct {
321285
cur_node: ?*parser.Node = undefined,
322286

323287
// start returns the first node to walk on.
324-
fn start(self: HTMLCollection) !?*parser.Node {
288+
fn start(self: *const HTMLCollection) !?*parser.Node {
325289
if (self.root == null) return null;
326290

327291
if (self.include_root) {

src/browser/dom/mutation_observer.zig

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ pub const MutationObserver = struct {
4545
options: MutationObserverInit,
4646
};
4747

48-
const deinitFunc = struct {
49-
fn deinit(ctx: ?*anyopaque, allocator: std.mem.Allocator) void {
50-
const o: *Observer = @ptrCast(@alignCast(ctx));
51-
allocator.destroy(o);
52-
}
53-
}.deinit;
54-
5548
const Observers = std.ArrayListUnmanaged(*Observer);
5649

5750
pub const MutationObserverInit = struct {
@@ -104,15 +97,15 @@ pub const MutationObserver = struct {
10497
arena,
10598
"DOMNodeInserted",
10699
EventHandler,
107-
.{ .cbk = self.cbk, .ctx = o, .deinitFunc = deinitFunc },
100+
.{ .cbk = self.cbk, .ctx = o },
108101
false,
109102
);
110103
try parser.eventTargetAddEventListener(
111104
parser.toEventTarget(parser.Node, node),
112105
arena,
113106
"DOMNodeRemoved",
114107
EventHandler,
115-
.{ .cbk = self.cbk, .ctx = o, .deinitFunc = deinitFunc },
108+
.{ .cbk = self.cbk, .ctx = o },
116109
false,
117110
);
118111
}
@@ -122,7 +115,7 @@ pub const MutationObserver = struct {
122115
arena,
123116
"DOMAttrModified",
124117
EventHandler,
125-
.{ .cbk = self.cbk, .ctx = o, .deinitFunc = deinitFunc },
118+
.{ .cbk = self.cbk, .ctx = o },
126119
false,
127120
);
128121
}
@@ -132,7 +125,7 @@ pub const MutationObserver = struct {
132125
arena,
133126
"DOMCharacterDataModified",
134127
EventHandler,
135-
.{ .cbk = self.cbk, .ctx = o, .deinitFunc = deinitFunc },
128+
.{ .cbk = self.cbk, .ctx = o },
136129
false,
137130
);
138131
}
@@ -142,7 +135,7 @@ pub const MutationObserver = struct {
142135
arena,
143136
"DOMSubtreeModified",
144137
EventHandler,
145-
.{ .cbk = self.cbk, .ctx = o, .deinitFunc = deinitFunc },
138+
.{ .cbk = self.cbk, .ctx = o },
146139
false,
147140
);
148141
}
@@ -153,15 +146,6 @@ pub const MutationObserver = struct {
153146
// TODO unregister listeners.
154147
}
155148

156-
pub fn deinit(self: *MutationObserver, state: *SessionState) void {
157-
const arena = state.arena;
158-
// TODO unregister listeners.
159-
for (self.observers.items) |o| {
160-
arena.destroy(o);
161-
}
162-
self.observers.deinit(arena);
163-
}
164-
165149
// TODO
166150
pub fn _takeRecords(_: *const MutationObserver) ?[]const u8 {
167151
return &[_]u8{};

src/browser/url/url.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ pub const URL = struct {
5151
) !URL {
5252
const arena = state.arena;
5353
const raw = try std.mem.concat(arena, u8, &[_][]const u8{ url, base orelse "" });
54-
errdefer arena.free(raw);
5554

5655
const uri = std.Uri.parse(raw) catch return error.TypeError;
5756
return init(arena, uri);

0 commit comments

Comments
 (0)