Skip to content
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

Optimize custom importer #85

Merged
merged 2 commits into from
Apr 2, 2024
Merged

Optimize custom importer #85

merged 2 commits into from
Apr 2, 2024

Conversation

ntkme
Copy link
Member

@ntkme ntkme commented Apr 1, 2024

This PR substantially improves the performance of custom importers that return paths without source contents, by using FileImporter to load file from disk directly on dart-sass side, instead of reading from on ruby side and then passing to dart-sass via protobuf.

The current sass module resolution spec does not allow file extensions other than the standard ['.sass', '.scss', '.css']. The dart-sass implementation does not resolve if a custom FileImporter returns a url with non-standard extension. However, loading of sass files with non-standard extensions is used in sprockets. Therefore, in such case we have to fallback to an Importer to read the file on ruby side.

@ntkme ntkme force-pushed the refactor-custom-importer branch 4 times, most recently from a6305b6 to 8126841 Compare April 2, 2024 00:42
Substantially improve the performance of custom importers that return
paths without source contents, by using FileImporter to load file from
disk directly on dart-sass side, instead of reading from on ruby side
and then pass to dart-sass via protobuf.
@ntkme ntkme merged commit d988fde into main Apr 2, 2024
76 checks passed
@ntkme ntkme deleted the refactor-custom-importer branch April 2, 2024 00:57
@ntkme
Copy link
Member Author

ntkme commented Apr 2, 2024

require_relative 'lib/sassc-embedded'
require 'benchmark'

puts Sass.info

class CustomImporter < SassC::Importer
  def imports(path, _parent_path)
    Import.new(path)
  end
end

bootstrap = './vendor/github.com/twbs/bootstrap-rubygem/assets/stylesheets/_bootstrap.scss'

result = Benchmark.measure do
  100.times do
    SassC::Engine.new(File.read(bootstrap), { style: :compressed, filename: bootstrap, importer: CustomImporter }).render
  end
end

puts result

Before:

sass-embedded	1.72.0	(Embedded Host)	[Ruby]
dart-sass	1.72.0	(Sass Compiler)	[Dart]
  1.839168   0.735015   2.574183 ( 17.942375)

After:

sass-embedded	1.72.0	(Embedded Host)	[Ruby]
dart-sass	1.72.0	(Sass Compiler)	[Dart]
  0.967505   0.322428   1.289933 ( 16.500571)

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.

None yet

1 participant