Skip to content

Commit acd75e6

Browse files
committed
Preload Jieba data on master process
1 parent a6708cb commit acd75e6

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

lib/homeland.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
require "homeland/version"
44
require "homeland/plugin"
55

6+
unless ENV["RAILS_PRECOMPILE"]
7+
# Preload Jieba
8+
require "homeland/search"
9+
end
10+
611
module Homeland
712
cattr_reader :boot_at
813

lib/homeland/search.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Search
55
attr_accessor :term, :terms
66

77
INVALID_CHARS = /[:()&!'"]/
8+
JIEBA = JiebaRb::Segment.new
89

910
def initialize(term)
1011
term = term.to_s.squish.gsub(INVALID_CHARS, "")
@@ -24,9 +25,7 @@ def query_results
2425

2526
class << self
2627
def jieba
27-
return @jieba if defined? @jieba
28-
29-
@jieba = JiebaRb::Segment.new
28+
JIEBA
3029
end
3130
end
3231

lib/tasks/memory.rake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
task memory: :environment do
2+
require "homeland"
3+
include ApplicationHelper
4+
include ActionView::Helpers::OutputSafetyHelper
5+
include ActionView::Helpers::TextHelper
6+
7+
str = Homeland::Markdown.example
8+
9+
a = []
10+
11+
puts "Starting to profile memory..."
12+
b = {}
13+
puts "Before =>"
14+
print_memory
15+
16+
count = 5_000_000
17+
step = (count / 100).to_i
18+
count.times do |i|
19+
sanitize_markdown(Homeland::Markdown.call(str))
20+
21+
if i % step == 0
22+
print_memory
23+
end
24+
end
25+
26+
print_memory
27+
puts GC.start
28+
puts "After GC"
29+
print_memory
30+
end
31+
32+
def print_memory
33+
rss = (`ps -eo pid,rss | grep #{Process.pid} | awk '{print $2}'`.to_i / 1024.0).round(1)
34+
puts "rss: #{rss}mb live objects #{GC.stat[:heap_live_slots]}"
35+
end

0 commit comments

Comments
 (0)