镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 7f46c2d

Browse files
committed
perf(intl): reuse static list and relative options (#606)
Project ListFormat type/style and RelativeTimeFormat style/numeric native enums through immutable static StringCells in resolvedOptions. Locale and numberingSystem remain dynamically owned and managed, and every fresh observable result object is still constructed in spec field order. Against frozen benchmark parent 338b472, the exact paired 6,400-operation invocation drops managed cells from 444,002 to 418,402 (-25,600), GC cell bytes from 62,566,912 to 60,928,512 (-1,638,400), and backing allocations from 811,213 to 785,613 (-25,600). Execution counters, Shape requests, and checksum 975333233 are identical. Twenty-one order-balanced pairs are wall-neutral: 317.699333 ms to 317.838250 ms (+0.044%), with 8/21 candidate wins. This commit claims allocation reduction only. Exact test262 remains ListFormat 81/81 and RelativeTimeFormat 80/80 before and after. Clean full unit rerun: 1,722 passed, 1 skipped, 0 failed, 0 leaked. All 11 enum spellings and dynamic ownership controls pass normally and under TSan; focused service, bounded, moving, parallel, and OOM groups pass.
1 parent 338b472 commit 7f46c2d

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎src/context.zig‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13286,6 +13286,42 @@ test "parallel_js: Intl.PluralRules category metadata is immutable" {
1328613286
try std.testing.expectEqual(@as(f64, 4), result.asNum());
1328713287
}
1328813288

13289+
test "Intl ListFormat and RelativeTimeFormat resolved enums use exact unmanaged static cells" {
13290+
const Case = struct { source: []const u8, expected: []const u8, cell: *const strcell.StringCell };
13291+
const cases = [_]Case{
13292+
.{ .source = "new Intl.ListFormat('en',{type:'conjunction'}).resolvedOptions().type", .expected = "conjunction", .cell = strcell.staticCell("conjunction") },
13293+
.{ .source = "new Intl.ListFormat('en',{type:'disjunction'}).resolvedOptions().type", .expected = "disjunction", .cell = strcell.staticCell("disjunction") },
13294+
.{ .source = "new Intl.ListFormat('en',{type:'unit'}).resolvedOptions().type", .expected = "unit", .cell = strcell.staticCell("unit") },
13295+
.{ .source = "new Intl.ListFormat('en',{style:'long'}).resolvedOptions().style", .expected = "long", .cell = strcell.staticCell("long") },
13296+
.{ .source = "new Intl.ListFormat('en',{style:'short'}).resolvedOptions().style", .expected = "short", .cell = strcell.staticCell("short") },
13297+
.{ .source = "new Intl.ListFormat('en',{style:'narrow'}).resolvedOptions().style", .expected = "narrow", .cell = strcell.staticCell("narrow") },
13298+
.{ .source = "new Intl.RelativeTimeFormat('en',{style:'long'}).resolvedOptions().style", .expected = "long", .cell = strcell.staticCell("long") },
13299+
.{ .source = "new Intl.RelativeTimeFormat('en',{style:'short'}).resolvedOptions().style", .expected = "short", .cell = strcell.staticCell("short") },
13300+
.{ .source = "new Intl.RelativeTimeFormat('en',{style:'narrow'}).resolvedOptions().style", .expected = "narrow", .cell = strcell.staticCell("narrow") },
13301+
.{ .source = "new Intl.RelativeTimeFormat('en',{numeric:'always'}).resolvedOptions().numeric", .expected = "always", .cell = strcell.staticCell("always") },
13302+
.{ .source = "new Intl.RelativeTimeFormat('en',{numeric:'auto'}).resolvedOptions().numeric", .expected = "auto", .cell = strcell.staticCell("auto") },
13303+
};
13304+
13305+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
13306+
.enable_gc = true,
13307+
.enable_jit = false,
13308+
});
13309+
defer ctx.destroy();
13310+
var last: Value = Value.undef();
13311+
for (cases) |case| {
13312+
last = try ctx.evaluate(case.source);
13313+
try std.testing.expectEqualStrings(case.expected, last.asStr());
13314+
try std.testing.expectEqual(case.cell, last.asStringCell());
13315+
try std.testing.expect(!last.asStringCell().isGcManaged());
13316+
}
13317+
const dynamic_locale = try ctx.evaluate("new Intl.ListFormat('de').resolvedOptions().locale");
13318+
const dynamic_numbering = try ctx.evaluate("new Intl.RelativeTimeFormat('en-u-nu-arab').resolvedOptions().numberingSystem");
13319+
try std.testing.expect(dynamic_locale.asStringCell().isGcManaged());
13320+
try std.testing.expect(dynamic_numbering.asStringCell().isGcManaged());
13321+
ctx.collectGarbage();
13322+
try std.testing.expectEqualStrings("auto", last.asStr());
13323+
}
13324+
1328913325
test "Intl structural part results are reclaimed under a bounded precise heap" {
1329013326
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
1329113327
.enable_gc = true,

‎src/interpreter.zig‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31334,13 +31334,13 @@ fn intlResolvedOptionsFn(comptime service: []const u8) value.NativeFn {
3133431334
try put(self, o, "timeStyle", data.time_style);
3133531335
} else if (comptime std.mem.eql(u8, service, "RelativeTimeFormat")) {
3133631336
const data = this.asObj().intlRelativeTimeFormatData().?;
31337-
try self.setProp(o, "style", try Value.strAlloc(self.arena, data.style.string()));
31338-
try self.setProp(o, "numeric", try Value.strAlloc(self.arena, data.numeric.string()));
31337+
try self.setProp(o, "style", data.style.value());
31338+
try self.setProp(o, "numeric", data.numeric.value());
3133931339
try self.setProp(o, "numberingSystem", try Value.strAlloc(self.arena, data.numbering_system));
3134031340
} else if (comptime std.mem.eql(u8, service, "ListFormat")) {
3134131341
const data = this.asObj().intlListFormatData().?;
31342-
try self.setProp(o, "type", try Value.strAlloc(self.arena, data.kind.string()));
31343-
try self.setProp(o, "style", try Value.strAlloc(self.arena, data.style.string()));
31342+
try self.setProp(o, "type", data.kind.value());
31343+
try self.setProp(o, "style", data.style.value());
3134431344
} else if (comptime std.mem.eql(u8, service, "DisplayNames")) {
3134531345
// Reflect the immutable normalized record in Table-6 order.
3134631346
const data = this.asObj().intlDisplayNamesData() orelse

‎src/value.zig‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,14 @@ pub const IntlRelativeTimeFormatData = struct {
15061506
pub fn string(self: Style) []const u8 {
15071507
return @tagName(self);
15081508
}
1509+
1510+
pub fn value(self: Style) Value {
1511+
return switch (self) {
1512+
.long => Value.str("long"),
1513+
.short => Value.str("short"),
1514+
.narrow => Value.str("narrow"),
1515+
};
1516+
}
15091517
};
15101518

15111519
pub const Numeric = enum(u8) {
@@ -1519,6 +1527,13 @@ pub const IntlRelativeTimeFormatData = struct {
15191527
pub fn string(self: Numeric) []const u8 {
15201528
return @tagName(self);
15211529
}
1530+
1531+
pub fn value(self: Numeric) Value {
1532+
return switch (self) {
1533+
.always => Value.str("always"),
1534+
.auto => Value.str("auto"),
1535+
};
1536+
}
15221537
};
15231538

15241539
locale: []const u8 = "en",
@@ -1554,6 +1569,14 @@ pub const IntlListFormatData = struct {
15541569
pub fn string(self: Kind) []const u8 {
15551570
return @tagName(self);
15561571
}
1572+
1573+
pub fn value(self: Kind) Value {
1574+
return switch (self) {
1575+
.conjunction => Value.str("conjunction"),
1576+
.disjunction => Value.str("disjunction"),
1577+
.unit => Value.str("unit"),
1578+
};
1579+
}
15571580
};
15581581

15591582
pub const Style = enum(u8) {
@@ -1570,6 +1593,14 @@ pub const IntlListFormatData = struct {
15701593
pub fn string(self: Style) []const u8 {
15711594
return @tagName(self);
15721595
}
1596+
1597+
pub fn value(self: Style) Value {
1598+
return switch (self) {
1599+
.long => Value.str("long"),
1600+
.short => Value.str("short"),
1601+
.narrow => Value.str("narrow"),
1602+
};
1603+
}
15731604
};
15741605

15751606
locale: []const u8 = "en",

0 commit comments

Comments
 (0)