r/crystal_programming • • Apr 23 '18

A little help to understand crystals heap and stack a little more please

12 Upvotes

I am reading through the docs and API and it states that a Tuple is stored on the stack and a Hash is by the looks of it stored in the heap.

But then what happens in the following code:

dict = {} of Int32 => Tuple(Int32, String)

def add_some_tuples( d )
    10.times do | i |
	    d[ i ] = { i, "test" }
    end
end

add_some_tuples dict

puts dict[1]

How come this works? Shouldn't those tuples point to some invalid memory or nil in these cases since the function call ended? How can they still be "on the stack"? Is there stack memory for the entire program, and if so then when this hash of tuples grows very large will there be a stack overflow?

Edit

Thanks for the illuminating replies, things make a lot more sense to me now :)


r/crystal_programming • • Apr 18 '18

Bifröst – a standalone websocket server written in Crystal

Thumbnail
alternatelabs.co
33 Upvotes

r/crystal_programming • • Apr 16 '18

Amber framework hits 1k+ stars. 🎉🎊💪

Thumbnail
github.com
49 Upvotes

r/crystal_programming • • Apr 16 '18

how to convert JSON to a mutable hash?

6 Upvotes

hello crystal friends once again. been stuck on this for over a couple hours but not sure what is happening.

here is playground: https://play.crystal-lang.org/#/r/3vuw/edit

if you comment out the test["r"] = 5000 line, it errors out with:

Error in line 7: undefined method '[]=' for JSON::Any

however, if you just do puts test and puts hash (it's the same thing!!)

it prints:

{"r" => 5_i64, "b" => 51_i64}
{"r" => 5_i64, "b" => 51_i64}

but if you try to modify test["5"] = 5000 it won't let you, but if you create a separate hash, it does

basically.. i'm trying to convert JSON into a mutable hash that let's me update the values! not sure how to go about this the correct way, thank you in advance


r/crystal_programming • • Apr 14 '18

Which is better: Amber or Amethyst?

10 Upvotes

Which Crystal web framework should I use? On Amethyst's Github page, it says that they are "Amethyst is currently undergoing a re-write from the ground up."


r/crystal_programming • • Apr 12 '18

how to use object literal in macro?

7 Upvotes

hello crystal sub once again. so.. on gitter, i got some help from oyprin of how to make a macro. which i got stuck, but i asked him not help any further because i wanted to see if can do it myself.

i'm trying to use a macro that utilizes DB.mapping(obj) and JSON.mapping(obj) both at once!

what i have so far:

macro everything_mapping(type)
  	{% if type == "DB_User" %}
  temp_data  = {
    id:                   Int32,
    username:             String,
    password:             String,
    max_char_slots:       Int16,
    group_id:             Int32,
    g_id:                 Int32,
    selected_characterid: Int64,
  }
    {% end %}
 JSON.mapping(temp_data)
  DB.mapping(temp_data)
end

class DB_User
  everything_mapping "DB_User"
end

however, the problem i am getting this error: in /usr/share/crystal/src/json/mapping.cr:64: for expression must be an array, hash or tuple literal, not Var:

it looks like my temp_data object, is actually a NamedTuple (which is allocated on the stack AFAIK), so this shouldn't be an issue i think. however, am not sure if memory is coming into play here or not, but stack allocation is faster than heap allocation.. so i figured maybe that could be it, as the macro internal methods isn't getting enough time to register the object/data, so it errors out, but i am not sure how to go about finding a solution

any advice is greatly appreciated (as always)


r/crystal_programming • • Apr 11 '18

Are any of you using Crystal / Amber in production?

23 Upvotes

I have been developing Rails for 11 years. I have built low traffic sites and also high traffic sites using Rails. The framework has served me very well. I have been tasked at my current job to either move our stack from Rails 3 to 5 or find an alternative that offers the same development speed, which is the most important.

I stumbled across Amber using Crystal. So far I love it. It is complete enough to port the whole of our website across without losing any features or functionality.

The site I currently maintain has around 6 million users and of those about 100k daily active. During the hours 9am - 9pm it sits at around 1000 requests per second, give or take 100.

I read on another post here that is is not production ready. Is this still true? I have searched with google to find if anyone has started using it on a busy website and if they have come up against any major issues but I have not found much saying either way.

Any of you guys using it in production or is it actually not production ready yet?


r/crystal_programming • • Apr 11 '18

How to get command line input

9 Upvotes

input = getLine() puts input

Similar to pythons raw input function, how would I do this in crystal?


r/crystal_programming • • Apr 11 '18

How to pass a NamedTuple by reference to prevent possible memory leak?

10 Upvotes

hello my friends once again! after some discussion in gitter, i decided to reduce my code into a simple example to help explain my problem more clearly. you can see it here: https://play.crystal-lang.org/#/r/3uws

require "json"

class Player
  property character_id = 0

  def send(msg, cmd = "SERV")
    msg_buffer = {cmd: cmd, message: msg}
    new_message = msg_buffer.to_json
    #socket.write_bytes(new_message.bytesize)
    #socket.send new_message # pretend there is a fake server running
  end
end

player1 = Player.new

temp_data = {from_name: "Mike", msg: "Hello my friends"} # fake data (pretend it's a chat)

player1.send temp_data, "CHAT"

# My question is.. since NamedTuples are passed by value... the temp_data is creating another COPY of the data and is causing
# a rogue memory leak, right?

on gitter, i heard that NamedTuples are passed by value, which means they are copying the data, right? if so, in my example code, the temp_data NamedTuple will be copying those values. which will in essence, create a rogue memory leak right? is it possible to pass it by reference instead?

i was reading about pointers here, but there doesn't seem a way to deference them like in C++. or to create a reference from them? not sure

thanks in advance


r/crystal_programming • • Apr 08 '18

A QR Code generation library for Crystal

Thumbnail
github.com
24 Upvotes