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

Commit 10ac2f3

Browse files
committed
perf(intl): reuse static resolved field strings (#603)
Project the closed Intl.DateTimeFormat component and style enum directly through immutable static StringCells. The none state remains absent, while locale, calendar, numbering-system, time-zone, and every other dynamically owned string retain their existing allocation paths. Across 3,200 operations, exact managed-cell reductions match the emitted enum count: default -9,600, components -22,400, style -6,400, and locale -9,600. The component row drops GC cell bytes from 18,638,656 to 17,205,056 (-1,433,600) and backing allocations from 265,626 to 243,226 (-22,400), with identical execution counters, Shape requests, and checksum. In 21 order-balanced component-row pairs, median wall time improves from 81.766750 ms to 79.220792 ms (-3.114%) with 21/21 candidate wins. The other three rows range from -0.280% to +0.307%, so no broad throughput claim is made. Exact test262 DateTimeFormat remains 244/244 passing before and after. Full unit gate: 1,719 passed, 1 skipped, 0 failed, 0 leaked. The all-spelling static-cell test passes normally and under TSan; focused bounded, moving, parallel, and OOM DateTimeFormat tests pass 9/9.
1 parent e9d641d commit 10ac2f3

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎src/context.zig‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13457,6 +13457,38 @@ test "Intl.DateTimeFormat uses only native resolved state and preserves legacy c
1345713457
)).asBool());
1345813458
}
1345913459

13460+
test "Intl.DateTimeFormat resolved field enums use exact unmanaged static cells" {
13461+
try std.testing.expect(value.IntlDateTimeFormatData.Field.none.value() == null);
13462+
const Case = struct { source: []const u8, expected: []const u8, cell: *const strcell.StringCell };
13463+
const cases = [_]Case{
13464+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',year:'numeric'}).resolvedOptions().year", .expected = "numeric", .cell = strcell.staticCell("numeric") },
13465+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',year:'2-digit'}).resolvedOptions().year", .expected = "2-digit", .cell = strcell.staticCell("2-digit") },
13466+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',month:'long'}).resolvedOptions().month", .expected = "long", .cell = strcell.staticCell("long") },
13467+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',weekday:'short'}).resolvedOptions().weekday", .expected = "short", .cell = strcell.staticCell("short") },
13468+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',weekday:'narrow'}).resolvedOptions().weekday", .expected = "narrow", .cell = strcell.staticCell("narrow") },
13469+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',dateStyle:'full'}).resolvedOptions().dateStyle", .expected = "full", .cell = strcell.staticCell("full") },
13470+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',dateStyle:'medium'}).resolvedOptions().dateStyle", .expected = "medium", .cell = strcell.staticCell("medium") },
13471+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',timeZoneName:'shortOffset'}).resolvedOptions().timeZoneName", .expected = "shortOffset", .cell = strcell.staticCell("shortOffset") },
13472+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',timeZoneName:'longOffset'}).resolvedOptions().timeZoneName", .expected = "longOffset", .cell = strcell.staticCell("longOffset") },
13473+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',timeZoneName:'shortGeneric'}).resolvedOptions().timeZoneName", .expected = "shortGeneric", .cell = strcell.staticCell("shortGeneric") },
13474+
.{ .source = "new Intl.DateTimeFormat('en', {timeZone:'UTC',timeZoneName:'longGeneric'}).resolvedOptions().timeZoneName", .expected = "longGeneric", .cell = strcell.staticCell("longGeneric") },
13475+
};
13476+
13477+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
13478+
.enable_gc = true,
13479+
.enable_jit = false,
13480+
});
13481+
defer ctx.destroy();
13482+
for (cases) |case| {
13483+
const result = try ctx.evaluate(case.source);
13484+
try std.testing.expectEqualStrings(case.expected, result.asStr());
13485+
try std.testing.expectEqual(case.cell, result.asStringCell());
13486+
try std.testing.expect(!result.asStringCell().isGcManaged());
13487+
ctx.collectGarbage();
13488+
try std.testing.expectEqualStrings(case.expected, result.asStr());
13489+
}
13490+
}
13491+
1346013492
test "Intl.DateTimeFormat resolved state is reclaimed under a bounded precise heap" {
1346113493
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
1346213494
.enable_gc = true,

‎src/interpreter.zig‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31304,24 +31304,24 @@ fn intlResolvedOptionsFn(comptime service: []const u8) value.NativeFn {
3130431304
try self.setProp(o, "hour12", Value.boolVal(data.hour12));
3130531305
}
3130631306
const put = struct {
31307-
fn string(s: *Interpreter, target: *value.Object, name: []const u8, bytes: []const u8) EvalError!void {
31308-
if (bytes.len > 0) try s.setProp(target, name, try Value.strAlloc(s.arena, bytes));
31307+
fn field(s: *Interpreter, target: *value.Object, name: []const u8, resolved: value.IntlDateTimeFormatData.Field) EvalError!void {
31308+
if (resolved.value()) |projected| try s.setProp(target, name, projected);
3130931309
}
31310-
}.string;
31311-
try put(self, o, "weekday", data.resolved_weekday.string());
31312-
try put(self, o, "era", data.resolved_era.string());
31313-
try put(self, o, "year", data.resolved_year.string());
31314-
try put(self, o, "month", data.resolved_month.string());
31315-
try put(self, o, "day", data.resolved_day.string());
31316-
try put(self, o, "dayPeriod", data.resolved_day_period.string());
31317-
try put(self, o, "hour", data.resolved_hour.string());
31318-
try put(self, o, "minute", data.resolved_minute.string());
31319-
try put(self, o, "second", data.resolved_second.string());
31310+
}.field;
31311+
try put(self, o, "weekday", data.resolved_weekday);
31312+
try put(self, o, "era", data.resolved_era);
31313+
try put(self, o, "year", data.resolved_year);
31314+
try put(self, o, "month", data.resolved_month);
31315+
try put(self, o, "day", data.resolved_day);
31316+
try put(self, o, "dayPeriod", data.resolved_day_period);
31317+
try put(self, o, "hour", data.resolved_hour);
31318+
try put(self, o, "minute", data.resolved_minute);
31319+
try put(self, o, "second", data.resolved_second);
3132031320
if (data.resolved_fractional_second_digits > 0)
3132131321
try self.setProp(o, "fractionalSecondDigits", Value.num(@floatFromInt(data.resolved_fractional_second_digits)));
31322-
try put(self, o, "timeZoneName", data.resolved_time_zone_name.string());
31323-
try put(self, o, "dateStyle", data.date_style.string());
31324-
try put(self, o, "timeStyle", data.time_style.string());
31322+
try put(self, o, "timeZoneName", data.resolved_time_zone_name);
31323+
try put(self, o, "dateStyle", data.date_style);
31324+
try put(self, o, "timeStyle", data.time_style);
3132531325
} else if (comptime std.mem.eql(u8, service, "RelativeTimeFormat")) {
3132631326
const data = this.asObj().intlRelativeTimeFormatData().?;
3132731327
try self.setProp(o, "style", try Value.strAlloc(self.arena, data.style.string()));

‎src/value.zig‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,26 @@ pub const IntlDateTimeFormatData = struct {
11801180
.long_generic => "longGeneric",
11811181
};
11821182
}
1183+
1184+
/// ECMA-402 resolved component/style names are a closed set. Project
1185+
/// them through immutable cells so reflection does not manufacture a
1186+
/// managed copy of native enum state on every resolvedOptions call.
1187+
pub fn value(self: Field) ?Value {
1188+
return switch (self) {
1189+
.none => null,
1190+
.numeric => Value.str("numeric"),
1191+
.two_digit => Value.str("2-digit"),
1192+
.long => Value.str("long"),
1193+
.short => Value.str("short"),
1194+
.narrow => Value.str("narrow"),
1195+
.full => Value.str("full"),
1196+
.medium => Value.str("medium"),
1197+
.short_offset => Value.str("shortOffset"),
1198+
.long_offset => Value.str("longOffset"),
1199+
.short_generic => Value.str("shortGeneric"),
1200+
.long_generic => Value.str("longGeneric"),
1201+
};
1202+
}
11831203
};
11841204

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

0 commit comments

Comments
 (0)