Upgrade to Pro — share decks privately, control downloads, hide ads and more …

环境变量为何能让 Ruby 快十倍

Minqi Pan
September 24, 2016

环境变量为何能让 Ruby 快十倍

Why environment variables could slow down your Ruby scripts for 10 times

https://www.youtube.com/watch?v=A1V-vZklVLY

Minqi Pan

September 24, 2016
Tweet

More Decks by Minqi Pan

Other Decks in Programming

Transcript

  1. GitLab 耬ᵅ #15452 “On average it seems to take around

    13 seconds to cherry-pick commits for GitLab CE. Most of the time appears to be spent in GitHooksService#execute (around 10 seconds on average).” – Yorick Peterse
  2. – Yorick Peterse “I'm a bit confused, how does unsetting

    environment variables make things run faster?”
  3. – Douwe Maan “The measurements don't lie, but I'm not

    quite sure why this works either. Can you elaborate?”
  4. bundler/setup ጱֵአ࣋ว • bin/rails ᧣አԧ bundler/setup • bundle exec *

    ᧣አԧ bundler/setup • bundle show * ᧣አԧ bundler/setup • bundle bin-stubs ጱኞ౮ᇔ᮷᧣አԧ bundler/setup • ࢩ種 bundler/setup پԒฎ Ruby 貉ۖ鉑纷ጱຽ蟴
  5. bundler/setup ጱᦡᦇፓຽ • “make your code see just the dependencies

    that it should see and nothing else” • 矒ګঅ gem ପጱے᫹᪠趥矾ᔱᶲଧ • ᦏ纷ଧ௴অ胼୚አک Gemfile Ӿ瞲ਧጱ粚๜ጱପ • ᘒӬ獡ฎ Gemfile Ӿ๚瞲ਧጱ gem ᮷犋ݢᥠ
  6. Kernel#require • require ‘thing’ • ইຎ ‘thing’ ૪ᕪے᫹鉑牧ಗᤈᕮ๳ • 玽ڞ螁ܲ

    $LOAD_PATH ($:) ತک ‘thing’ ଚے᫹ԏ • 舙ತ犋کڞಲڊ LoadError
  7. rubygems/core_ext/ kernel_require.rb • ᯿碝಑୏ Kernel ଚ remove_method ധԧ :require •

    ᯿碝ਧԎԧ Kernel#require • ᛔ Ruby 1.9 犥๶ RubyGems ฎٖᗝጱଚᛔۖ貉አ • ᛔ Ruby 1.9 犥๶鉖಍ฎ Kernel#require ጱ፥ྋᶎፓ
  8. rubygems/core_ext/ kernel_require.rb • require ‘thing’ • ইຎ ‘thing’ ૪ᕪے᫹鉑牧ಗᤈᕮ๳ •

    玽ڞ螁ܲ $LOAD_PATH ($:) ತک ‘thing’ ଚے᫹ԏ • 玽ڞ՗૪ਞᤰጱ gem Ӿ੔ತ ‘thing’ ଚ಩ᧆ gem ጱ ᪠趥ے獈 $LOAD_PATH ($:) • 舙ತ犋کڞಲڊ LoadError
  9. bundler/setup ࣁ狶Ջԍ • ತکӞᕟ粬ਧ粚๜ጱ gem牧玲ڊ犢ժጱ gemspec • ಩ਙժጱ lib/ ፓ趟ጱᕷ੒᪠趥ے獈

    $LOAD_PATH牧 犥׎ Ruby ݢ犥 require ک粬ਧ粚๜ጱ gem • “unpatch”物݄ധ rubygems/core_ext/ kernel_require.rb ጱ鉪ᬋ牧犥׎贶ሿ“獡ฎ Gemfile Ӿ๚瞲ਧጱ gem ᮷犋ݢᥠ”
  10. Gemfile.lock ጱᦡᦇ௏మ • 薹ຉਠ Gemfile ݸ಩ᕮຎᖨਂӥ๶ጱӞӻ๢ګ • ۱珀 “exact versions

    of all of the gems that you used the last time you know for sure that the application worked” • 犌 npm 2 / npm 3 ᦡᦇ௏మጱ੒穉
  11. Gemfile 犌 Gemfile.lock 
 ݢ胼ڊሿ犋ݶྍ • ࣁ૪磪 Gemfile.lock ጱ眐٭ӥ牧አಁๅ碝ԧ Gemfile

    ᘒဌ磪᯿碝 bundle牧ڞ Gemfile.lock 鉑๗ • 舙羬ᕹ૪磪胼ჿ᪃ Gemfile 碝襑穩ጱ gem牧ڞ貉ۖ ଫአ෸ᛔۖๅ碝 Gemfile.lock • 舙羬ᕹჿ᪃犋ԧ Gemfile ጱ碝襑穩牧ڞಸᲙ • ࢩ種ྯ稞貉ۖ᮷ᥝ᯿碝薹 Gemfile + Gemfile.lock
  12. 禊盢ು虡 • search_for(dependency) አ๶矾ᔱჿ᪃ᕳਧ狅ᩢጱ狅ᩢᥢ໒ • dependencies_for(specification) አ๶蜴ࢧᕳਧᥢ໒ጱ狅ᩢ • requirement_satisfied_by?(requirement, activated,

    spec) ࣁ୮ 獮ጱ狅ᩢࢶ activated Ӿᦇᓒ requirement ฎ玽胼ᤩ spec ჿ᪃ • sort_dependencies(dependencies, activated, conflicts) ԅ൉ ṛ௔胼牧ೲ“਻ฃ薹”ጱᶲଧ矎ଧ dependencies ؃ᦡਂࣁӞӻ“狅ᩢᥢ໒൉׀ᘏ”牧ਙ贶ሿইӥොဩ
  13. 盥綡 • ྯ稞貉ۖጱ෸狡ಗᤈ Molinillo ᓒဩ፥ጱ஠ᥝހ牫 • Gemfile + Gemfile.lock ጱݶྍ௔፥ጱ盄᯿ᥝހ牫

    • ᇚᇗྯ稞貉ۖ Ruby 纷ଧጱ硳ሲ牧绗ᒌഘ๶ԧՋԍ牫 • Gemfile.lock ጱᦡᦇฎ஠ᶳጱހ牫