Development

ข่าวนี้เหมาะมากสำหรับคนที่สนใจเรื่องการออกแบบคอมไพเลอร์นะครับ (มีหรือเปล่าหว่า?)

ย้อนความกันหน่อยว่า เว็บไซต์ขนาดมหึมาอย่าง Facebook ถูกเขียนขึ้นมาด้วย PHP แต่จำนวนผู้ใช้ระดับนี้ ต้องการประสิทธิภาพที่สูงกว่า PHP ทั่วไป และแนวทางมาตรฐานของวงการคือแปลงฟังก์ชันบางส่วนเป็น C++ เพื่อรีดประสิทธิภาพให้ดียิ่งขึ้น

อย่างไรก็ตาม การแปลบางส่วนของโค้ด PHP เป็น C++ จะมีปัญหาเรื่องการดูแลรักษาโค้ดในระยะยาว (โดยเฉพาะโค้ดที่ซับซ้อนระดับของ Facebook) ซึ่งทางแก้ของบริษัทก็คือพัฒนา HipHop for PHP เป็นตัวช่วยแปล PHP เป็น C++ โดยอัตโนมัติ นั่นคือตอนโปรแกรมเมอร์เขียนก็เป็นเป็น PHP แต่ตอนใช้งานจริงก็ใช้ HipHop ช่วยแปลเป็น C++ ให้ แล้วนำไปคอมไพล์ตามปกติอีกครั้ง (รายละเอียดอ่านในข่าวเก่า)

อธิบายอีกครั้งคือ กระบวนการแปลโค้ดของ Facebook จะเป็น 2 ขั้นดังนี้

  1. PHP→AST→C++ ด้วย HipHop (หมายเหตุ: AST = abstract syntax tree)
  2. C++→x64 ด้วย g++

แต่ในการใช้งานจริง Facebook แบ่ง HipHop for PHP ออกเป็น 2 โหมดสำหรับ 2 งาน

  • HipHop interpreter (hphpi) ตัวแปลภาษาแบบ interpreter สำหรับโปรแกรมเมอร์ภายในบริษัททดสอบโค้ดใหม่ๆ จะได้ไม่เสียเวลาคอมไพล์
  • HipHop compiler (hphpc) ตัวแปลภาษาแบบ compiler สำหรับงานที่ใช้จริง ให้บริการผู้ใช้จริงๆ เพื่อประสิทธิภาพที่ดีกว่า

ปัญหาที่ Facebook พบกับ HipHop มีสองประการ

  • ทั้ง hphpi/hphpc ใช้การแปลงโค้ด PHP→C++ แบบ static ซึ่งมีอุปสรรคในการปรับแต่งโค้ดให้มีประสิทธิภาพที่ดีขึ้น (อ่านเรื่อง binary translation ประกอบ)
  • ฐานโค้ดของ hphpi/hphpc เป็นคนละชุดกัน มีกระบวนการแปลโค้ด (ส่วนของ AST) คนละชุดกัน ทำให้ดูแลโค้ดของ hphpi/hphpc ลำบากเพราะต้องทำสองชุดขนานกันไป

ทางออกของ Facebook ในการแก้ปัญหาทั้งสองอย่างคือเปลี่ยนมาใช้การแปลโค้ดแบบ dynamic translation ที่มีประสิทธิภาพมากขึ้น และสร้าง "ไบต์โค้ด" กลางที่ใช้ร่วมกันระหว่างโหมด interpreter/compiler จะได้ดูแลง่ายขึ้น

ชื่อของมันคือ HipHop Virtual Machine (hhvm) ส่วนภาษาไบต์โค้ดใหม่เรียกว่า HipHop bytecode (HHBC)

hhvm/hhbc ใช้เทคนิคการแปลโค้ดที่ต่างไปจาก virtual machine อื่นๆ ในท้องตลาด (Java และ C# ใช้ JIT ส่วนเอนจินจาวาสคริปต์อย่าง TraceMonkey ใช้วิธี trace) เทคนิคของ hhvm จะเรียบง่ายกว่า โดยเรียกว่า "tracelet" (รายละเอียดผมคงไม่ลงลึก ใครอยากรู้ก็ตามไปอ่านกันต่อเองครับ)

ตอนนี้ Facebook พัฒนา hhvm ให้มีประสิทธิภาพดีกว่า hphpi ได้แล้ว (เร็วกว่า 1.6 เท่า) แต่ยังไม่เร็วเท่ากับ hphpc (ทำงานได้ 0.6 เท่าของ hphpc) อย่างไรก็ตาม Facebook คาดว่าจะพัฒนา hhvm ให้แซง hphpc ได้ในท้ายที่สุด

นักพัฒนาของ Facebook เปลี่ยนจาก hphpi มาเป็น hhvm สำหรับงานพัฒนาภายในองค์กรกันแล้ว ซึ่ง Facebook ให้เหตุผลว่ากระบวนการแปลโค้ดที่เร็วขึ้นจากเดิม 3 วินาทีก็มีความหมายกับโปรแกรมเมอร์ของบริษัทเป็นอย่างมาก

โค้ดของ hhvm ถูกปล่อยออกมาแล้วใน GitHub ของ HipHop ใครสนใจก็ตามไปดูกันเองนะครับ

ที่มา - Facebook Engineering's Notes

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand