Skip to content

Commit 5462a40

Browse files
committed
perf(tsx): benchmark and perf fixes
1 parent f9ca63c commit 5462a40

File tree

2 files changed

+150
-5
lines changed

2 files changed

+150
-5
lines changed

Diff for: internal/printer/print-to-tsx.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,12 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
574574
p.print(`"`)
575575
endLoc = a.ValLoc.Start
576576
}
577-
contentToValStart := p.sourcetext[:a.ValLoc.Start]
578-
contentToValEnd := p.sourcetext[:endLoc]
579577

580578
if _, ok := htmlEvents[a.Key]; ok {
581-
p.addTSXScript(getUTF16Length(contentToValStart), getUTF16Length(contentToValEnd), a.Val, "event-attribute")
579+
p.addTSXScript(getUTF16Length(p.sourcetext[:a.ValLoc.Start]), getUTF16Length(p.sourcetext[:endLoc]), a.Val, "event-attribute")
582580
}
583581
if a.Key == "style" {
584-
p.addTSXStyle(getUTF16Length(contentToValStart), getUTF16Length(contentToValEnd), a.Val, "style-attribute", "css")
582+
p.addTSXStyle(getUTF16Length(p.sourcetext[:a.ValLoc.Start]), getUTF16Length(p.sourcetext[:endLoc]), a.Val, "style-attribute", "css")
585583
}
586584
case astro.EmptyAttribute:
587585
p.print(a.Key)
@@ -766,7 +764,7 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
766764

767765
if n.FirstChild != nil && (n.DataAtom == atom.Script || n.DataAtom == atom.Style) {
768766
maxLength := endLoc
769-
if endLoc > len(p.sourcetext) { // Sometimes, when tags are not closed properly and stuff, endLoc can be greater than the length of the source text, wonky stuff
767+
if endLoc > len(p.sourcetext) { // Sometimes, when tags are not closed properly, endLoc can be greater than the length of the source text, wonky stuff
770768
maxLength = len(p.sourcetext)
771769
}
772770
contentToContentEnd := p.sourcetext[:maxLength]

Diff for: internal/printer/print-to-tsx_test.go

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package printer
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
astro "github.com/withastro/compiler/internal"
8+
handler "github.com/withastro/compiler/internal/handler"
9+
"github.com/withastro/compiler/internal/transform"
10+
)
11+
12+
func BenchmarkPrintToTSX(b *testing.B) {
13+
source := `---
14+
import MobileMenu from "$components/layout/MobileMenu.astro";
15+
import MobileMenuSide from "$components/layout/MobileMenuSide.astro";
16+
import Header from "$components/layout/Header.astro";
17+
import Socials from "$components/layout/Socials.astro";
18+
import { getBaseSiteURL } from "$utils";
19+
import { Head } from "astro-capo";
20+
import "src/assets/style/prin.css";
21+
import type { MenuItem } from "../data/sidebarMenu";
22+
import Spritesheet from "$components/Spritesheet.astro";
23+
import GoBackUp from "$components/layout/GoBackUp.astro";
24+
25+
interface Props {
26+
title?: string;
27+
description?: string | undefined;
28+
navItems?: MenuItem[];
29+
preloadCatalogue?: boolean;
30+
}
31+
32+
const { title, description, navItems, preloadCatalogue } = Astro.props;
33+
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
34+
---
35+
36+
<html lang="en">
37+
<Head>
38+
<meta charset="UTF-8" />
39+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
40+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
41+
<meta name="generator" content={Astro.generator} />
42+
<title>Erika</title>
43+
44+
<link
45+
rel="preload"
46+
href="/assets/fonts/IBMPlexResult.woff2"
47+
as="font"
48+
type="font/woff2"
49+
crossorigin
50+
/>
51+
52+
<link
53+
rel="preload"
54+
href="/assets/fonts/InterResult.woff2"
55+
as="font"
56+
type="font/woff2"
57+
crossorigin
58+
/>
59+
60+
{preloadCatalogue && <link rel="preconnect" href="/api/catalogue" crossorigin />}
61+
62+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
63+
<meta name="description" content={description ? description : "My personal website"} />
64+
<meta property="og:title" content={title ? title : "Erika"} />
65+
<meta property="og:description" content={description ? description : "My personal website"} />
66+
67+
<meta property="og:type" content="website" />
68+
<meta property="og:site_name" content="erika.florist" />
69+
70+
<meta property="og:image" content={getBaseSiteURL() + "social-card.png"} />
71+
<meta name="twitter:card" content="summary" />
72+
<meta name="twitter:image" content={getBaseSiteURL() + "social-card.png"} />
73+
74+
<link
75+
rel="alternate"
76+
type="application/rss+xml"
77+
title="Blog"
78+
href={getBaseSiteURL() + "rss/blog"}
79+
/>
80+
81+
<link
82+
rel="alternate"
83+
type="application/rss+xml"
84+
title="Catalogue"
85+
href={getBaseSiteURL() + "rss/catalogue"}
86+
/>
87+
88+
<link rel="canonical" href={canonicalURL} />
89+
<meta property="og:url" content={canonicalURL} />
90+
91+
<script src="../assets/scripts/main.ts"></script>
92+
</Head>
93+
<body class="bg-black-charcoal">
94+
<script is:inline>
95+
const theme = localStorage.getItem("theme"),
96+
isSystemDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
97+
theme === "dark" || (!theme && isSystemDark)
98+
? document.documentElement.classList.add("dark")
99+
: theme === "light"
100+
? document.documentElement.classList.remove("dark")
101+
: theme === "system" && isSystemDark && document.documentElement.classList.add("dark");
102+
</script>
103+
<div id="app" class="bg-white-sugar-cane">
104+
<Header />
105+
106+
<main>
107+
<slot />
108+
</main>
109+
110+
<footer
111+
class="m-12 flex justify-center bg-black-charcoal px-5 py-6 leading-tight text-white-sugar-cane sm:m-0 sm:mt-12 sm:px-0"
112+
>
113+
<section class="flex w-centered-width justify-between">
114+
<Socials />
115+
116+
<div class="prose">
117+
Powered by <a href="https://astro.build/">Astro</a><br />
118+
<a href="https://github.com/Princesseuh/erika.florist">Source Code</a><br />
119+
<a href="/changelog/">Changelog</a>
120+
</div>
121+
</section>
122+
</footer>
123+
124+
<GoBackUp />
125+
<MobileMenu />
126+
<MobileMenuSide navMenu={navItems ?? []} />
127+
</div>
128+
<Spritesheet />
129+
</body>
130+
</html>
131+
`
132+
for i := 0; i < b.N; i++ {
133+
h := handler.NewHandler(source, "AstroBenchmark")
134+
var doc *astro.Node
135+
doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h), astro.ParseOptionEnableLiteral(true))
136+
if err != nil {
137+
h.AppendError(err)
138+
}
139+
140+
PrintToTSX(source, doc, TSXOptions{
141+
IncludeScripts: false,
142+
IncludeStyles: false,
143+
}, transform.TransformOptions{
144+
Filename: "AstroBenchmark",
145+
}, h)
146+
}
147+
}

0 commit comments

Comments
 (0)