{"id":25191641,"url":"https://github.com/nikomalik/low-level-functions","last_synced_at":"2025-05-07T21:42:13.911Z","repository":{"id":253396863,"uuid":"843379028","full_name":"NikoMalik/low-level-functions","owner":"NikoMalik","description":"Low-level functions for golang that help to avoid allocations ","archived":false,"fork":false,"pushed_at":"2025-04-11T15:14:47.000Z","size":117,"stargazers_count":25,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-11T16:28:16.170Z","etag":null,"topics":["go","gopkg","low-level-functions","low-level-programming","optimization","optimization-algorithms","optimization-methods","pkgutils"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NikoMalik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-16T11:40:18.000Z","updated_at":"2025-04-11T15:14:50.000Z","dependencies_parsed_at":"2024-08-20T15:46:06.761Z","dependency_job_id":"847af08f-1073-42b7-8358-5140279eb6b6","html_url":"https://github.com/NikoMalik/low-level-functions","commit_stats":null,"previous_names":["nikomalik/low-level-functions"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikoMalik%2Flow-level-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikoMalik%2Flow-level-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikoMalik%2Flow-level-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NikoMalik%2Flow-level-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NikoMalik","download_url":"https://codeload.github.com/NikoMalik/low-level-functions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252961805,"owners_count":21832189,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","gopkg","low-level-functions","low-level-programming","optimization","optimization-algorithms","optimization-methods","pkgutils"],"created_at":"2025-02-09T22:35:52.026Z","updated_at":"2025-05-07T21:42:13.888Z","avatar_url":"https://github.com/NikoMalik.png","language":"Go","readme":"# low-level-functions\n\n\u003e Low-level functions for golang that help to avoid allocations\n\n1. **String(b []byte) string**\n\n   - **What it does**: Converts a slice of bytes to a string without copying data, using insecure data access.\n   - **Why it's faster**: Avoids copying data by creating a string that references the same bytes as the original slice.\n   - **Risks**: Violates the immutability of strings in Go. If the source slice is changed, the string will change as well, which can lead to unexpected bugs.\n\n2. **StringToBytes(s string) []byte**\n\n   - **What it does**: Converts a string to a slice of bytes without copying the data, using insecure data access.\n   - **Why it's faster**: Similar to `String`, avoids copying data by returning a slice that references the same bytes as the string.\n   - **Risks**: If the string changes, the byte slice will change synchronously, which can cause problems when the data changes.\n\n3. **CopyString(s string) string**\n\n   - **What it does**: Creates a copy of a string by creating a new byte slice and copying the data from the string into that slice.\n   - **Why it's faster**: Uses `MakeNoZero`, which does not initialize memory with zeros, speeding up the creation of a new slice.\n   - **Risks**: Fewer risks compared to `String`, but there may be problems if the original data in the string must be kept intact.\n\n4. **ConvertSlice[TFrom, TTo any](from []TFrom) ([]TTo, error)**\n\n   - **What it does**: Converts a slice of one type to a slice of another type, provided the element sizes are the same.\n   - **Why it's faster**: Doesn't create a new slice, just changes the slice header, keeping a reference to the same data in memory.\n   - **Risks**: Conversion errors can cause failures if element sizes do not match or types are not compatible.\n\n5. **MakeNoZero(l int) []byte**\n\n   - **What it does**: Allocates memory for a byte slice without initializing it with zeros.\n   - **Why it's faster**: Skips the step of initializing memory with zeros, which significantly speeds up the memory allocation process.\n   - **Risks**: May lead to unexpected results if not all bytes are written before they are read.\n\n6. **MakeNoZeroCap(l int, c int) []byte**\n\n   - **What it does**: Creates a slice of bytes with a specific length and capacity without initializing with zeros.\n   - **Why it's faster**: Similar to `MakeNoZero`, avoids initializing memory with zeros, which increases the speed of memory allocation.\n   - **Risks**: Similar to `MakeNoZero`.\n\n7. **StringBuffer (structure and methods)**\n\n   - **What it does**: Implements a buffer for working with strings with the ability to change the length and capacity.\n   - **Why it's faster**: Uses `MakeNoZeroCap` to allocate memory, which speeds up buffer handling. Also uses low-level methods to add data.\n   - **Risks**: The main risks are associated with the use of `MakeNoZeroCap`; data initialization may be skipped.\n\n8. **ConvertOne[TFrom, TTo any](from TFrom) (TTo, error)**\n\n   - **What it does**: Converts a value of one type to a value of another type if their sizes are the same.\n   - **Why it's faster**: Converts data directly through unsafe type conversion without creating additional data.\n   - **Risks**: Conversion errors can result in incorrect data or crashes.\n\n9. **MustConvertOne[TFrom, TTo any](from TFrom) TTo**\n\n   - **What it does**: Converts one value of one type to a value of another type if their sizes match. Does not return an error.\n   - **Why it's faster**: Avoids error checking, which makes the function execution faster.\n   - **Risks**: If errors occur, there is no way to handle them, which may cause the program to crash.\n\n10. **MakeZero[T any](l int) []T**\n\n    - **What it does**: Allocates memory for a slice of any type with initialization with zeros.\n    - **Why it's faster**: Optimized for large amounts of data, uses `mallocgc` with initialization.\n    - **Risks**: Similar `MakeNoZero`.\n\n11. **MakeZeroCap[T any](l int, c int) []T**\n\n    - **What it does**: Creates a slice with a specific length and capacity with initialization with zeros.\n    - **Why it's faster**: Similar to `MakeZero`, optimized for big data.\n    - **Risks**: Similar `MakeNoZeroCap`.\n\n12. **MakeNoZeroString(l int) []string**\n\n    - **What it does**: Allocates memory for a slice of strings without initializing it with zeros.\n    - **Why it's faster**: Skips initialization of memory with zeros.\n    - **Risks**: Similar to `MakeNoZero`, may lead to unexpected results if strings are read before writing.\n\n13. **MakeNoZeroCapString(l int, c int) []string**\n\n    - **What it does**: Creates a slice of strings with a specific length and capacity without initializing with zeros.\n    - **Why it's faster**: Skips memory initialization with zeros.\n    - **Risks**: Similar to `MakeNoZeroCap`.\n\n14. **Equal(a, b []byte) bool**\n\n    - **What it does**: Compares two byte slices for equality using the low-level `memequal` function.\n    - **Why it's faster**: Uses direct memory comparison, which is faster than byte-by-byte comparison.\n    - **Risks**: If the slice lengths do not match, it immediately returns false, which may not always be the desired behavior.\n\n15. **IsNil(v any) bool**\n\n    - **What it does**: Checks if the value is nil.\n    - **Why it's faster**: Uses direct checking with `reflect.ValueOf`.\n    - **Risks**: Can cause panic when passing an uninitialized interface.\n\n16. **IsEqual(v1, v2 any) bool**\n\n    - **What it does**: Checks if two values point to the same memory location.\n    - **Why it's faster**: Uses direct pointer comparison.\n    - **Risks**: May not work correctly if values are of composite types or if objects are in different memory locations but with the same values.\n\n17. **AtomicCounter (structure and methods)**\n\n    - **What it does**: Implements an atomic counter using cache-line alignment to prevent false splitting.\n    - **Why it's faster**: Avoids false splits by using cache-line alignment, which improves performance in multi-threaded environments.\n    - **Risks**: More memory consumption due to alignment.\n\n18. **GetItem[T any](slice []T, idx int) T**\n\n    - **What it does**: Gets the slice item by index using an unsafe pointer cast.\n    - **Why it's faster**: Avoids additional checks and accesses memory directly.\n    - **Risks**: May cause panic if the index goes outside the slice.\n\n19. **GetItemWithoutCheck[T any](slice []T, idx int) T**\n\n    - **What it does**: Similar to `GetItem`, but without the index check.\n    - **Why it's faster**: Removes the index check, which speeds up access.\n    - **Risks**: Very unsafe, as going beyond the slice can cause data corruption or program crash.\n\n20. **Pointer[T any](d T) \\*T**\n    - **What it does**: Returns a pointer to the passed value.\n    - **Why it's faster**: Very simple operation, no data copying required.\n    - **Risks**: Few risks, but requires caution when working with pointers.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikomalik%2Flow-level-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikomalik%2Flow-level-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikomalik%2Flow-level-functions/lists"}