-
Notifications
You must be signed in to change notification settings - Fork 34
Instantiate constants lazily so unused constants are not instantiated. #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for this, great work |
I experimented on my company project using this branch. I will have more experiments. |
This PR is kind of hard to get now with the latest format changes. Would you like to redo it? |
# Conflicts: # core/jvm/src/main/scala/org/threeten/bp/zone/TzdbZoneRulesCompiler.scala # core/shared/src/main/scala/org/threeten/bp/DayOfWeek.scala # core/shared/src/main/scala/org/threeten/bp/Duration.scala # core/shared/src/main/scala/org/threeten/bp/Instant.scala # core/shared/src/main/scala/org/threeten/bp/LocalDate.scala # core/shared/src/main/scala/org/threeten/bp/LocalDateTime.scala # core/shared/src/main/scala/org/threeten/bp/LocalTime.scala # core/shared/src/main/scala/org/threeten/bp/Month.scala # core/shared/src/main/scala/org/threeten/bp/MonthDay.scala # core/shared/src/main/scala/org/threeten/bp/OffsetDateTime.scala # core/shared/src/main/scala/org/threeten/bp/OffsetTime.scala # core/shared/src/main/scala/org/threeten/bp/Period.scala # core/shared/src/main/scala/org/threeten/bp/Year.scala # core/shared/src/main/scala/org/threeten/bp/YearMonth.scala # core/shared/src/main/scala/org/threeten/bp/ZoneOffset.scala # core/shared/src/main/scala/org/threeten/bp/chrono/Chronology.scala # core/shared/src/main/scala/org/threeten/bp/chrono/HijrahDate.scala # core/shared/src/main/scala/org/threeten/bp/chrono/HijrahEra.scala # core/shared/src/main/scala/org/threeten/bp/chrono/IsoEra.scala # core/shared/src/main/scala/org/threeten/bp/chrono/JapaneseChronology.scala # core/shared/src/main/scala/org/threeten/bp/chrono/JapaneseEra.scala # core/shared/src/main/scala/org/threeten/bp/chrono/MinguoChronology.scala # core/shared/src/main/scala/org/threeten/bp/chrono/MinguoEra.scala # core/shared/src/main/scala/org/threeten/bp/chrono/ThaiBuddhistChronology.scala # core/shared/src/main/scala/org/threeten/bp/chrono/ThaiBuddhistEra.scala # core/shared/src/main/scala/org/threeten/bp/format/DateTimeFormatter.scala # core/shared/src/main/scala/org/threeten/bp/format/DateTimeFormatterBuilder.scala # core/shared/src/main/scala/org/threeten/bp/format/DecimalStyle.scala # core/shared/src/main/scala/org/threeten/bp/format/FormatStyle.scala # core/shared/src/main/scala/org/threeten/bp/format/ResolverStyle.scala # core/shared/src/main/scala/org/threeten/bp/format/SignStyle.scala # core/shared/src/main/scala/org/threeten/bp/format/SimpleDateTimeFormatStyleProvider.scala # core/shared/src/main/scala/org/threeten/bp/format/TextStyle.scala # core/shared/src/main/scala/org/threeten/bp/format/internal/TTBPDateTimeFormatterBuilder.scala # core/shared/src/main/scala/org/threeten/bp/format/internal/TTBPDateTimeTextProvider.scala # core/shared/src/main/scala/org/threeten/bp/temporal/ChronoField.scala # core/shared/src/main/scala/org/threeten/bp/temporal/ChronoUnit.scala # core/shared/src/main/scala/org/threeten/bp/temporal/IsoFields.scala # core/shared/src/main/scala/org/threeten/bp/temporal/JulianFields.scala # core/shared/src/main/scala/org/threeten/bp/temporal/TemporalAdjusters.scala # core/shared/src/main/scala/org/threeten/bp/temporal/WeekFields.scala # core/shared/src/main/scala/org/threeten/bp/zone/ZoneOffsetTransitionRule.scala # core/shared/src/main/scala/org/threeten/bp/zone/ZoneRulesInitializer.scala # core/shared/src/main/scala/org/threeten/bp/zone/ZoneRulesProvider.scala
This PR reduced the size of BTW, instead of |
Wow this is a great improvement. I tried with lazy val and didn't see so much improvements, but I didn't apply it so extensively. From my reading of this scala-js/scala-js#2227 I thought switching to def would be beneficial but I can test that later on Some of my tests switching to def would produce test errors so it has to be tested carefully |
Thanks for this PR |
From my observation on Scala.js-generated JS files,
val
inobject
are instantiated if the object is referenced, even if theval
s are not used in user-land.lazy val
are initialized with dummy value and then lazy-initialization code is generetated in JS. Iflazy val
is not referenced in user-land, such lazy-initialization will get swiped away.So I think making constants
lazy
will reduce generated JS size.Ofcourse lazy-initialization will put a small overhead, but not so serious I assume.
In this PR, non-primitive constants are marked as
lazy
,hoping unused Classes and their instances will not be included in generated JS.
Primitive constants (e.g.
Int
orString
) are not marked to avoid lazy-initialization overhead, since their involvement may not increase JS size so much.