楊志璿

@25077667

Joined on Mar 4, 2017

  • Hello World:u5272: Side project 筆記 Git 貫徹底層 C++ Tips GPT一問 clean code/arch 反思
     Like  Bookmark
  • SOO pointer I RAII Thread-Safe and Re-entrancy FFI Uniform initialization 埋坑清單 constexpr and const restrict slides[target=_blank]
     Like  Bookmark
  • 引言 dictobject.c 的演變對 Python 執行時性能的發展至關重要。最初設計為無序哈希表,字典的實現經歷了顯著變化,以應對不斷變化的性能和記憶體需求。理解 dictobject.c 的歷史背景——包括其在 Python 3.6 中從無序結構轉變為有序結構——能夠提供寶貴的見解,有助於我們理解在記憶體使用、查找效率和順序保留之間的權衡和最佳化。 Python 字典(dict)是 CPython 中最廣泛使用且最重要的數據結構之一。字典的核心實現在 cpython/Objects/dictobject.c 中。本文從資深架構師的角度,對 dictobject.c 中的具體實現進行深入分析,並系統性地闡述 Python 3.x 字典的演變與優化,引用 PEP 372、PEP 412、PEP 509 和 PEP 589,以全面了解這些變更背後的動機(為什麼)和技術方法(如何實現)。 核心設計與挑戰 Python 字典是一個高度優化的哈希表,基於開放位址法,主要目標是在查找、插入和刪除操作中提供 O(1) 的平均時間複雜度。這種效率是通過精心設計的工程優化實現的,包括處理哈希碰撞和管理存儲重新調整的策略。 在 dictobject.c 中,最關鍵的結構是 PyDictObject,它包含指向哈希表的指針、其大小、鍵值對陣列及其他元數據。隨著不同版本的 Python,PyDictObject 的結構也隨之演變,以適應新的記憶體需求和性能優化。特別是在 Python 3.6 中,字典實現從無序轉變為有序,這需要重新組織內部存儲結構,以在不影響查找性能的前提下保留插入順序(有關此變更的動機和影響,請參見 PEP 468)。這一轉變是基於成本效益分析的結果,該分析指出,在不犧牲性能的前提下,這種變化在開發者易用性和一致性方面具有顯著優勢。當時的基準測試表明,保持插入順序只需極少的額外開銷,主要得益於重新設計的鍵值對結構,使其更加緊湊和高效。這一設計使得 Python 能夠默認提供有序字典,增強了可預測性,同時確保查找操作與先前的無序實現一樣高效。
     Like  Bookmark
  • Intro GPT 一問 Implement a Simple Vector Class Interview: Advanced Memory Management and Optimization 深入解析 PEP 683:不朽物件與固定引用計數的錯誤處理 深入分析 CPython dictobject.c 的設計與實現
     Like  Bookmark
  • 本文深入探討 PEP 683: 不朽物件與固定引用計數,解析其對引用計數溢出邊界條件的改進,並探討這些改進如何提升 CPython 的錯誤處理能力及整體效能(參見 PEP 683「Reducing CPU Cache Invalidation」章節)。📈💡 文章詳細分析這些改變,並討論其在大型應用中的實際影響。📚 目標是幫助讀者深入了解 PEP 683 提出的各種技術機制、設計選擇及其長遠影響,特別是在高效能和可靠性方面的優勢。🔍✨ 背景與動機 💭 目前,CPython 的實作中,物件的引用計數是可變的,這導致即使是不可變的物件(例如全域常數 🌍),其內部狀態仍會隨引用計數的變化而改變,帶來以下幾個顯著問題: 效能問題 🚀:引用計數操作會引起 CPU 🖥️ 快取行失效,特別是在多核心環境中,頻繁的引用計數更新容易成為效能瓶頸,尤其在共享變數或熱點數據上。 錯誤處理風險 ⚠️:當引用計數超出其可表示的範圍時,可能導致物件無法正確釋放,最終引發記憶體洩漏甚至系統性錯誤,這在長時間運行或負載波動的環境中尤為危險。 Data Race 風險 💥:儘管 GIL(全域直譯器鎖 🔒)在一定程度上保護了 Python 的多執行緒環境,但引用計數仍然面臨多執行緒操作下的 data race 風險。這些風險對高並發系統構成了挑戰,特別是在 CPU 執行緒間共享記憶體時。 PEP 683 提出的解決方案是在不顯著增加 CPython 複雜性的前提下,透過引入「不朽物件 🏺」機制來解決上述問題(參見 PEP 683「Motivation」章節)。這些物件的引用計數將被固定,避免邊界條件錯誤,進而顯著提升系統的穩定性。這項提案代表了一種簡潔但有效的方式,旨在保持 CPython 的高效能,同時消除記憶體管理中的邊界風險。🔧🔒
     Like  Bookmark
  • Memory Layout and Class Design:Define an object Foo which is an empty class. Explain its memory layout. After defining an empty class Foo, what is the minimum size of this class in memory and why? Adding Members: Modify Foo to contain exactly one int variable. Describe the updated memory layout. In function stack, heap, or static storage duration. How does adding an int member affect the alignment and padding of the class? String Handling:
     Like  Bookmark
  • 因為我很懶,這邊就當我的筆記本,長話短說,就留下一點點小筆記 Windows XP 並沒有支援 C++17 的讀寫鎖因為 AcquireSRWLockExclusive 是從 Windows 7 才開始支援 Gnome 43 在 Wayland 的 system tray 幾乎都會失效。解法:appindicator + libappindicator GTK 2 跟 3 都要裝 MSVC17 的 range-base loop 對於 attribute 有 compiler bug
     Like  Bookmark
  • 目的:Code review interviewee 的作業 關於本次作業如果能重來,你會如何改進你的程式。你可以假定自己重來的時候,具有第一次寫完這份作業的 knowledge,換句話說,你可以保留經過這份作業所習得的知識,重寫一遍,你會如何改進? 在所謂軟體工程的世界,團隊合作是相當重要的。所以你這份作業,會如何展現你在團隊合作,以及前後版本變更當中,所提供的價值? 沒有要嚴厲批判,我們也歡迎擅長使用現代化工具的人。但我想請你誠實面對自己來說,有多少比例是藉由 LLM 輔助你完成的?LLM 讓你在這份作業當中學到了什麼?evidence:connect()、accept()、recv()與send()等系統呼叫 「或監測mprotect()調用」<-> 「ptrace相關功能呼叫」 rootkit hook read()這個system call 數據結構 「必須有良好的log來追蹤所有eBPF的行為,以維持可追朔性。」據你所知有沒有 log 不可使用的情況?對 re-entrancy, concurrent programming 的認知?
     Like  Bookmark
  • <contributors 25077667 > :::success Rules: 在題號後標注該答案完成度 ❌ 尚未填寫 🔰 暫時有解或僅有部份解,且認證未達三次 ✅ 三個人認證此答案是可行答案 誰可以認證答案?
     Like 2 Bookmark
  • https://chatgpt.com/share/c4dfd43c-15f6-4201-a318-7dde06c8e5e0 Background In this interview, we aim to assess the candidate's proficiency in advanced memory management and optimization techniques in C++. The focus will be on their ability to implement and use custom memory allocators in the STL, manage memory pools, understand and exploit memory models and alignment for performance, and apply advanced garbage collection techniques. Interview Questions Task 1: Custom Allocators in STL Question: Implement a custom allocator for an STL container, such as std::vector, that allocates memory from a pre-allocated memory pool. We define the arena's shared interface, which is following the cppreference: https://en.cppreference.com/w/cpp/memory/allocator
     Like 1 Bookmark
  • 突發奇想,讓 GPT 來決定什麼問題是困難的問題,看看我有沒有辦法都達成。 畢竟身為平常沒事就看 Cppreference 與 C++ 各版標準的人,我對 GPT 能問出的問題有十分的信心可以完成。 於是參閱 GPT 認為困難的問題: https://chat.openai.com/share/10bbc2b3-9791-49b3-9957-f0a51843c126 Understood. Let's delve into more challenging and nuanced aspects of C++ programming, going beyond RAII and basic range-based for loops and iterators. Advanced Memory Management and Optimization:Custom allocators in STL: Implementing and using allocators for specialized memory management. Intrusive containers and their advantages in performance-sensitive contexts.Note: This does not seem to fit the "follow-up" structure. We request that ChatGPT revise this item.
     Like  Bookmark
  • contributed by < 25077667 > :::spoiler 平常當社畜,又有其他 side project 要寫,大家都好忙,大家一起加油! ::: 前置踩坑 ➜ ~ gcc -v Using built-in specs. COLLECT_GCC=gcc
     Like 2 Bookmark
  • Interview Question: Expression Templates and Compile-Time Polymorphism Background: Expression templates are a C++ template metaprogramming technique used for optimizing high-performance computations, particularly in numerical computing and scientific applications. They enable the efficient evaluation of complex expressions by eliminating unnecessary temporaries and combining multiple operations into a single loop at compile time. Task: Implement a Simple Vector Class:Create a template class Vector that encapsulates a dynamic array of elements (use std::vector for storage). Implement basic operations like element access and size retrieval.
     Like  Bookmark
  • using K = key_type; using T = value_type; class config_base { config_base() {} public: virtual T get(const K) = 0; virtual void set(const K, const K) = 0; };
     Like  Bookmark
  • 這篇起源自前一場面試,因為筆者這個嘴砲性格,遇到不如意總會說個幾句 於是有這篇詳述當場面試官的「錯誤」 一句話概括這個標題:「不是所有 thread-safe 都是 reentrant」,千萬不要誤把 thread-safe 就當作 reentrance。 前情背景會讓筆者下定決心寫這邊部落格,除了面試官觀念錯誤之外,還對筆者搖頭嘆氣。 其實以前就有寫過文章,但當時沒有寫好重寫會更好。 定義
     Like  Bookmark
  • 上面文章總說改天介紹,這邊就是改天介紹的 todo list 或者讀者想看什麼樣的議題,也可以補充在這邊 SignatureDefault type in C89a = malloc(1024) ABI issue Itanium C++ ABI RTTI (should I put it here?) pimpl idiom
     Like  Bookmark
  • 似乎蠻多人對 FFI, ABI issue 沒有深入探討 筆者也早在埋坑清單上面留下 ABI issue 預計要編寫,然而遇到另外一篇專案原作者表示需要用到其他不同語言的三方依賴庫,而他認為這部份如果不使用跟三方相依的程式語言會有開發困難,於是跟著寫三方語言。 筆者認為這部份,需要考驗程式設計師對於程式如何運作的基本功。所以先撰寫這篇 FFI 吧! 如何在 Python 呼叫 C++ 撰寫的程式? 眾所周知,C++ 多數情況跑得比 Python 快,很多時候在 Python domain 做運算過度耗時,我們有沒有辦法把一些 sub-routine 搬到 C++ domain 執行呢? 顯然答案是肯定的 binding tools
     Like  Bookmark
  • Q1 Modern C++? 考慮以下程式 void foo() { VMPBegin(); // some complex modern C++ auto res = [] { struct Tmp { constexpr Tmp() = default;
     Like  Bookmark
  • :::success 本篇著重介紹 raw pointer,下篇才會介紹到 smart pointer 建議搭配 Jserv 老師的「你所不知道的C語言:指標篇」閱讀 但是會著重介紹 Jserv 老師沒有介紹到的部份。 :::warning 但不是代表要讀完老師的內容,才能閱讀本篇 這篇會比該篇短 :::
     Like  Bookmark
  • Source :::success 🎉🎉🎉 performance comparism placeholder 🎉🎉🎉 ::: How to verify? Compile the local gcc
     Like  Bookmark