Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does type conversion internally work? What is the memory utilization for the same?

How does Go type conversion internally work?

What is the memory utilisation for a type cast? For example:

var str1 string
str1 = "26MB string data"
byt := []byte(str1)
str2 := string(byt)

whenever I type convert any variable, will it consume more memory?

I am concerned about this because when I try to unmarshall, I get "fatal error: runtime: out of memory"

err = json.Unmarshal([]byte(str1), &obj)

str1 value comes from HTTP response, but read using ioutils.ReadAll, hence it contains the complete response.

like image 901
Hardy Avatar asked Jul 20 '17 07:07

Hardy


People also ask

What is type conversion explain two types of conversion with examples?

In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.

What is type of conversion in computer?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting. Explicit type conversion can also be achieved with separately defined conversion routines such as an overloaded object constructor.

What type of conversion that process a converting value of one data type to another type of data?

Automatic conversion of a value from one data type to another by a programming language, without the programmer specifically doing so, is called implicit type conversion. It happens whenever a binary operator has two operands of different data types.

What is type conversion How is it done in Python?

Type Conversion is the conversion of object from one data type to another data type. Implicit Type Conversion is automatically performed by the Python interpreter. Python avoids the loss of data in Implicit Type Conversion.


1 Answers

It's called conversion in Go (not casting), and this is covered in Spec: Conversions:

Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.

So generally converting does not make a copy, only changes the type. Converting to / from string usually does, as string values are immutable, and for example if converting a string to []byte would not make a copy, you could change the content of the string by changing elements of the resulting byte slice.

See related question: Does convertion between alias types in Go create copies?

There are some exceptions (compiler optimizations) when converting to / from string does not make a copy, for details see golang: []byte(string) vs []byte(*string).

If you already have your JSON content as a string value which you want to unmarshal, you should not convert it to []byte just for the sake of unmarshaling. Instead use strings.NewReader() to obtain an io.Reader which reads from the passed string value, and pass this reader to json.NewDecoder(), so you can unmarshal without having to make a copy of your big input JSON string.

This is how it could look like:

input := "BIG JSON INPUT"
dec := json.NewDecoder(strings.NewReader(input))

var result YourResultType
if err := dec.Decode(&result); err != nil {
    // Handle error
}

Also note that this solution can further be optimized if the big JSON string is read from an io.Reader, in which case you can completely omit reading it first, just pass that to json.NewDecoder() directly, e.g.:

dec := json.NewDecoder(jsonSource)

var result YourResultType
if err := dec.Decode(&result); err != nil {
    // Handle error
}
like image 122
icza Avatar answered Sep 24 '22 07:09

icza