Skip to content

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

Merged
merged 5 commits into from
Mar 6, 2020

Conversation

exoego
Copy link

@exoego exoego commented Oct 17, 2019

From my observation on Scala.js-generated JS files,

  • normal val in object are instantiated if the object is referenced, even if the vals are not used in user-land.
  • lazy val are initialized with dummy value and then lazy-initialization code is generetated in JS. If lazy 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 or String) are not marked to avoid lazy-initialization overhead, since their involvement may not increase JS size so much.

@cquiroz
Copy link
Owner

cquiroz commented Oct 17, 2019

Thanks for this, great work
Did you quanitfy the improvement on size? I have done a few rounds of these and noted that the improvement is not always as I expected so I run them through a test project that uses scala-java-time

@exoego
Copy link
Author

exoego commented Oct 18, 2019

I experimented on my company project using this branch.
Unfortunately, a fullOptJS-produced file became 2-3% larger (tens of KB) on that project.
The project uses only OffsetDateTime and some constants (predefined instances) of DateFormatter, so I expected some amount of size reduction... but opposite.

I will have more experiments.

@cquiroz
Copy link
Owner

cquiroz commented Mar 4, 2020

This PR is kind of hard to get now with the latest format changes. Would you like to redo it?

exoego added 3 commits March 6, 2020 10:50
# 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
@exoego
Copy link
Author

exoego commented Mar 6, 2020

This PR reduced the size of demo-opt.js from 621941 bytes to 583983 bytes (6% reduction).

BTW, instead of lazy val, should we use def on constants as same as in PR #158 where possibe?

@cquiroz
Copy link
Owner

cquiroz commented Mar 6, 2020

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

@cquiroz
Copy link
Owner

cquiroz commented Mar 6, 2020

Thanks for this PR

@cquiroz cquiroz merged commit fde94d9 into cquiroz:master Mar 6, 2020
@exoego exoego deleted the lazy-constants branch March 6, 2020 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants